Mapbox-gl-native: Animate MGLAnnotation coordinate changes

Created on 13 Mar 2017  路  7Comments  路  Source: mapbox/mapbox-gl-native

Hello there,

Is there a way to animate MGLAnnotation coordinate changes using Core Animation?

Currently, I'm doing it like this without any animations:

func changeCoordinate() {
    myAnnotation.coordinate = model.getCurrentCoordinate()
    mapView.setNeedsLayout()
    mapView.layoutIfNeeded()
}

I would love to do something like this:

func changeCoordinate() {
    UIView.animate(withDuration: 1.5, animations: { 
        self.myAnnotation.coordinate = self.model.getCurrentCoordinate()
        self.mapView.setNeedsLayout()
        self.mapView.layoutIfNeeded()
    })
}
annotations iOS support

Most helpful comment

I went back and forth with MapBox support and got 2 takeaways to share:

  1. Make sure your MapBox iOS SDK is updated. (I was using v3.3.7 when v3.5 was the latest version)
  2. Make sure your annotation classes are subclasses of the MGLPointAnnotation class.

Then this code should work:

var myAnnotation = MGLPointAnnotation() // or a subclass

// ...

func changeCoordinate() {
    UIView.animate(withDuration: 1.5, animations: { 
        // Here Core Animation calls MapBox using an implicit animation.
        self.myAnnotation.coordinate = self.model.getCurrentCoordinate()
    })
}

All 7 comments

Bumping this issue.

I don't need a final solution right now. Somebody please just comment whether animation of annotations has been attempted, and if so, what is the best way to do it at the moment.

Thanks in advance!
Petr

Hi @petrmanek. Currently, an MGLAnnotationView can be implicitly animated using Core Animation. This would allow you to describe an animation that should occur for your view when its annotation coordinate changes like in changeCoordinate() above.

Closing this put please feel free to contact our support team if you have additional questions about how to use our iOS SDK.

I went back and forth with MapBox support and got 2 takeaways to share:

  1. Make sure your MapBox iOS SDK is updated. (I was using v3.3.7 when v3.5 was the latest version)
  2. Make sure your annotation classes are subclasses of the MGLPointAnnotation class.

Then this code should work:

var myAnnotation = MGLPointAnnotation() // or a subclass

// ...

func changeCoordinate() {
    UIView.animate(withDuration: 1.5, animations: { 
        // Here Core Animation calls MapBox using an implicit animation.
        self.myAnnotation.coordinate = self.model.getCurrentCoordinate()
    })
}

Thanks @petrmanek. You may be interested in following this issue about an issue with using UIView animation blocks (such as the one in https://github.com/mapbox/mapbox-gl-native/issues/8378#issuecomment-289450780) when the annotation location change takes the annotation view out of the viewport.

this is good, but I cannot click on annotatiun during core animation :-(

@djdance would you mind opening a new ticket? Thanks!

Was this page helpful?
0 / 5 - 0 ratings