android - 离开应用程序时保持 google 地图片段在位置上放大
android - keep google map fragment zoomed in on location when leaving app
我有一个在后台运行并更新片段中用户位置的定位服务。
关闭应用程序时,该服务会进入前台 startForeground()
并显示正在进行的通知。
当我点击通知时,它打开了应用程序,但我想改变的是地图全部缩小了,我必须等待下一个位置更新才能放大地图。
我的 Fragment
的 onCreateView
:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_home, container, false);
ButterKnife.bind(this, root);
mMapView.onCreate(savedInstanceState);
mMapView.onResume(); // needed to get the map to display immediately
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
mMapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap mMap) {
googleMap = mMap;
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
googleMap.setMyLocationEnabled(true);
startLocationService();
}
});
return root;
}
因此每次创建片段时都会调用此方法 - 例如,场景:
打开应用程序,onCreateView
调用,定位服务启动,放大地图,关闭应用程序(服务仍然 运行 并显示通知),点击通知,然后 onCreateView
再次调用并地图先缩小了。
有没有什么好的方法可以"keep"用户位置并防止地图做整个缩小 -> 等待位置更新 -> 放大?
我希望当用户通过点击通知打开应用时,地图已经在他的位置放大。
我有同样的问题,我通过关注
修复了将近 90%
- 使用融合位置
- 在共享首选项中保存位置
- 当第一次找到位置时从位置服务广播,以便地图的相机立即移动到该位置
- 移除相机放大的动画
使用此代码进行相机移动
CameraUpdate center=
CameraUpdateFactory.newLatLng(new LatLng(40.76793169992044,
-73.98180484771729));
CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);
map.moveCamera(center);
map.animateCamera(zoom);
您可以按照以下步骤操作:
1) 使用 SharedPreference
保存了您的最后位置和缩放级别
2) 从 SharedPreference
获取 OnCreateView 方法上的旧位置和缩放级别
3) 在不设置相机动画的情况下调用 onMapReady() 回调中的第一条语句:
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(oldLocation.getLatitude(), oldLocation.getLongitude()), 12(yourzoomlevel)));
我有一个在后台运行并更新片段中用户位置的定位服务。
关闭应用程序时,该服务会进入前台 startForeground()
并显示正在进行的通知。
当我点击通知时,它打开了应用程序,但我想改变的是地图全部缩小了,我必须等待下一个位置更新才能放大地图。
我的 Fragment
的 onCreateView
:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_home, container, false);
ButterKnife.bind(this, root);
mMapView.onCreate(savedInstanceState);
mMapView.onResume(); // needed to get the map to display immediately
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
mMapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap mMap) {
googleMap = mMap;
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
googleMap.setMyLocationEnabled(true);
startLocationService();
}
});
return root;
}
因此每次创建片段时都会调用此方法 - 例如,场景:
打开应用程序,onCreateView
调用,定位服务启动,放大地图,关闭应用程序(服务仍然 运行 并显示通知),点击通知,然后 onCreateView
再次调用并地图先缩小了。
有没有什么好的方法可以"keep"用户位置并防止地图做整个缩小 -> 等待位置更新 -> 放大?
我希望当用户通过点击通知打开应用时,地图已经在他的位置放大。
我有同样的问题,我通过关注
修复了将近 90%- 使用融合位置
- 在共享首选项中保存位置
- 当第一次找到位置时从位置服务广播,以便地图的相机立即移动到该位置
- 移除相机放大的动画
使用此代码进行相机移动
CameraUpdate center=
CameraUpdateFactory.newLatLng(new LatLng(40.76793169992044,
-73.98180484771729));
CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);
map.moveCamera(center);
map.animateCamera(zoom);
您可以按照以下步骤操作:
1) 使用 SharedPreference
保存了您的最后位置和缩放级别2) 从 SharedPreference
获取 OnCreateView 方法上的旧位置和缩放级别3) 在不设置相机动画的情况下调用 onMapReady() 回调中的第一条语句:
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(oldLocation.getLatitude(), oldLocation.getLongitude()), 12(yourzoomlevel)));