2015-10-30 17 views
0

希望有人可以提供幫助,我正試圖在mapView上實施拖放一個Pin。但是我希望該引腳能夠被拖動和放下,並且此註釋與用戶位置的註釋分開。雖然下面的代碼的作品,它的用戶觸摸創建重複的引腳,引腳不能上拖動:如何在地圖上拖放和拖動一個別針Objective-C

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

你有你的解決方案? –

回答

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



    } 
}