【问题标题】:How to Drag and drop the annotation pin in Mkmapview如何在 Mkmapview 中拖放注释引脚
【发布时间】:2016-03-01 12:24:49
【问题描述】:

我有一个注释图钉,用于显示可以拖动的当前搜索位置。该注释图钉正在拖动,然后该图钉显示用户放下图钉。如何这样做。

【问题讨论】:

    标签: ios objective-c


    【解决方案1】:

    要使注释可拖动,请将注释视图的可拖动属性设置为 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?

    如何在IOS上管理MKAnnotationView的拖放?

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

    【讨论】:

    • @vijetha 如果它真的对您有帮助,请点赞这个答案...谢谢
    【解决方案2】:

    iOS 11.x Swift 4.0 答案,基于Ashish Thummar 解决方案。

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    
        if annotation is MKUserLocation {
            return nil
        }
    
        let reuseId = "pin"
        var pav: MKPinAnnotationView? = self.mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
        if pav == nil {
            pav = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            pav?.isDraggable = true
            pav?.canShowCallout = true
        } else {
            pav?.annotation = annotation
        }
    
        return pav
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-15
      • 2012-08-09
      • 2016-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多