Android Map Version 2 with support library

2

I'm using api v2 from the Android maps.

I have the following statement in my Activity

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

And the following code to retrieve the Map:

 GoogleMap mMap = ((MapFragment) getSupportFragmentManager().findFragmentById(
                R.id.map)).getMap();

This code has the following error:

 Error:(150, 79) java: inconvertible types
  required: com.google.android.gms.maps.MapFragment
  found:    android.support.v4.app.Fragment

How do I use version 2 of android maps when I'm using the Compatibility API?

    
asked by anonymous 19.03.2014 / 21:38

1 answer

2

To use the compatibility api you need to make two changes to your code.

In xml, add the compatibility map API class:

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

And in your activity:

    GoogleMap mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(
                R.id.map)).getMap();

This will fix your error.

You can get more information by visiting the official English documentation

    
19.03.2014 / 21:38