How to put Google Maps V2 on a Fragment using ViewPager How to put Google Maps V2 on a Fragment using ViewPager android android

How to put Google Maps V2 on a Fragment using ViewPager


By using this code we can setup MapView anywhere, inside any ViewPager or Fragment or Activity.

In the latest update of Google for Maps, only MapView is supported for fragments. MapFragment & SupportMapFragment doesn't work. I might be wrong but this is what I saw after trying to implement MapFragment & SupportMapFragment.

Setting up the layout for showing the map in the file location_fragment.xml:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <com.google.android.gms.maps.MapView        android:id="@+id/mapView"        android:layout_width="match_parent"        android:layout_height="match_parent" /></RelativeLayout>

Now, we code the Java class for showing the map in the file MapViewFragment.java:

public class MapViewFragment extends Fragment {    MapView mMapView;    private GoogleMap googleMap;    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {        View rootView = inflater.inflate(R.layout.location_fragment, container, false);        mMapView = (MapView) rootView.findViewById(R.id.mapView);        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;                // For showing a move to my location button                googleMap.setMyLocationEnabled(true);                // For dropping a marker at a point on the Map                LatLng sydney = new LatLng(-34, 151);                googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker Title").snippet("Marker Description"));                // For zooming automatically to the location of the marker                CameraPosition cameraPosition = new CameraPosition.Builder().target(sydney).zoom(12).build();                googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));            }        });        return rootView;    }    @Override    public void onResume() {        super.onResume();        mMapView.onResume();    }    @Override    public void onPause() {        super.onPause();        mMapView.onPause();    }    @Override    public void onDestroy() {        super.onDestroy();        mMapView.onDestroy();    }    @Override    public void onLowMemory() {        super.onLowMemory();        mMapView.onLowMemory();    }}

Finally you need to get the API Key for your app by registering your app at Google Cloud Console. Register your app as Native Android App.


The following approach works for me.

import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import com.google.android.gms.maps.CameraUpdateFactory;import com.google.android.gms.maps.GoogleMap;import com.google.android.gms.maps.MapView;import com.google.android.gms.maps.MapsInitializer;import com.google.android.gms.maps.model.BitmapDescriptorFactory;import com.google.android.gms.maps.model.CameraPosition;import com.google.android.gms.maps.model.LatLng;import com.google.android.gms.maps.model.MarkerOptions;/** * A fragment that launches other parts of the demo application. */public class MapFragment extends Fragment {MapView mMapView;private GoogleMap googleMap;@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,        Bundle savedInstanceState) {    // inflat and return the layout    View v = inflater.inflate(R.layout.fragment_location_info, container,            false);    mMapView = (MapView) v.findViewById(R.id.mapView);    mMapView.onCreate(savedInstanceState);    mMapView.onResume();// needed to get the map to display immediately    try {        MapsInitializer.initialize(getActivity().getApplicationContext());    } catch (Exception e) {        e.printStackTrace();    }    googleMap = mMapView.getMap();    // latitude and longitude    double latitude = 17.385044;    double longitude = 78.486671;    // create marker    MarkerOptions marker = new MarkerOptions().position(            new LatLng(latitude, longitude)).title("Hello Maps");    // Changing marker icon    marker.icon(BitmapDescriptorFactory            .defaultMarker(BitmapDescriptorFactory.HUE_ROSE));    // adding marker    googleMap.addMarker(marker);    CameraPosition cameraPosition = new CameraPosition.Builder()            .target(new LatLng(17.385044, 78.486671)).zoom(12).build();    googleMap.animateCamera(CameraUpdateFactory            .newCameraPosition(cameraPosition));    // Perform any camera updates here    return v;}@Overridepublic void onResume() {    super.onResume();    mMapView.onResume();}@Overridepublic void onPause() {    super.onPause();    mMapView.onPause();}@Overridepublic void onDestroy() {    super.onDestroy();    mMapView.onDestroy();}@Overridepublic void onLowMemory() {    super.onLowMemory();    mMapView.onLowMemory();}}

fragment_location_info.xml

<?xml version="1.0" encoding="utf-8"?><com.google.android.gms.maps.MapViewxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/mapView"android:layout_width="match_parent"android:layout_height="match_parent" />


You can use this line if you want to use GoogleMap in a fragment:

<fragment            android:id="@+id/map"            android:layout_width="wrap_content"            android:layout_height="match_parent"            class="com.google.android.gms.maps.SupportMapFragment" />

GoogleMap mGoogleMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();