Reputation: 5020
I am using Mapbox in my iOS Project with Swift language. I am currently looking a way to programatically rotate the mapview at a fix altitude with certain coordinates inside. I have tried alot and even have browsed their API Reference as well but could not find any help.
Has anyone tried to do anything like this in which MapView is being continously rotating keeping certain coornidates in bounds using MAPBOX Library.
Help would be really appreciated.
Upvotes: 0
Views: 1121
Reputation: 5020
Thanks to @riastrad for the guidance and I have come up with a bunch of code that can help to acheive this feature.
Sharing with everyone so that they can get help if they require:
Code is for Swift 4.2
//Create a bound using two coordinates.
let coordinateBounds = MGLCoordinateBounds(sw: coordinateOne, ne: coordinateTwo)
//Add Insets for the bounds if needed
let mapEdgeInsets = UIEdgeInsets(top: 10.0, left: 0.0, bottom: 0.0, right: 10.0)
//get the camera that fit those bounds and edge insets
let camera = self.mapView.cameraThatFitsCoordinateBounds(coordinateBounds, edgePadding: mapEdgeInsets)
//Update camera pitch (if required)
camera.pitch = 60
//setup CameraHeading
let zoomLevel = self.mapView.zoomLevel
var cameraHeading = camera.heading
if zoomLevel > 14 {
cameraHeading += 2.2
} else {
cameraHeading += 0.7
}
if cameraHeading > 359 {
cameraHeading = 1
}
camera.heading = cameraHeading
//set new camera with animation
let newCamera = camera
self.mapView.setCamera(newCamera, withDuration: 0.1, animationTimingFunction: CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut))
put the above set of code inside a method and call that method repeatedly every 0.1 seconds.
private func enableRotationTimer(_ enable:Bool) {
guard self.store != nil else { return }
if enable == true {
if mapRotationTimer == nil {
mapRotationTimer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(rotateCamera), userInfo: nil, repeats: true)
}
} else {
mapRotationTimer?.invalidate()
mapRotationTimer = nil
}
}
Hope this helps for others. Thanks
Upvotes: 2
Reputation: 1774
The setCamera
function allows you to pass an animation function. You can see an example of this here: https://docs.mapbox.com/ios/maps/examples/camera-animation/
For limiting the bounds of your map camera, this requires some sleight of hand and a bit of calculating to determine where the camera is in relation to the bounds you’d like to set. You can find an example of this here: https://docs.mapbox.com/ios/maps/examples/constraining-gestures/
Based on how you’ve phrased your question, it sounds like you’ll need to combine these two approaches.
⚠️ disclaimer: I currently work at Mapbox⚠️
Upvotes: 1