Android map Version 2 with supporte library

Asked

Viewed 135 times

2

I’m using the Android maps v2 api.

I have in my Activity the following statement

    <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 shows 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 the android maps when using the compatibility API?

1 answer

2

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

In xml, add the compatibility api map 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 on your Activity:

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

This will correct your mistake.

You can get more information by accessing the official documentation in English

Browser other questions tagged

You are not signed in. Login or sign up in order to post.