iOS - MKMapView - 可拖动标注

7
我已经准备好注释,但是正在尝试找出如何使用我的代码使其可拖动:
-(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];
}

提前感谢您!

1个回答

16

要使注释可拖动,需要将注释视图的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;
}
如果你需要处理用户停止拖动并放置注释的情况,请参考:如何在 iOS 上管理 MKAnnotationView 的拖放?
另外,你的注释对象(实现了MKAnnotation协议)应该有一个可设置的coordinate属性。你正在使用实现了setCoordinateMKPointAnnotation类,所以这部分已经处理好了。

我正在使用相同的代码,但不知道为什么它不起作用,你们有什么想法吗? - guru

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接