Friday, December 21, 2012

Using SupportMapFragment

The post "A simple example using Google Maps Android API v2" using com.google.android.gms.maps.MapFragment. It work on device run API 12 and above. For older API level, using com.google.android.gms.maps.SupportMapFragment.

SupportMapFragment on Nexus One running Android 2.3.6


Modify layout to replace com.google.android.gms.maps.MapFragment with com.google.android.gms.maps.SupportMapFragment.
<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" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />
    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment"/>

</RelativeLayout>


Modify main code to extend android.support.v4.app.FragmentActivity, rather than Activity. Otherwise, AndroidRuntime will be Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader.
package com.example.androidmapsv2;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
 }

}



The series:
A simple example using Google Maps Android API v2, step by step.

18 comments:

Anonymous said...

It doesnt work, still get the ClassNotFOundException supportmapfragment..

Erik said...

Please read Add reference library google-play-services.jar to project using MapFragment and SupportMapFragment.

cioro said...

can you be a little more specific that that? I added the google play service lib in my workplace, added it to my project but i still get that error

Erik said...

Can you specify what error?

cioro said...

the classnotfoundexception concerning the MapFragment. Even though i added the reference to google play services

cioro said...

also, some old posts said you cant try the google maps v2 on emultor, is it true?

Erik said...

hello cioro,

I have no any idea at this moment.

I have try Using MapFragment instead of SupportMapFragment, I have no your problem.

And also, I have posted your problem in the article, hope somebody can help.

cioro said...

thanks, I will take a look at your code. Also, shouldn't I be takeing into account these settings? http://i345.photobucket.com/albums/p379/raziel_by1/gplayser_zps567cc28b.jpg

Erik said...

Your setting is same as me, "Is Library" checked and my Target is Android 2.2.

Erik said...

hello cioro,

oic...! I overlook your question about emulator; gmail group all comments together.

Yes, Google Play services is not supported on the Android emulator.

cioro said...

Thats quite interesting. Thank you for your help, but i have one small question left for you: how can you constantly get your position on google maps? Constantly, as for example, each time you move 10m or 2 minutes have passed. I know how you get them once, but how always, thats bugging me. Also, this is a more "clean" version from google in order to get the GoogleMap myMap=((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

Erik said...

hello cioro,

For updating my location, I know how to do it using old solution for MapView (V1). For V2, I'm still studying. Google docs seem not too much on it.

Yes, your version is more clean. In the post Get GoogleMap from MapFragment/SupportMapFragment I get it step-by-step for more easy understanding.

cioro said...

Well thanks for your support. If i figure it out i will let you know. Great job with the tutorials, hope to see more

Erik said...

hello cioro,

Please read it Tips to add Support Library.

Anonymous said...

maps not showing in mu emulator buton on phone shows a black maps page with just gray tiles and map controls. please whats the issue?

Erik said...

Google Play services, include Google Maps Android API v2, is not supported on the Android emulator

Alex said...

Why always shows only a part of map...in this case only Africa. In my Android device 2.3.4.
Thanks

Unknown said...

it says process com.palak.maps has stoped unexpectedly when opening app,i am running on android 2.3.5,followed all the steps couple of time but no help :( please help following is my code,

***********************manifest****************************************


























*********************************************Main.Java*************************************
package com.palak.maps;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class Main extends FragmentActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

}
******************************************main.xml**********************************************






***********************************************************************************
thanks in advance.