【问题标题】:Implementing MKPinAnnotationView drag feature?实现 MKPinAnnotationView 拖动功能?
【发布时间】:2012-12-10 15:04:33
【问题描述】:

谁能推荐一个很好的教程来为 iPhone MKMapView 中的MKPinAnnotationView 实现拖动功能?

【问题讨论】:

标签: iphone ios mkmapview draggable mkpinannotationview


【解决方案1】:

您可以从this 链接找到 MKPinAnnotationView 拖放功能的源代码演示。

更新:上述网站网址中给出的 Github 项目链接无效。但我从this url 找到了新的项目示例。

【讨论】:

    【解决方案2】:

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

    这通常在 viewForAnnotation 委托方法中完成,因此请确保将 MKMapView 委托设置为 self 并在 .h 文件中符合它。

    例如:

    - (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; // Right here baby!
            pav.canShowCallout = YES;
        }
        else
        {
            pav.annotation = annotation;
        }
    
        return pav;
    }
    

    好的,下面是管理注释拖动操作的代码:

    - (void)mapView:(MKMapView *)mapView 
        annotationView:(MKAnnotationView *)annotationView 
        didChangeDragState:(MKAnnotationViewDragState)newState 
        fromOldState:(MKAnnotationViewDragState)oldState 
    {
        if (newState == MKAnnotationViewDragStateEnding) // you can check out some more states by looking at the docs
        {
            CLLocationCoordinate2D droppedAt = annotationView.annotation.coordinate;
            NSLog(@"dropped at %f,%f", droppedAt.latitude, droppedAt.longitude);
        }
    }
    

    这应该会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-14
      • 1970-01-01
      • 1970-01-01
      • 2011-07-01
      • 2011-12-05
      相关资源
      最近更新 更多