Reputation: 5198
I have a location service which runs in the background and updates the location of the user in the fragment.
When closing the app, the service comes to the foreground with startForeground()
and showing an ongoing notification.
When I click the notification, it opens the app, but what I want to change is that the map is all zoomed out, and I have to wait for the next location updated to zoom in the map.
my Fragment
's 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;
}
So this method is called everytime the fragment is created - for example, the scenario of:
Open app, onCreateView
called, location service starts, zoom in map, closing app (service still running and showing notification), notification click, then onCreateView
called again and map is zoomed out at first.
Is there a good way to "keep" the user location and prevent from the map to do the whole zoomed-out -> wait for location update -> zoom in?
I want that when the user opens the app from notification click, the map is already zoomed in on his location.
Upvotes: 0
Views: 1695
Reputation: 1984
You could follow this steps :
1) Saved your last location and zoom level using SharedPreference
2) Get the old Location and zoom level on your OnCreateView method from SharedPreference
3) Call the first statement in onMapReady() callback without animating camera :
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(oldLocation.getLatitude(), oldLocation.getLongitude()), 12(yourzoomlevel)));
Upvotes: 2
Reputation: 405
I have the same problem and I fixed it almost 90% by following
use this code for camera move
CameraUpdate center=
CameraUpdateFactory.newLatLng(new LatLng(40.76793169992044,
-73.98180484771729));
CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);
map.moveCamera(center);
map.animateCamera(zoom);
Upvotes: 2