如何在 Objective-C 中的地图上拖放和拖动图钉

Posted

技术标签:

【中文标题】如何在 Objective-C 中的地图上拖放和拖动图钉【英文标题】:How can I Drop and Drag a pin on a map in Objective-C 【发布时间】:2015-10-30 01:34:34 【问题描述】:

希望有人能提供帮助,我正在尝试在 mapView 上放置一个 Pin。但我希望引脚能够被拖动和放下,并且此注释与用户位置的注释分开。虽然下面的代码有效,但它会为用户的触摸创建重复的 pin,并且 pin 不能被拖动:

- (void)dropPinFromPress:(UIGestureRecognizer *)recognizer 
    // Add Pin from User's Touch
    if (recognizer.state != UIGestureRecognizerStateBegan) 
        return;
    

    // convert touched position to map coordinate
    CGPoint userTouch = [recognizer locationInView:self.mapView];
    CLLocationCoordinate2D mapPoint = [self.mapView convertPoint:userTouch toCoordinateFromView:self.mapView];

    // Add Pin from user's touch
    Annotation *pin = [[Annotation alloc]initWithLocation:mapPoint];
    [self.mapView addAnnotation:pin];    

【问题讨论】:

你有你的解决方案?? 【参考方案1】:
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation

    MKAnnotationView *a = [ [ MKAnnotationView alloc ] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
    //  a.title = @"test";
    if ( a == nil )
        a = [ [ MKAnnotationView alloc ] initWithAnnotation:annotation reuseIdentifier: @"currentloc" ];

    NSLog(@"%f",a.annotation.coordinate.latitude);
    NSLog(@"%f",a.annotation.coordinate.longitude);

    CLLocation* currentLocationMap = [[[CLLocation alloc] initWithLatitude:a.annotation.coordinate.latitude longitude:a.annotation.coordinate.longitude] autorelease];
    [self coordinatesToDMS:currentLocationMap];


    MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:a reuseIdentifier:@"currentloc"];
    if(a.annotation.coordinate.longitude == mapView.userLocation.coordinate.longitude ||  a.annotation.coordinate.latitude == mapView.userLocation.coordinate.latitude  )
    
        if ([annotation isKindOfClass:MKUserLocation.class])
        
            //user location view is being requested,
            //return nil so it uses the default which is a blue dot...
            return nil;
               
        //a.image = [UIImage imageNamed:@"userlocation.png"];
        //a.pinColor=[UIColor redColor];
    
    else 
    

        // annView.image =[UIImage imageNamed:@"map-pin.png"];
        //PinFirst=FALSE;
        //annView.pinColor = [UIColor redColor];
        // annView.annotation=annotation;
    

    annView.animatesDrop = YES;
    annView.draggable = YES;

    annView.canShowCallout = YES;

    return annView;


- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated

    NSLog(@"map Drag");


- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view didChangeDragState:(MKAnnotationViewDragState)newState
fromOldState:(MKAnnotationViewDragState)oldState;

    NSLog(@"pin Drag");

    if (newState == MKAnnotationViewDragStateEnding)
    
        CLLocationCoordinate2D droppedAt = view.annotation.coordinate;
        NSLog(@"Pin dropped at %f,%f", droppedAt.latitude, droppedAt.longitude);

        CLLocation* draglocation = [[[CLLocation alloc] initWithLatitude:droppedAt.latitude longitude:droppedAt.longitude] autorelease];



    

【讨论】:

以上是关于如何在 Objective-C 中的地图上拖放和拖动图钉的主要内容,如果未能解决你的问题,请参考以下文章

在 FabricJS 画布上拖放和删除对象

JQuery-UI 在 Re-Drag 上拖放和重新拖动克隆

如何在图像上拖放绘制的多边形

如何在listview - WPF MVVM上拖放后更新codeBehind中的ObservableCollection

拖放和调整大小重叠

如何在散点图上拖放