iOS - MKMapView - 可拖动注释 [英] iOS - MKMapView - Draggable Annotations

查看:28
本文介绍了iOS - MKMapView - 可拖动注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

提前致谢!

推荐答案

要使注解可拖动,请将注解view的 draggable 属性设置为 YES.

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

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

This is normally done in the viewForAnnotation delegate method.

例如:

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


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


此外,您的注释对象(实现 MKAnnotation 的对象)应该具有可设置的 coordinate 属性.您正在使用 MKPointAnnotation 类,它确实实现了 setCoordinate,因此该部分已经处理完毕.


In addition, your annotation object (the one that implements MKAnnotation) should have a settable coordinate property. You are using the MKPointAnnotation class which does implement setCoordinate so that part's already taken care of.

这篇关于iOS - MKMapView - 可拖动注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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