方向更改后未更新GoogleMap(Android)投影可见区域吗? [英] GoogleMap (Android) projection visible region not updated after orientation change?

查看:143
本文介绍了方向更改后未更新GoogleMap(Android)投影可见区域吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调用我的SupportMapFragment onCreateView()方法时,我会读取地图的

When my SupportMapFragment onCreateView() method is called, I read the map's

getProjection().getVisibleRegion().latLngBounds.northeast.x

getProjection().getVisibleRegion().latLngBounds.southwest.y

我第一次以纵向模式启动该应用程序时,得到 x = 720 y = 1230 .但是,将方向更改为横向后,我又看到了相同的值.

The first time I launch the app (in portrait mode), I get x=720 and y=1230. However, after changing the orientation to landscape I'm seeing the same values again.

返回纵向模式会产生 x = 1280 y = 670 (这是我希望从横向模式中获得的效果),然后回到横向模式将得出第一个再次编号.

Going back to portrait mode yields x=1280 and y=670 (which is what I'd expect from landscape), and going back to landscape gives the first numbers again.

就像投影始终在后面改变方向一样.

It's like the projection is always one orientation change behind.

有什么想法,怎么解决?

Any idea what's wrong, and how to fix it?

推荐答案

这里的问题是onConfigurationChanged(我假设您要检查x和y值的位置)在之前 GoogleMap可以更新其投影.

The problem here is that onConfigurationChanged (which is where I assume you check the x and y values) gets called before the GoogleMap can update its projection.

我发现的一种方法是触发0%的虚拟缩放,一旦完成onCameraIdle(使用更新的投影),就会调用onCameraIdle

One way I found around this is to trigger a dummy zoom by 0% which will call onCameraIdle once it is finished (with the updated projection)

添加OnCameraIdleListener:

Add OnCameraIdleListener:

// add this to your imports
import com.google.android.gms.maps.GoogleMap.OnCameraIdleListener

// add this to your map-using activity
class MapsActivity implements ..., OnCameraIdleListener { ... }

然后,在您的地图使用活动中:

Then, inside your map-using activity:

@Override
public void onCameraIdle() {
    // ... do something with getProjection() ...
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // dummy zoom to trigger onCameraIdle with *correct* orientation
    mMap.animateCamera( CameraUpdateFactory.zoomBy(0f) );
}

这篇关于方向更改后未更新GoogleMap(Android)投影可见区域吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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