user1501127 user1501127 - 6 months ago 11
Android Question

GoogleMap as a Fragment and not as a FragmentActivity

I have been trying for a few days now and i just cant make it to work, Is it possible to extend a

googleMap
from
Fragment
and not
FragmentActivity
?

I use the same example code
android studio
creates as
map fragment
example and just been trying to make it into a
Fragment
and not
FragmentActivity
.

Is it possible?

I'm using target sdk21 and minsdk 21

Answer

use the following code in your activity/fragment where you would like to use Maps

<fragment
   class="com.google.android.gms.maps.MapFragment"
   android:name="com.google.android.gms.maps.MapFragment"
   android:id="@+id/map"
   android:layout_width="match_parent"
   android:layout_height="match_parent" />

Then in your Fragment's onCreateView you can use the following code

private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the
        // map.
        if (mMapFragment == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMapFragment = ((MapFragment) getFragmentManager().findFragmentById(R.id.map));
            // Check if we were successful in obtaining the map.
            if (mMapFragment != null) {
                setUpMap();
            }
        }
    }

setup you map stuff in below mentioned method

private void setUpMap() {

        mMapFragment.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap map) {
             ...
            }
        }
   }

Hope it helps...

Comments