Android Drawer Example, how to implement Google Map Fragment

后端 未结 2 1133
夕颜
夕颜 2020-12-20 02:20

Actually I am trying to implement Google Map on Android Drawer example, basically I would like to show a map by selecting the first menu item. All the provided examples are

相关标签:
2条回答
  • 2020-12-20 03:00

    Update your Location Fragment XML as following :

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

    Implement in your fragment the OnMapReadyCallback interface, and write the code for configuring your Map in onMapReady(GoogleMap map) method, for example :

    public void onMapReady(GoogleMap map) {
        googleMap.addMarker(new MarkerOptions()
                 .position(new LatLng(0, 0))
                 .title("Marker"));
    
        googleMap.setMyLocationEnabled(true);
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(17.0f) );
    }
    

    I hope it will be helpful for you !

    0 讨论(0)
  • 2020-12-20 03:18

    If you want to use maps within the fragment class you should use MapView.

    MapView mapView;
    
    GoogleMap googleMap;
    
    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
        {
            View view = inflater.inflate(R.layout.your_layout_name, container, false);
    
            mapView = (MapView) view.findViewById(R.id.map);
    
                mapView.onCreate(savedInstanceState);
    
                if(mapView!=null)
                {
                    googleMap = mapView.getMap();
    
                    googleMap.getUiSettings().setMyLocationButtonEnabled(false);
    
                    googleMap.setMyLocationEnabled(true);
    
                    googleMap.getUiSettings().setZoomControlsEnabled(true);
                }
    
            return view;
    
        }
    
      @Override
        public void onResume() 
        {
            mapView.onResume();
    
            super.onResume();
        }
        @Override
        public void onDestroy() 
        {
            super.onDestroy();
    
            mapView.onDestroy();
        }
        @Override
        public void onLowMemory() 
        {
            super.onLowMemory();
    
            mapView.onLowMemory();
        }
    

    In your xml file paste code below

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

    try this you will see a good result...

    0 讨论(0)
提交回复
热议问题