Select to view content in your preferred language

AGSMapView.locationToScreen returns (nan,nan)

918
3
02-02-2018 03:27 PM
WorthSparks
Regular Contributor

My app displays spatially accurate graphics in a view on top of the mapView. To do so, I perform on-the-fly transformations for the graphics from within viewpointChangedHandler. Sometimes on first load, some of the graphics don't show on the screen where they should. After much debugging, I have discovered that even though the mapView.map has been loaded, calling mapView.locationToScreen() returns a CGPoint where both x and y are NaN, CGPoint(x: nan, y: nan).

The map is already AGSLoadStatusLoaded. But obviously, I'm trying to do this too soon. What trigger or observable on the mapView or mapView.map can I use to know when mapView.locationToScreen is ready to return good screen coordinates?

0 Kudos
3 Replies
WorthSparks
Regular Contributor

In case anyone needs a workaround, this is what worked for me. It is crude, I know. But it works even on an iPhone 5s, which is the slowest device my app will run on.

self.map = AGSMap(item: portalItem)
self.map?.load {_ in
    // Even after the map load has completed, self.map is not ready
    // for any locationToScreen calls yet.
    Timer.scheduledTimer(withTimeInterval: 0.5, repeats: false) {_ in
        // But after waiting half a second, in all my tests, it is ready.
        // The following line triggers sketchView.draw(CGRect), which
        // calls this mapView's locationToScreen several times in pursuit
        // of sketch stroke transformations.
        self.sketchView.setNeedsDisplay()
    }
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I'm not too keen on relying on a Timer for the solution. I'm hoping someone can tell me a better way.

0 Kudos
DiveshGoyal
Esri Regular Contributor

Try observing the AGSMapView#spatialReference property and wait till it is initialized. That should signal that the mapview has been properly setup to work with the provided map.

WorthSparks
Regular Contributor

Thanks, I'll try that and report back here. I'll bet that works.

0 Kudos