Wednesday, 28 August 2013

Google maps v2 - Adding supported maps fragments

The fragment in Android comes with two flavours: The normal one and the support one. This last one is the one recommended because it is compatible with most of the android devices.

From the basic tutorial of Google Maps, it shows how to use the normal fragment for Google Maps, but the supported one also could be used.

1. The fragment
Use the supported fragment
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

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

</RelativeLayout> 

2. Import the proper library
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;

3. Get the map properly
private GoogleMap googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

Source:
Google developers - Google maps Android API v2

No comments:

Post a Comment