How to move a MKAnnotation without adding/removing it from the map?

前端 未结 11 1088
情深已故
情深已故 2020-12-13 06:22

Is it possible to move the coordinate of a MKAnnotation without adding and removing the annotation from the map?

相关标签:
11条回答
  • 2020-12-13 07:06

    Most answers are based on fact that your Annotation has been made via MKPlacemark extending. But what about any other object (i.e. custom image for marker) which only implements MKAnnotation protocol? It wouldn't have the setCoordinate method. So here is the answer based on code, provided by 100grams

        MKAnnotationView *av = [self.mapView viewForAnnotation:user];
        //user is an object which implements <MKAnnotation>
        // You will probably get your MKAnnotationView in your own way
        if (av){
            // here user returns CLLocationCoordinate2D coordinate  
            MKMapPoint mapPoint = MKMapPointForCoordinate([user getLastLocation]); 
            // converting mapPoint to CGPoint 
            CGPoint newPos;
            CGFloat zoomFactor =  self.mapView.visibleMapRect.size.width / self.mapView.bounds.size.width;
            newPos.x = mapPoint.x/zoomFactor;
            newPos.y = mapPoint.y/zoomFactor;
            // animation for annotation's move
            static NSString *POSITION_KEY=@"positionAnimation";
        static NSString *PATH_ANIMATION_KEY=@"position";
        if (MKMapRectContainsPoint(self.mapView.visibleMapRect, mapPoint)) {
            CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:PATH_ANIMATION_KEY];
            animation.fromValue = [NSValue valueWithCGPoint:av.center];
            animation.toValue = [NSValue valueWithCGPoint:newPos];  
            animation.duration = 0.3;
            animation.delegate = av;
            animation.fillMode = kCAFillModeForwards;
            [av.layer addAnimation:animation forKey:POSITION_KEY];
        }
            // moving view
            av.center = newPos;
    

    so You basically need to get the corresponding MKAnnotationView which could be also achieved by iteration via all of annotations and it's new location. I made an array holder for all of it (NSMutableArray *mapAnnotaions) so now I could iterate just like:

    for (id annotation in mapAnnotaions) {call refresh method here for id}
    
    0 讨论(0)
  • 2020-12-13 07:06

    Take a look at my MapKitDragAndDrop sample, it allows you to update MKAnnotation and move MKAnnotationView on both iPhone OS 3 and iOS 4 (will use built-in dragging support).

    0 讨论(0)
  • 2020-12-13 07:09

    I found a pretty simple method. I wanted to update my player's position smoothly without removing and then re-adding the player annotation. This works well for pre-iOS4 apps, and what it does is both move the pin and center the map on the pin's new location:

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [player setCoordinate:aLoc.coordinate];
    [mapView setCenterCoordinate:aLoc.coordinate animated:NO];
    [UIView commitAnimations];
    

    Here's the same thing but using the recommended block feature for iOS4:

    [UIView animateWithDuration:0.5 animations:^{
        [player setCoordinate:aLoc.coordinate];
        [mapView setCenterCoordinate:aLoc.coordinate animated:NO];
    }];
    
    0 讨论(0)
  • 2020-12-13 07:11

    If you keep a reference to your annotation, make a change to the coordinate property, then add the annotation to the map again, the location of the annotation view will update. You do not need to remove the annotation (which will cause the annotation view to momentarily disappear). This will not give you a nice transition for when the annotation coordinate is updated however. It also does not result in a memory leak nor are there a multiple of the same annotation added to the map (if you look at the number of annotations on the mapview, it remains constant when you re-add the annotation).

    0 讨论(0)
  • 2020-12-13 07:13

    Subclass MKPointAnnotation and it has the capability to update the annotation view on the map to the new coordinate automatically.

    0 讨论(0)
提交回复
热议问题