我有一个使用谷歌地图的应用程序。我将 latLngBounds 保存到变量中,然后捆绑以便稍后检索它。我的问题是我想保存屏幕的当前位置,然后将相机移动到那个 LatLng。但是每次当我单击标记并启动另一个活动并返回时,屏幕都会以单击的标记为中心,但我想摆脱此功能并使屏幕返回到之前的位置。
编辑:问题是我的应用程序没有从包中恢复数据,我不知道为什么。这是我保存和恢复状态的方法,但没有一种方法有效:
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
//requestWindowFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.activity_main);
if (bundle!= null){
lat = bundle.getDouble("lat1",0);
lon = bundle.getDouble("lon1",0);
ne3 = bundle.getDouble("lat2",-200);
ne4 = bundle.getDouble("lon2",-200);
Log.d("tag","bundle is not null");
}
setUpMapIfNeeded();
}
@Override
public void onPause() {
super.onPause();
manager.removeUpdates(this);
}
@Override
protected void onResume() {
super.onResume();
if (bundle!= null){
lat = bundle.getDouble("lat1",0);
lon = bundle.getDouble("lon1",0);
ne3 = bundle.getDouble("lat2",-200);
ne4 = bundle.getDouble("lon2",-200);
Log.d("tag","bundle is not null");
}
setUpMapIfNeeded();
}
@Override
protected void onSaveInstanceState(Bundle bundle) {
Log.e("tag","calling onSave");
if(mMap!=null){
LatLngBounds location3 = mMap.getProjection().getVisibleRegion().latLngBounds;
double lat4 = location3.getCenter().latitude;
double lon4 = location3.getCenter().longitude;
double lat5 = location3.northeast.latitude;
double lon5 = location3.northeast.longitude;
bundle.putDouble("lat1", lat4);
bundle.putDouble("lon1",lon4);
bundle.putDouble("lat2",lat5);
bundle.putDouble("lon2",lon5);
}
super.onSaveInstanceState(bundle);
}
@Override
public void onRestoreInstanceState(Bundle bundle) {
super.onRestoreInstanceState(bundle);
if (bundle!= null){
lat = bundle.getDouble("lat1",0);
lon = bundle.getDouble("lon1",0);
ne3 = bundle.getDouble("lat2",-200);
ne4 = bundle.getDouble("lon2",-200);
Log.d("tag","bundle is not null");
}
setUpMapIfNeeded();
// ... recover more data
}
问题似乎是从另一个活动返回时捆绑包以某种方式为空