Loading Android Google Map fragment managed by ViewPager
Accepted answer is good start point but if we will try to use it it will fail, it not describe full solution. Full working fragment with map which can be used in ViewPager
and Tabs
( notice using map in fragmeny lifecycle ):
public class AttractionMapTabFragment extends AttractionTab implements OnMapReadyCallback {
private ScrollMapView gMapView;
private GoogleMap gMap;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_map, null);
gMapView = (ScrollMapView) view.findViewById(R.id.map);
gMapView.getMapAsync(this);
//most important use onCreate;
gMapView.onCreate(getArguments());
return view;
}
@Override
public void onMapReady(GoogleMap googleMap) {
gMap = googleMap;
gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new
LatLng(51.3623329,21.7719342), 8));
}
@Override
public void onResume() {
super.onResume();
if (gMapView != null)
gMapView.onResume();
}
@Override
public void onDestroy() {
super.onDestroy();
if (gMapView != null)
gMapView.onDestroy();
}
@Override
public void onStart() {
super.onStart();
if (gMapView != null)
gMapView.onStart();
}
@Override
public void onStop() {
super.onStop();
if (gMapView != null)
gMapView.onStop();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (gMapView != null)
gMapView.onSaveInstanceState(outState);
}
}
Custom MapView
class - it is needed if we use ScrollView
as parent, it give possibility to move map.If You are not using ScrollView
in this screen then standard MapView
can be used.
public class ScrollMapView extends MapView {
public ScrollMapView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
/**
* Request all parents to relinquish the touch events
*/
getParent().requestDisallowInterceptTouchEvent(true);
return super.dispatchTouchEvent(ev);
}
}
Last thing is layout - R.layout.fragment_map
:
<?xml version="1.0" encoding="utf-8"?>
<your.app.ScrollMapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" />
In your sole_map.xml
you must add a MapView, let's say something like this:
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.gms.maps.MapView
android:id="@+id/soleViewMap"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Then get it from your SoleMap fragment with the id view.findViewById(R.id.soleViewMap);
This will return you a MapView, then you can do what you're doing now. It should look something like this:
public class SoleMap extends Fragment implements OnMapReadyCallback {
MapView gMapView;
GoogleMap gMap = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.sole_map, container, false);
gMapView = (MapView) view.findViewById(R.id.soleViewMap);
gMapView.getMapAsync(this);
return view;
}
@Override
public void onMapReady(GoogleMap map) {
gMap = map;
gMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new
LatLng(49.39,-124.83), 20));
}
}
Don't forget to associate your mapview with the fragment lifecycle calling the events onCreate
, onLowMemory
, onPause
, onResume
and onDestroy
(hope not missing any of them), and call to MapsInitializer.initialize(getContext());
as well