How to get Google Maps object inside a Fragment

前端 未结 7 1908
走了就别回头了
走了就别回头了 2021-02-19 01:26

I am using fragments in my application and I am new for this. There is a fragment in which I want to display Google Map and want to get its object, for this I have a fragment in

7条回答
  •  梦谈多话
    2021-02-19 02:02

    public class MapActivity extends FragmentActivity
          implements OnMapReadyCallback { }
    Then, while initializing the map, use getMapAsync() instead of getMap():
    
    
    //call this method in your onCreateMethod
     private void initializeMap() {
      if (mMap == null) {
        SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_map);
        mapFrag.getMapAsync(this);
      }
    }
    
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
    
    }
    

    You can use this to making map but it will return map object is null because you don't find solution with getMapAsync to create a new object mMap. It only background task with return null

提交回复
热议问题