android MapView in Fragment

后端 未结 5 1351
小蘑菇
小蘑菇 2020-11-30 02:49

I want to have MapView inside my Fragment

This is my FragmentLayout xml file



        
5条回答
  •  春和景丽
    2020-11-30 03:19

    From Josh Holtz's example on GitHub:

    You should add MapView in your Layout like

     
    

    and implement your Fragment like

    public class SomeFragment extends Fragment {
    
    MapView mapView;
    GoogleMap map;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)          {
        View v = inflater.inflate(R.layout.some_layout, container, false);
    
        // Gets the MapView from the XML layout and creates it
        mapView = (MapView) v.findViewById(R.id.mapview);
        mapView.onCreate(savedInstanceState);
    
        // Gets to GoogleMap from the MapView and does initialization stuff
        map = mapView.getMap();
        map.getUiSettings().setMyLocationButtonEnabled(false);
        map.setMyLocationEnabled(true);
    
        // Needs to call MapsInitializer before doing any CameraUpdateFactory calls
        try {
            MapsInitializer.initialize(this.getActivity());
        } catch (GooglePlayServicesNotAvailableException e) {
            e.printStackTrace();
        }
    
        // Updates the location and zoom of the MapView
        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
        map.animateCamera(cameraUpdate);
    
        return v;
    }
    
    @Override
    public void onResume() {
        mapView.onResume();
        super.onResume();
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
     }
    
     @Override
     public void onLowMemory() {
        super.onLowMemory();
        mapView.onLowMemory();
      }
    
    }
    

提交回复
热议问题