MKMapKit PushPin on Tap
I need to place a pushpin where a user taps on the MKMapView. Any ideas/clues/links for the purpose are requested to be shar开发者_运维百科ed. Thanks.
You can make use of a UITapRecognizer
. In the gesture handler, you can access the coordinate that the user has tapped on using,
CGPoint touchLocation = [gesture locationInView:mapView];
CLLocationCoordinate2D coordinate = [mapView convertPoint:touchLocation toCoordinateFromView:mapView];
You can use this coordinate to create an annotation. For that you will have to define a class which conforms to the MKAnnotation
protocol. Create and add an instance of it to the mapView.
In the delegate method mapView:viewForAnnotation:
, create and return an instance of MKPinAnnotationView
for the annotation.
There is caveat though. Adding the annotation within the gesture handler might not be the best move as gesture handler is unawares to the selection of the annotation view. To deal with this, you can use performSelector:withObject:afterDelay:
to schedule an addition. If the delegate method mapView:didSelectAnnotationView:
is called, cancel the perform request.
精彩评论