在触摸地图上添加mkannotation

时间:2012-02-02 15:23:53

标签: ios mkmapview mkannotationview

当用户点按地图以便他们选择位置时,我想在我的mkmapview中添加一个mkannotation。

我已经读过关于拖放的信息,但是如果你想移动到城市的另一个角落那会有点烦人,因为你必须一步一步地移动销钉。

如何获取用户点击并将我的引脚移动到那里的坐标?

谢谢!

2 个答案:

答案 0 :(得分:9)

使用UITapGestureRecognizer获取点按点的CGPoint和坐标。

  UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addPin:)];
  [recognizer setNumberOfTapsRequired:1];
  [map addGestureRecognizer:recognizer];
  [recognizer release];

然后添加您的目标操作

- (void)addPin:(UITapGestureRecognizer*)recognizer
{
  CGPoint tappedPoint = [recognizer locationInView:map];
  NSLog(@"Tapped At : %@",NSStringFromCGPoint(tappedPoint));
  CLLocationCoordinate2D coord= [map convertPoint:tappedPoint toCoordinateFromView:map];
  NSLog(@"lat  %f",coord.latitude);
  NSLog(@"long %f",coord.longitude);

    // add an annotation with coord
}

答案 1 :(得分:0)

ios< 3.2,您可以使用此代码段:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{
    if ( touch.tapCount == 1 ) {
      UITouch *touch = [[event allTouches] anyObject];  
      if (CGRectContainsPoint([map frame], [touch locationInView:self.view])) 
      {
         CLLocationCoordinate2D coord= 
                 [map convertPoint:tappedPoint toCoordinateFromView:map];
         NSLog(@"lat  %f",coord.latitude);
         NSLog(@"long %f",coord.longitude);

         // add an annotation with coord

         // or (as example before)
         // [self addPin];
      }
   }
}

它类似,但不要使用UIGestureRecognizer

希望这有帮助。