【问题标题】:iOS - MKMapView - Draggable AnnotationsiOS - MKMapView - 可拖动注释
【发布时间】:2012-08-09 07:00:37
【问题描述】:

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

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

提前致谢!

【问题讨论】:

    标签: ios xcode annotations mkmapview draggable


    【解决方案1】:

    要使注释可拖动,请将注释视图的 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 的对象)应该有一个可设置的coordinate 属性。您正在使用实现 setCoordinateMKPointAnnotation 类,因此该部分已经处理完毕。

    【讨论】:

    • 非常感谢!我是 iOS 开发新手。
    • 我正在使用相同的,但不知道为什么它不起作用你们有什么想法......
    猜你喜欢
    • 2016-01-16
    • 1970-01-01
    • 2015-06-28
    • 2018-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-08
    相关资源
    最近更新 更多