Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
223 views
in Technique[技术] by (71.8m points)

android - 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...