Add a 5000 meter circle around User Location via Mapbox and Swift

Viewed 338

i just want to draw a circle of 5000 meter around the current user location.
I achieved to draw a circle with radius of 100 pixels with the following code snippet.

        // MGLShapeSource needs a source - create a MGLPointAnnotation
        let pointAnnotation = MGLPointAnnotation()
        pointAnnotation.coordinate = CLLocationCoordinate2D(latitude: 49, longitude:9)
        
        // Create a MGLShapeSource
        let source = MGLShapeSource(identifier: "circle", shape: pointAnnotation, options: nil)
        style.addSource(source)
        

        // Create Circle Layer and style with Options
        let circleLayer = MGLCircleStyleLayer(identifier: "circle", source: source)
        circleLayer.circleOpacity = NSExpression(forConstantValue: 0)
        circleLayer.circleStrokeColor = NSExpression(forConstantValue: UIColor.systemBlue)
        circleLayer.circleStrokeWidth = NSExpression(forConstantValue: 1)
        
        // Radius of Circle, for now in pixels.
        let meterRadius = 5000 // not used yet
        
        let pixelRadius = 100 // Exchange with formula to convert from meters to Screen Pixels
        circleLayer.circleRadius = NSExpression(forConstantValue: pixelRadius)
        
        // Add Layer to Style
        style.addLayer(circleLayer)

Can someone assist me by creating a formula for calculating the correct Screen Pixel Value for this?

I read something like:

        let pixelRadius = meterRadius / 0.072 / cos(49 * Double.pi / 180)

But that would ignore the Zoomlevel.
Any hints on that?

0 Answers
Related