Google Maps Android API v2 getVisibleRegion()返回0 [英] Google Maps Android API v2 getVisibleRegion() returns 0

查看:188
本文介绍了Google Maps Android API v2 getVisibleRegion()返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用新的Google Maps Android API v2,需要检测屏幕视图角落的纬度坐标.我使用mMap.getProjection().getVisibleRegion()进行此操作,这在我第一次加载程序时效果很好,但是如果我离开程序,然后重新打开它(通过后退"按钮或主页"按钮)getVisibleRegion ()返回均为(0.0,0.0)的latlng坐标.我有一个解决方法,当我第一次打开程序时,我将VisibleRegion对象保存为应用程序类中的全局对象,但这似乎不是一个好主意.有人知道为什么重新打开时latlng坐标变为零(但不为null)吗?谢谢!

I am using the new Google Maps Android API v2 and need to detect latlng coordinates for the corners of my screen view. I use mMap.getProjection().getVisibleRegion() to do this, which works fine the first time I load the program, but if I leave the program and then re-open it (either via the back button or the home button) getVisibleRegion() returns latlng coordinates that are all (0.0, 0.0). I have a workaround where I save the VisibleRegion object as a global in the application class when I first open the program, but this seems like a bad idea. Anyone understand why the latlng coordinates go to zero (but not null) when re-opening? Thanks!

有时初始加载会给出(0.0,0.0)可见区域,因此我的解决方法不可行.使用getProjection().fromScreenLocation(point)也会返回0.

sometimes initial load gives a (0.0, 0.0) Visible Region so my workaround is not viable. Using getProjection().fromScreenLocation(point) also returns 0.

推荐答案

好吧,我想我已经解决了:我猜对了:该地图不可见,因此也没有visibleRegion(换句话说,区域为0 ,0,0,0到0,0,0,0).与此问题类似

Okay, I think I solved it: I was guessing right: The map was not visible and therefore there is also no visibleRegion (in other words a region from 0,0,0,0 to 0,0,0,0). Similar to this question

Android Google Maps API v2从onResume调用getProjection

我在地图上附加了一个onCameraChanged-Listener.每次移动地图(或有可用的摄像头=>都有visibleRegion)时,都会调用此方法.

I attached an onCameraChanged-Listener to my map. Everytime the map is moved (or the camera is available => There is a visibleRegion) this method is called.

private void initMap() {
    mMap = getMap();

    if (mMap != null) {
        try {

            mMap.setLocationSource(this);
            mMap.setOnMarkerClickListener(this);
            mMap.setOnCameraChangeListener(new OnCameraChangedListener() {
                @Override
                public void onCameraChange(CameraPosition cameraPosition) {
                  Log.d(TAG, "onCameraChange");
                  updateMapItems();
                }
            }
            MapsInitializer.initialize(getActivity());

            ...
        }
    }
}

此侦听器确保您具有visibleRegion.

This listener makes sure that you have a visibleRegion.

更新:自从Google Maps API v2的新更新以来,存在回调onMapReady().我尚未使用它,但它似乎完全可以解决此问题: https://developer.android.com/reference/com/google/android/gms/maps/OnMapReadyCallback.html

UPDATE: Since the new update of the Google Maps API v2, there is a callback onMapReady(). I did not use it yet, but it seems to serve exactly the purpose of this question: https://developer.android.com/reference/com/google/android/gms/maps/OnMapReadyCallback.html

这篇关于Google Maps Android API v2 getVisibleRegion()返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆