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

前端 未结 11 1087
情深已故
情深已故 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 06:46

    Don't know whether it's more generally possible, but it is if you have your own custom MKAnnotationView.

    I was able to adapt the approach documented at http://spitzkoff.com/craig/?p=108 by Craig Spitzkoff:

    1. Give your custom MKAnnotationView a reference to your MKAnnotation object
    2. Add an updateCoordinates method to the MKAnnotation object and call it when you want to change location of the annotation
    3. Call regionChanged on the custom MKAnnotationView to let it know when to reposition itself (e.g. when MKAnnotation has updated coordinates)
    4. In drawRect on the internal view object owned by your custom MKAnnotationView you can reposition using the coordinates of the MKAnnotation (you're holding a reference to it).

    You could likely simplify this approach further - you may not require an internal view object in all circumstances.

    0 讨论(0)
  • 2020-12-13 06:47

    After you update the coordinates or some other properties of the annotation, just call following MKMapViewDelegate method to redraw your annotation:

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation;
    

    e.g.:

    myAnnotation.coordinate = CLLocationCoordinate2DMake([newLat doubleValue], [newLon doubleValue]);
    [self mapView:self.mapView viewForAnnotation:myAnnotation];
    
    0 讨论(0)
  • 2020-12-13 06:51

    It is indeed possible without removing and adding the annotation off-course.

    You'll need to observe your MKAnnotation's coordinate and update the MKAnnotationView's position when it changes. Alternatively you can (and this is probably the more elegant way of doing it) derive your own MKAnnotation and MKAnnotationView and make them "movable".

    See Moving-MKAnnotationView for a detailed example which also animates the position of the annotation.

    0 讨论(0)
  • 2020-12-13 06:56

    It doesn't seem possible to change the coordinate of a MKAnnotation object and then inform the MKMapView of the change.

    However, removing the previous annotation from the map, changing the coordinate of your annotation object and adding it back to the map works well.

    [theMapView removeAnnotation:myAnnotation]; 
    [myAnnotation setLatitude:newLatitude];
    [theMapView addAnnotation:myAnnotation];
    

    Why don't you want to do it this way?

    0 讨论(0)
  • 2020-12-13 06:59

    Just use KVO:

    [annotation willChangeValueForKey:@"coordinate"];
    [annotation didChangeValueForKey:@"coordinate"];
    

    If your MKAnnotation has an setCoordinate method, just include these lines right there:

    - (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate {
        [self willChangeValueForKey:@"coordinate"];
        coordinate = newCoordinate;
        [self didChangeValueForKey:@"coordinate"];
    }
    
    0 讨论(0)
  • 2020-12-13 07:05

    In Swift 4.0 and IOS 11.0, I just add dynamic attribute to the coordinate property of child class of MKAnnotation class and it works: All Annotations on MapView update their location if coordinate value of MKAnnotation objects are updated:

    class CarAnnotation: NSObject, MKAnnotation {
        @objc dynamic var coordinate: CLLocationCoordinate2D
        //Add your custom code here
    }
    
    0 讨论(0)
提交回复
热议问题