добавить mkannotation на сенсорной карте - PullRequest
3 голосов
/ 02 февраля 2012

Я хочу добавить mkannotation к моему mkmapview, когда пользователь нажимает на карту, чтобы выбрать местоположение.

Я читал о перетаскивании, но это немного раздражает, если вы хотите перейти кв другой угол города, потому что вы должны шаг за шагом перемещать булавку.

Как я могу получить координату, где пользователь нажимает и переместить мою булавку там?

Ответы [ 2 ]

9 голосов
/ 02 февраля 2012

Используйте 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
}
0 голосов
/ 29 февраля 2012

На ios <3.2, </strong> вы можете использовать этот фрагмент:

- (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.

Надеюсь, это поможет.

...