HowTo detect tap on map annotaition Pin? - iphone

I want to detect tap on annotation pin(of mapkit) so that I can perform action on that event.
Now the default annotation flag pops up in case I touch the annotation pin. I want to customise that to call my method when pin is touched.

You need to implement the following delegate method
(MKAnnotationView) mapView viewForAnnotation:(id) annotation
Then just declare following in this method
MKPinAnnotationView *view=[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:#"abc"];
view.canShowCallout=YES;
view.calloutOffset=CGPointMake(-20,10); //As per your choice
Then you can add UI to your callout eg UIButton or UIImage as
view.rightCalloutAccesoryView
View.leftCalloutAccesoryView

As pin on map is MKAnnotationView you can add UITapGestureRecognizer on it(of yours), but if it already has some GestureRecognizer than you will need to remove it first.
Thanks

Related

How to remove pin view?

In my app i have a MapView and one annotation. The first touch on the pin showing it's view, but i can't find the to remove/hide it on another touch.
Thanks
Removes the specified annotation object from the map view.
- (void)removeAnnotation:(id < MKAnnotation >)annotation
Parameters
annotation:
The annotation object to remove. This object must conform to the MKAnnotation protocol.
To remove annotation view only check this question
[mapView deselectAnnotation:[mapView.selectedAnnotations objectAtIndex:0] animated:YES];

in mkmapview didSelectAnnotationView use can only touch a custom marker once, how to clear

I have custom markers in a map view. When the user touches one the app moves to another page. If the user returns to the map and touches the same item again
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
is not called until you touch somewhere in the map and then touch the marker again.
I tried deselecting the annotation view but the docs say not too, and in any case it didn't work.
Any ideas on how to fix this?
Instead of setting view.selected directly which the documentation says not to do, call the deselectAnnotation:animated: method instead:
[mapView deselectAnnotation:view.annotation animated:YES];
By the way, for the reverse, there's the selectAnnotation:animated: method.
Try using a UITapGestureRecognizer to recognize taps on the annotation view.

How do I show multiple MKAnnotations with the callout?

I want to add multiple MKAnnotations with the callout showing. I searched a few different threads on S.O and could not find a way to do it. Has anyone figured out how to add multiple pins with the callout visible?
Ok!
You should use [mapView selectAnnotation:currentAnnotation animated:FALSE]
But be sure to place this in the - (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView
delegate method
Trigger MKAnnotationView callout

Show callout when tapping overlay

I have an MKMapView with several overlays. Works all just fine, and it's incredible how simple it works. There is, however, one thing I can't get to work. The idea is simple: when a user taps within the area that is covered by an overlay, a callout with some information about that overlay has to come up. The overlays are all MKPolygons, which follow the MKOverlay protocol and therefore the MKAnnotation protocol.
The MKOverlay protocol conforms to the
MKAnnotation protocol. As a result,
all overlay objects are also
annotation objects and can be
treated as one or both in your code.
If you opt to treat an overlay object
as both, you are responsible for
managing that object in two places. If
you want to display both an overlay
view and annotation view for it, you
must implement both the
mapView:viewForOverlay: and
mapView:viewForAnnotation: methods in
your application delegate. It also
means that you must add and remove the
object from both the overlays and
annotations arrays of your map.
This comes from the Apple docs. I tried something like this:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
MKAnnotationView *aView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
aView.canShowCallout = YES;
}
But that doesn't seem to work. I've tried using gesture recognizers, but I have no idea how to show a callout other than by using the canShowCallOut property...
I suppose you have to add the MKOverlays as annotations also
[self.mapView addAnnotations:myOverlays];
Then return a MKAnnotationView in (mapView:viewForAnnotation) that's not hidden, either a graphic (tap-able) or zero alpha view. Next, add a UITapGestureRecognizer for each MKOverlayView, make sure it works with the map's gestures (UIGestureRecognizerDelegate implementation for simultaneous recognition). Finally when your gesture recognizer fires do this
[self.mapView setSelectedAnnotations:[NSArray arrayWithObject:myOverlayView.overlay]];
I'm not certain that this actually triggers the callOut showing though.
Also make sure your return title and/or subtitle from your overlay object.

MKAnnotationView image property

I have an MKAnnotationView (draggable property is set to YES) with a custom image, set via the image property. When the annotation is added to the map it has the custom image. But the image turns back to the default pin when the annotation is dragged. I also tried to overwrite the image propery everytime the dragState changes. No luck here also.
You're using an MKPinAnnotationView instance instead of using an MKAnnotationView instance.
You should use an MKAnnotationView, not a MKPinAnnotationView. Also consider my answer on your other question regarding dragging. Pretty cool stuff.
An MKPinAnnotationView is a ready made annotation view, just like the MKPointAnnotation is a concrete implementation of the MKAnnotation protocol.
These two classes could be used together to get quick, uncustomised results.
I believe you may be using MKPinAnnotationView which will always default to a pin as it's lifecycle occurs.