views:

307

answers:

2

Hi! Sometimes when trying to get the Latitude span or Longitude span of a mapview with getLatitudeSpan() and getLongitudeSpan() I get 0 and 360*1E6 respectively. This doesn't happen always but it is a problem, has anybody got this problem? Any workarounds? I tried using getProjection() too but I get the same problem. Here is the piece of code:

MapView mapView = (MapView) findViewById(R.id.mapview);
int lat = mapView.getLatitudeSpan(); // sometimes returns 0
int long = mapView.getLongitudeSpan(); // sometimes returns 360*1E6
+1  A: 

I would detect the condition and schedule your work to try again after a few hundred milliseconds. AFAIK, those values will eventually become available, but they may be rather timing-dependent. In theory, they should be calculated after you have set the center and set the zoom, but I suspect that they really aren't being calculated until after the MapView is rendered, and possibly not until some map data from the Internet is downloaded.

So, isolate your code that depends on those spans into a method. Call that method from onCreate() and the AsyncTask, probably as you're doing today. But, add detection logic to find the invalid values, then call postDelayed() on your MapView (or some other View) to invoke your method again after a few hundred milliseconds if the values are invalid. You may also need to add some sort of counter so you don't do this postDelayed() stuff indefinitely.

This, of course, is a hack. But, with the Google Maps component not being open source, it is difficult to come up with something more solid.

CommonsWare
A: 

getLatitudeSpan and getLongitudeSpan only works to give you the spans for the visible portion of the map. So if you call them in onCreate() the map will not always have been displayed yet. Call them in onStart() instead and you should be fine.

TheContstruct