MapView latitudeSpan / longtitudeSpan无效

时间:2010-11-22 14:29:37

标签: android android-mapview latitude-longitude postdelayed

我试图获得我的mapview的界限,但我遇到的问题是纬度始终设置为0,而longtitude总是设置为360 * 1E6。根据这个链接,这是因为它只会在地图满载时提供正确的坐标: Mapview getLatitudeSpan and getLongitudeSpan not working

现在,我对解决方案完全感到困惑,我使用我的mainactivity的onCreate(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;
}

如何让它等待mapview加载?我已经查看了postDelayed的API,但我无法让它工作。

请原谅我,如果我是愚蠢的o.o'

1 个答案:

答案 0 :(得分:3)

在android中创建“Timer”的正确方法是使用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);
        }
    }
};