在触摸图上添加 mkannotation

发布于 2025-01-01 17:48:16 字数 159 浏览 0 评论 0原文

我想在用户点击地图时向我的 mkmapview 添加 mkannotation,以便他们可以选择位置。

我读过有关拖放的内容,但如果您想移动到城市的另一个角落,这有点烦人,因为您必须一步一步移动图钉。

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

谢谢!

I want to add a mkannotation to my mkmapview when an user taps over the map so they can choose a location.

I've read about the drag&drop but that's a bit annoying if you want to move to the other corner of the city because you have to move step by step the pin.

How can I get the coordinate where a user taps and move my pin there?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那些过往 2025-01-08 17:48:16

使用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
}

Use UITapGestureRecognizer to get the CGPoint and coordinate of tapped point.

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

then add your target action

- (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
}
温折酒 2025-01-08 17:48:16

在 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

希望这有帮助。

On ios < 3.2, you can use this snippet:

- (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];
      }
   }
}

it's similar, but don't use UIGestureRecognizer.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文