MapView latitudeSpan / longtitudeSpan 不起作用

发布于 2024-10-04 03:17:27 字数 878 浏览 13 评论 0原文

我试图获取地图视图的边界,但遇到的问题是纬度始终设置为 0,经度始终设置为 360*1E6。根据此链接,这是因为它只会在地图完全加载时提供正确的坐标: Mapview getLatitudeSpan 和 getLongitudeSpan 不起作用

现在,我对解决方案完全感到困惑,我创建了从主活动(地图视图)的 onCreate 中调用的方法:

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;
}

如何使其等待地图视图加载?我查看了 postDelayed 的 API,但无法让它工作。

如果我很蠢请原谅我

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'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦年海沫深 2024-10-11 03:17:28

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

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);
        }
    }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文