Search code examples
iosannotationsmapboxmapbox-iosmapbox-ios-maps

MGLUserLocationAnnotationView subclass does not take the pitch perspective of the camera


I have styled the user location icon according to these docs:

https://docs.mapbox.com/ios/maps/examples/user-location-annotation/

It works, but although I worked with camera and pitch, it is displayed in two dimensions. How can i make it that it is in the right perspective of the camera and the pitch effect works?

IMG_5069

I added MGLMapCamera with this code:

func mapViewDidFinishLoadingMap(_ mapView: MGLMapView) {
       // Wait for the map to load before initiating the first camera movement.

       mapView.camera = MGLMapCamera(lookingAtCenter: mapView.centerCoordinate, altitude: 200, pitch: 50, heading: 0)
              
}

Isn't there a camera option mode like gps for Android? https://docs.mapbox.com/android/maps/examples/location-component-camera-options/


Solution

  • I found a solution.

    As far as I can see, there is no way to set this with an option. So I solved it manually:

    First, I create an image:

    private func setupLayers() {
    
         if arrow == nil {
               arrow = CALayer()
               let myImage = UIImage(named: "arrow")?.cgImage
               arrow.bounds = CGRect(x: 0, y: 0, width: size, height: size)
               
               arrow.contents = myImage
            
               layer.addSublayer(arrow)
         }
    }
    

    Then I use MGLRadiansFromDegrees to convert from radians to degrees and transform the image with every update:

    override func update() {
           // Convert Optional to Double
           let camerapitch = Double(mapView?.camera.pitch ?? 0)
            
           let pitch: CGFloat = MGLRadiansFromDegrees(camerapitch)
            
           layer.transform = CATransform3DMakeRotation(pitch, 1, 0, 0)
    }
    

    enter image description here