微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

iOS – MKMapView – 可拖动注释

我准备好了注释,但试着弄清楚如何使用我的代码使其可拖动:
-(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];
}

提前致谢!

解决方法

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

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

例如:

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

如果您需要在用户停止拖动并删除注释时进行处理,请参阅:
how to manage drag and drop for MKAnnotationView on IOS?

此外,您的注释对象(实现MKAnnotation的对象)应具有可设置的坐标属性.您正在使用MKPointAnnotation类,它实现了setCoordinate,以便已经处理了部分.

原文地址:https://www.jb51.cc/iOS/331124.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐