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

前端 未结 11 1096
情深已故
情深已故 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.

提交回复
热议问题