Skip to content Skip to sidebar Skip to footer

Mapview In Separate Fragment Android

I want to have a mapview like this suppose my main activity is like this Here my main activity holds the instance of mapview in main activity and then when I click on it ,then a

Solution 1:

You need to have the supportMapFragment in your xml file and get the reference of mapFragment into your MainFragment. The below code explains, How I accomplished the MapView inside Fragment. Write the below XML code in the mapView.xml and then Use the Fragment OnCreateView to get references.

<fragment xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mainMap" android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="200dp" />

publicclassMapInFragmentextendsFragmentimplementsOnMapReadyCallback {
private View rootView;
private GoogleMap mMap;

@Nullable@Overridepublic View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.mapView, container, false);





    try{
        SupportMapFragmentmapFragment= (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.mainMap);
        mapFragment.getMapAsync(MapInFragment.this);
    }catch (Exception e){
        e.printStackTrace();
    }

    return rootView;
}


@OverridepublicvoidonMapReady(GoogleMap googleMap) {
    try {
        mMap = googleMap;

        // Add a marker in Sydney and move the cameraLatLngcroplatlng=newLatLng(Double.parseDouble(latitude), Double.parseDouble(longitude));
        mMap.addMarker(newMarkerOptions().position(croplatlng).title(crop + "Field"));

        //mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(croplatlng,16));CameraUpdatecameraUpdate= CameraUpdateFactory.newLatLngZoom(croplatlng , 16);
        //mMap.addMarker(new MarkerOptions().position(location).title(""));//mMap.moveCamera(CameraUpdateFactory.newLatLng(currentLocation ));
        mMap.animateCamera(cameraUpdate,2000,null);
    }catch (Exception e){
        e.printStackTrace();
    }

}


publicvoidonDestroyView()
{
    try {
        Fragmentfragment= (getChildFragmentManager().findFragmentById(R.id.mainMap));
        if (fragment != null) {
            FragmentTransactionft= getActivity().getSupportFragmentManager().beginTransaction();
            ft.remove(fragment);
            ft.commit();
        }
    }catch (Exception e){
        e.printStackTrace();
    }
    super.onDestroyView();
}

}

Solution 2:

Step 1: Add Mapview on your main activity.

Step 2: Add OnClickListener on MapView

mMapView.getMap().setOnMapClickListener(newOnMapClickListener()
                {
                    @OverridepublicvoidonMapClick(LatLng arg0)
                    {
                        //Open New Activity
                    }
                });

Step 3: Open New Activiy to show your map.

Hope you will find this answer usefull. Happy Coading

Post a Comment for "Mapview In Separate Fragment Android"