如何在地图视图中拖动注释图钉? [英] How to drag annotation pin in a mapview?

查看:84
本文介绍了如何在地图视图中拖动注释图钉?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在地图视图"中有一个初始放置的图钉.

I have an initial dropped pin in Map View.

我要完成的任务是将该图钉拖动到地图上的任意位置,并从该图钉获取新坐标.怎么做?我应该添加什么?

What I want to accomplish is to drag that pin anywhere within the map and get the new coordinates from that pin. How to do that? What should I add?

我有这种方法可以在初始放置图钉.

I have this method where I do the initial drop pin.

- (void) performSearch
{
    MKLocalSearchRequest *request =
    [[MKLocalSearchRequest alloc] init];
    request.naturalLanguageQuery = _searchString;
    request.region = _mapView.region;

    _matchingItems = [[NSMutableArray alloc] init];

    MKLocalSearch *search =
    [[MKLocalSearch alloc]initWithRequest:request];

    [search startWithCompletionHandler:^(MKLocalSearchResponse
                                         *response, NSError *error) {
        if (response.mapItems.count == 0)
        NSLog(@"No Matches");
        else
        for (MKMapItem *item in response.mapItems)
        {
            [_matchingItems addObject:item];
            annotation = [[MKPointAnnotation alloc]init];
            annotation.coordinate = item.placemark.coordinate;
            annotation.title = item.name;
            [_mapView addAnnotation:annotation];


            MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance (annotation.coordinate, 800, 800);
            [_mapView setRegion:region animated:NO];
        }
    }];

}

推荐答案

首先,遵循 MKMapViewDelegate

//ViewController.m  
@interface ViewController () <MKMapViewDelegate>  
@end  

然后,将地图视图的委托设置为 self

Then, set the delegate of your map view to be self

-(void)viewDidLoad {  
    [super viewDidLoad];  
    _mapView.delegate = self;  
    ...  
    ...  
  }  

然后实现以下两个委托方法.

Then implement the following two delegate methods.

第一个委托方法返回注解对象关联的视图,并设置该视图为可拖动,
第二种方法处理注释视图的拖动状态.

The first delegate method returns the view associated with the annotation object, and setting this view to be draggable,
The second method, handles dragging state of an annotation view.

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation {  
    MKPinAnnotationView *pin = (MKPinAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:@"pin"];  

  if(!pin) {  
      pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reueseIdentifier:@"pin"];  
    } else {  
      pin.annotation = annotation;  
    }  

  pin.draggable = YES;  

  return pin;  
}  

在这里,我们要求一个标识符为@"pin"的 MKPinAnnotationView
如果我们没有收到一回,我们就创建它.
然后,将视图设置为可拖动.

Here we requesting for an MKPinAnnotationView with the identifier of @"pin",
If we doesn't receive one back, we just create it.
Then we set the view to be draggable.

以上内容足以移动并因此更改注释的坐标.
如果您想在设置新坐标时调用某个方法,则可以使用第二个委托方法

The above could be enough for moving, and therefore changing, the annotation's coordinates.
If you would like to call some method when a new coordinate is set, you can do this with the second delegate method-

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)annotationView didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState {  
  {  
      if(newState == MKAnnotationViewDragStateStarting)  {  
        NSLog(@"%f, %f", view.annotation.coordinate.latitude, view.annotation.coordinate.longitude);  
        }  

      if(newState == MKAnnotationViewDragStateEnding) {  
        NSLog(@"%f, %f", view.annotation.coordinate.latitude, view.annotation.coordinate.longitude);  
        // Here you can call whatever you want to happen when to annotation coordinates changes  
        }  
  }  

在这里,您可以确定拖动实际上何时结束,然后可以调用所需的任何方法来处理新坐标.

Here you determine when the dragging has actually ended, and then you can call whatever method you like, that will handle the new coordinates.

请注意,拖动开始和结束时,我还包括了 NSLog 调用.
这只是出于调试目的,因此您将看到实际上已更改了坐标.

Note that I've also included an NSLog call when the dragging is starting and ending.
This is just for debugging purposes, so you'll see that the coordinates are actually being changed.

祝你好运.

这篇关于如何在地图视图中拖动注释图钉?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆