GoogleMap (Android) projection visible region not updated after orientation change?

戏子无情 提交于 2020-05-30 03:09:10

问题


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

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

and

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

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.

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?


回答1:


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.

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)


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


来源:https://stackoverflow.com/questions/24542871/googlemap-android-projection-visible-region-not-updated-after-orientation-chan

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!