getMapAsync() in Fragment

后端 未结 4 1661
旧时难觅i
旧时难觅i 2020-12-05 05:19

I having trouble implementing Google Map in Fragment.

This is my the part of my fragment class:

public class FragmentStoreFinderMap extends Fragment          


        
4条回答
  •  悲哀的现实
    2020-12-05 06:00

    In your XML layout you should do like this

    
    
    
    
    
    

    inside your fragment implement this

    private MapView mapView;
    private GoogleMap googleMap;
    

    override onCreateView if there is no onCreateView just like the code below

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.your_layout, container, false);
    }
    

    override onViewCreated() inside that onViewCreated() put this

    mapView = (MapView) view.findViewById(R.id.map);
    mapView.onCreate(savedInstanceState);
    mapView.onResume();
    mapView.getMapAsync(this);//when you already implement OnMapReadyCallback in your fragment
    

    when you already implement OnMapReadyCallback in your fragment put this/code like this

    @Override
    public void onMapReady(GoogleMap map) {
        googleMap = map;
    }
    

    hope this helps you.

提交回复
热议问题