xcode iOS - MKMapView - 可拖动注释

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11927692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:13:19  来源:igfitidea点击:

iOS - MKMapView - Draggable Annotations

iosxcodeannotationsmkmapviewdraggable

提问by Kenny L.

I have the annotation ready to go, but trying to figure out on how to make it draggable with my code:

我已经准备好了注释,但我试图弄清楚如何使用我的代码使其可拖动:

-(IBAction) updateLocation:(id)sender{

    MKCoordinateRegion newRegion;

    newRegion.center.latitude = mapView.userLocation.location.coordinate.latitude;
    newRegion.center.longitude = mapView.userLocation.location.coordinate.longitude;

    newRegion.span.latitudeDelta = 0.0004f;
    newRegion.span.longitudeDelta = 0.0004f;

    [mapView setRegion: newRegion animated: YES];


    CLLocationCoordinate2D coordinate;
    coordinate.latitude = mapView.userLocation.location.coordinate.latitude;
    coordinate.longitude = mapView.userLocation.location.coordinate.longitude;

    MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];

    [annotation setCoordinate: coordinate];
    [annotation setTitle: @"Your Car is parked here"];
    [annotation setSubtitle: @"Come here for pepsi"];


    [mapView addAnnotation: annotation];
    [mapView setZoomEnabled: YES];
    [mapView setScrollEnabled: YES];
}

Thanks in advance!

提前致谢!

回答by

To make an annotation draggable, set the annotation view'sdraggableproperty to YES.

要使注释可拖动,请将注释视图的draggable属性设置为YES

This is normally done in the viewForAnnotationdelegate method.

这通常在viewForAnnotation委托方法中完成。

For example:

例如:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    static NSString *reuseId = @"pin";
    MKPinAnnotationView *pav = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
    if (pav == nil)
    {
        pav = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];
        pav.draggable = YES;
        pav.canShowCallout = YES;
    }
    else
    {
        pav.annotation = annotation;
    }

    return pav;
}


If you need to handle when the user stops dragging and drops the annotation, see:
how to manage drag and drop for MKAnnotationView on IOS?


如果需要在用户停止拖放注解时进行处理,请参阅:
如何在IOS上管理MKAnnotationView的拖放?


In addition, your annotation object (the one that implements MKAnnotation) should have a settable coordinateproperty. You are using the MKPointAnnotationclass which does implement setCoordinateso that part's already taken care of.


此外,您的注释对象(实现 的对象MKAnnotation)应该有一个可设置的coordinate属性。您正在使用MKPointAnnotation确实实现的类,setCoordinate因此该部分已经得到处理。