Alright, there are several things here....
Firstly, when are you doing your screenToMap
calculation? It will return 0 when your mapView
hasn't fully rendered yet (even if your mapView already has a frame).
So you definitely cannot do it in our viewDidLoad
or viewWillAppear
, but, currently, also not after layoutSubviews
. You need to calculate it after your map has rendered, this can be achieved using mapRenderer
's onMapRendered
event.
Example available here: https://github.com/CartoDB/mobile-ios-samples/blob/master/AdvancedMap.Objective-C/AdvancedMap/CaptureController.mm
We created an issue concerning this: https://github.com/CartoDB/mobile-sdk/issues/162
Secondly, if you ask for coordinates from CartoMobileSDK's method, coordinates are already returned in our internal coordinate system, meaning that you don't need to do any additional conversion. The correct way to ask for bounds and position would be:
let minPosWGS = map.screen(toMap: minScreenPos)!
let maxPosWGS = map.screen(toMap: maxScreenPos)!
and:
let markerCenter = userMarker!.getBounds().getCenter()
Thirdly, X
increases from left to right on your screen, as well as on your map, however, Y
increases from top to bottom on your screen, but from bottom to top on your map, so you'd have to initialize min and max as such:
let screenWidth = Float(map.frame.size.width)
let screenHeight = Float(map.frame.size.height)
let minScreenPos = NTScreenPos(x: 0.0, y: screenHeight)
let maxScreenPos = NTScreenPos(x: screenWidth, y: 0)
Please note that this calculation also depends on your view's orientation and map rotation. Currently we assume that your rotation is 0 and your view is in portrait mode.
And finally, iOS uses scaled coordinates, but Carto's Mobile SDK expects true coordinates. So you need to multiply your values by scale:
let screenWidth = Float(map.frame.size.width * UIScreen.main.scale)
let screenHeight = Float(map.frame.size.height * UIScreen.main.scale)