How to use google map V2 inside fragment?

后端 未结 9 867
攒了一身酷
攒了一身酷 2020-12-02 13:43

I have a fragment which is a part of Viewpager, and I want to use Google Map V2 inside that fragment. This is what I have tried so far,

In my fragment,



        
9条回答
  •  鱼传尺愫
    2020-12-02 14:08

    This is how I did it

    in layout:

    
    

    in code:

    public class GPS extends FragmentActivity {
    
    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }
    
    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (supportMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            supportMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            // Check if we were successful in obtaining the map.
            if (supportMap != null) {
                MarkerOptions mo = new MarkerOptions().position( new LatLng( latitude, longitude ) );
                supportMap.addMarker( mo );
            }
        }
    }
    }
    

    Took me a lot of fiddling, you need just the right combination of things, make sure your class extends FragmentActivity

提交回复
热议问题