Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapview getLatitudeSpan and getLongitudeSpan not working

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
like image 497
Jan S. Avatar asked Apr 19 '10 12:04

Jan S.


3 Answers

I have gone through this exact same problem and the solutions stated above works. However I would like to present a more clear implementation for all the beginners out there.

Define a view variable for your map: private MapView mapView;

And assign the mapview:

mapView = (MapView) findViewById(R.id.mapview);

Add the following method:

    /**
     * Wait for mapview to become ready.
     */
    private Runnable waitForMapTimeTask = new Runnable() {
        public void run() {
            // If either is true we must wait.
            if(mapView.getLatitudeSpan() == 0 || mapView.getLongitudeSpan() == 360000000)
                mapView.postDelayed(this, TIME_TO_WAIT_IN_MS);
        }
    };

In your onCreate/onResume add the following prior to calling getLatitudeSpan:

mapView.postDelayed(waitForMapTimeTask, TIME_TO_WAIT_IN_MS);

And you are good to go :)

like image 111
slott Avatar answered Sep 20 '22 01:09

slott


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.

like image 36
CommonsWare Avatar answered Sep 20 '22 01:09

CommonsWare


I have been messing around with this... and having a mapview in a tabbed activity... the values are not initialized until onResume()... I verified this by putting:

        final Handler mHandler = new Handler();
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                latSpan = mapView.getLatitudeSpan();
                lngSpan = mapView.getLongitudeSpan();
                if ((latSpan == 0) && (lngSpan == 360000000)) {
                    count++;
                    System.out.println("here we go again! " + count);
                    mHandler.postDelayed(this, 100);
                }       
            }
        }, 100);

In Various locations... in onCreate() and onStart() it will go indefinitely (until I switch to that tab), in onResume() it gets correct values immediately.

like image 38
ibash Avatar answered Sep 19 '22 01:09

ibash