关于mkmapview的mkpinannotation问题

时间:2011-06-24 02:50:45

标签: objective-c cocoa-touch

我在mkmapview中添加了mkpinannotation。

我想将引脚移动到我点击mkmapview的位置。

在任何地方都有例子吗?

帮助appriciated。

1 个答案:

答案 0 :(得分:1)

首先,将手势识别器添加到地图视图中:

UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] 

        initWithTarget:self action:@selector(tapGestureHandler:)];

tgr.delegate = self; 

 //also add 
<UIGestureRecognizerDelegate> to @interface

[mapView addGestureRecognizer:tgr];

[tgr release];

接下来,实施shouldRecognizeSimultaneously:WithGestureRecognizer:并返回YES,以便您的点击手势识别器可以与地图同时工作(否则地图上的点按不会被地图自动处理)

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
    shouldRecognizeSimultaneouslyWithGestureRecognizer
        :(UIGestureRecognizer *)otherGestureRecognizer

{

    return YES;
}

最后,实现手势处理程序:

- (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);

}

通过这种方式,您可以获得在地图上触摸的位置的纬度和经度,一旦获得纬度和经度,您就可以轻松地将销钉放入该位置。