Converting GPS coordinates to Metal Layer coordinates for Mapbox Maps

70 views Asked by At

I am trying to draw a triangle connecting Berlin, Kyiv & Helsinki(or any such location) on a CustomLayer with Mapbox iOS SDK using Metal. The triangle should move with the map and these cities when the map is zoomed or dragged.

Coordinates for these locations are Longitude and Latitude. While the drawing anything in Metal requires coordinates where center is middle of the screen, from BottomLeft(-1, -1) to TopRight(1, 1). My drawing requires the conversion of Long & Lat to Metal GL drawing coordinates system. Something like -

let berlin: simd_float2 = MapboxUtil.MercatorCoordinate(fromLongitude: 13.403,
                                                             latitude: 52.562)

I think I can do some maths and convert the 360 deg Long & Lat to a 2.0 unit scale that the Metal's canvas can understand. But this needs to take into account the zoom level, part of the map visible on the screen etc. Something like what @nauti has done in this answer. And this calculation will be done everytime the map is rendered(60 times a second) and the same calculation will happen when the map is moved or the zoom level is changed. Seems like a lot of calculations. Ideally there should be a way in the SDK do that or atleast some utility method.

Long story short, am I approaching the problem in a wrong way? Has anyone done something similar? How do I convert the coordinates from one system to another? What part goes inside the shader.

Of course, I will draw a little more complex object in my final application. The triangle connecting Cities is just for my understanding.

I am trying to use Mapbox iOS SDK on iOS with Swift.

1

There are 1 answers

0
Benzy Neez On

Maybe you can use the coordinates that you get from an MKMapPoint:

import MapKit

let berlin = CLLocationCoordinate2D(latitude: 13.403, longitude: 52.562)
let p = MKMapPoint(berlin)
print("(x,y)=(\(p.x), \(p.y))")

(x,y)=(173410795.8840889, 124131311.78095224)

So to get from these coordinates to a coordinate in a map going from BottomLeft(-1, -1) to TopRight(1, 1), I guess you would just need to know the x,y coordinates of two opposite corners of the map (assuming the map is projected linearly).