I have added an mkpinannotation in an mkmapview.
I wo开发者_运维百科uld like to move the pin to the place i tap on the mkmapview.
Is there an example anywhere?
Help appriciated.
First, add the gesture recognizer to the map view:
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(tapGestureHandler:)];
tgr.delegate = self;
//also add
<UIGestureRecognizerDelegate> to @interface
[mapView addGestureRecognizer:tgr];
[tgr release];
Next, implement shouldRecognizeSimultaneously:WithGestureRecognizer:
and return YES
so your tap gesture recognizer can work at the same time as the map's (otherwise taps on pins won't get handled automatically by the map)
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer
:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
Finally, implement the gesture handler:
- (void)tapGestureHandler:(UITapGestureRecognizer *)tgr
{
CGPoint touchPoint = [tgr locationInView:mapView];
CLLocationCoordinate2D touchMapCoordinate
= [mapView convertPoint:touchPoint toCoordinateFromView:mapView];
NSLog(@"tapGestureHandler: touchMapCoordinate = %f,%f",
touchMapCoordinate.latitude, touchMapCoordinate.longitude);
}
In this way you can get the Latitude and Longitude for location which you touch on the Map and once you get latitude and longitude you can easily drop a pin in to that location.
精彩评论