so I’d like to go from image 1 to image 2 when an annotation is clicked (Mapbox):
https://i.stack.imgur.com/OFIFa.png
It’s fairly easy to have the map center on the annotation, by calling mapView.setCenter() inside one of the Mapbox delegate functions as follows:
func mapView(_ mapView: MGLMapView, didSelect annotation: MGLAnnotation) {
mapView.setCenter(annotation.coordinate, animated: true)
}
Obviously, this centers the annotation to the middle of the screen, but I need the annotation to be ‘centered’ to in the area above the view that pops up so it’s still visible (ideally, it’ll be equidistant from the top of ‘View’ and the top edge of the screen).
I was thinking of setting the zoomLevel in setCenter and then zooming to a specific distance south of the annotation, but the problem with this is various screen sizes on iOS devices will have the annotation centered differently.
I was also thinking maybe I could do some kind of coordinate conversion from the map to a CGPoint on the screen but am really unsure about how to implement this correctly (I’ve only ever used mapView.convert(CGPoint,toCoordinateFrom: mapView), and this won’t be useful here). I’m not sure how to approach this problem. Any help would be much appreciated, whether it’s just getting me started on a path or if you already have a solution that’s even better. Thanks!
2
Answers
You could resize the map frame to that square then center the coordinate. This would mean the view would no longer pop op over the mapview though it would appear to.
this can be done with:
x and y are start coordinates in the superview (the view behind the mapview) coordinate system. Then the width and height parameters creates a rectangle with those dimensions starting from x and y. Be careful with the start point, it’s sometimes in the lowerleft and sometimes in the upper left depending on the situation. Depending on your implementation you might have to modify the popup views frame too.
-if your map is always connected to the top of the screen you could use
I don’t use MapBox but if you can access the region’s span (horizontal and vertical distance of the map)
You can subtract the desired amount of the latitude’s span from the annotation’s coordinate and set that center for the map.
Here is some SwiftUI code.