Issue
I'm using a GoogleMap/MapView in a layout, but as a View not a Fragment (because the parent needs to be a Fragment), so the fragment's layout includes this:
<com.google.android.gms.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
The Fragment contains this:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
<......>
initMap(bundle, mapView);
return rootView;
}
@Override
public void onResume() {
super.onResume();
MyApp.bus.register(this);
updateMapLocation(MyApp.getMostRecentLocation());
}
@Override
public void onPause() {
super.onPause();
MyApp.bus.unregister(this);
}
@Subscribe
public void locationReceived(LocationReceived m) {
Timber.i("Received bus message - Location!");
updateMapLocation(MyApp.getMostRecentLocation());
}
Its parent Fragment contains this:
private MapView mapView;
private GoogleMap map;
@Override
public void onResume() {
super.onResume();
if (mapView!=null) {
mapView.onResume();
map = this.mapView.getMap();
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (mapView!=null) mapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
if (mapView!=null) mapView.onLowMemory();
}
protected void initMap(Bundle bundle, MapView mapView) {
this.mapView = mapView;
this.mapView.onCreate(bundle);
map = this.mapView.getMap();
map.getUiSettings().setMyLocationButtonEnabled(false);
map.setMyLocationEnabled(true);
map.setBuildingsEnabled(true);
map.getUiSettings().setZoomControlsEnabled(false);
map.getUiSettings().setMyLocationButtonEnabled(true);
try {
MapsInitializer.initialize(this.getActivity());
} catch (Exception e) {
Timber.e(e, "Error initialising Google Map");
}
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(MyApp.getCentralUKLatLng(), getResources().getInteger(R.integer.map_zoom_initial));
map.animateCamera(cameraUpdate);
}
@Override
public void onPause() {
super.onPause();
if (mapView!=null) mapView.onPause();
}
protected void updateMapLocation(Location location) {
Timber.i("Moving map to a new location: " + location);
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 15);
map.animateCamera(cameraUpdate);
}
New locations are being delivered via an Otto bus. The code above works perfectly, but ONLY the first time. If I open another fragment in front of this one, and then close it again, the map fails to animate to subsequently provided locations. The locations are definitely received, the animateCamera() method is definitely called (with a valid location and zoom) but absolutely nothing happens. No error, no log message, nothing. What makes it even more infuriating is on one Fragment (which is identical to the code above) it works fine when resuming the Fragment.
I assume I am doing something wrong with how the GoogeMap or the MapView is being (re)initialised on resume, but I'm passing through the onPause() and onResume() calls to the MapView which I understand is necessary. What else do I need to do?
Solution
This is because you have not write the code in onResume(). See when you open and close the second fragment the first fragment will always stay there so when the second fragment gets closed the onResume() of first will get called. So you have to animate the camera in onResume().
Answered By - Hardik Chauhan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.