Question

I am trying to get the bounds of my mapview but I had the problem that the latitude is always set to 0 and longtitude is always set to 360*1E6. According to this link, that is because it will only offer the right coordinates when the map is fully loaded: Mapview getLatitudeSpan and getLongitudeSpan not working

Now, I am totally confused about the solution, I made this method which I call from the onCreate of my mainactivity (the mapview):

public int[][] getBounds()
{
 GeoPoint center = this.mapView.getMapCenter();
 int latitudeSpan = this.mapView.getLatitudeSpan();
 int longtitudeSpan = this.mapView.getLongitudeSpan();
 int[][] bounds = new int[2][2];

 bounds[0][0] = center.getLatitudeE6() + (latitudeSpan/2);
 bounds[0][1] = center.getLongitudeE6() + (longtitudeSpan/2);

 bounds[1][0] = center.getLatitudeE6() - (latitudeSpan/2);
 bounds[1][1] = center.getLongitudeE6() - (longtitudeSpan/2);
 return bounds;
}

How do I make this wait for the mapview to load? I've looked in the API for postDelayed, but I cannot get it to work.

Forgive me if I am being stupid o.o'

Was it helpful?

Solution

The correct way to create a "Timer" in android is to utilize android.os.Handler:

private Handler updateHandler = new Handler(); 

updateHandler.postDelayed(waitForMapTimeTask, TIME_TO_WAIT_IN_MS);

private Runnable waitForMapTimeTask = new Runnable() {
    public void run() {
        getBounds();
        // Read the bounds to see if something reasonable is returned
        if (!mapIsLoaded) {
            updateHandler.postDelayed(waitForMapTimeTask, TIME_TO_WAIT_IN_MS);
        }
    }
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top