Search code examples
javaandroidxmlgoogle-maps-api-2android-maps-v2

Why i only got a Blank map with Google Maps Android API V2?


i'm trying to build a app with a Google Maps Api V2, but i only got a blank screen with zoom buttons everytime. I think everything is fine in my code. I have all the permissions necessary, i correctly got the Key. I already delete debug.keystore to eclipse generate another one, but i still got the same blank screen with zoom buttons. Can anyone see if i doing something wrong in my codes ?

My Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.meu"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="18" />

 <permission
    android:name="com.example.meu.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.example.meu.permission.MAPS_RECEIVE" />

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<!-- Required OpenGL ES 2.0. for Maps V2 -->
<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
     <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="****My key here***" />
     <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

My layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

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

</RelativeLayout>

My Java code:

 package com.example.meu;

 import com.google.android.gms.maps.GoogleMap;
 import com.google.android.gms.maps.MapFragment;
 import android.app.Activity;
 import android.app.AlertDialog;
 import android.os.Bundle;
 import android.widget.Toast;

 public class MainActivity extends Activity {

       // Google Map
       private GoogleMap googleMap;

       @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState); 
      try {
          // Loading map
          setContentView(R.layout.activity_main);
          initilizeMap();

      } catch (Exception e) {
           e.printStackTrace();
    }

}

/**
 * function to load map. If map is not created it will create it for you
 * */
private void initilizeMap() {
    if (googleMap == null) {
        googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

        // check if map is created successfully or not
        if (googleMap == null) {
            Toast.makeText(getApplicationContext(),"Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
        }
    }
}

@Override
protected void onResume() {
    super.onResume();
    initilizeMap();
}

I already read a lot of things here, but no one could help me.


Solution

  • Firstly: As your map is a fragment (MAPS API V.2) you should extend your MainActivity as a FragmentActivity

     public class MainActivity extends FragmentActivity {
    

    instead of

     public class MainActivity extends Activity {
    

    Secondly: When casting the fragment as a map use the SupportMapFragment class (for better cmpatability between android versions) and getSupportFragmentManager() to find your map fragment.

    ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
    

    instead of

    ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
    

    Also: as per the comments of the question "asker"(!?): If you find that your app is working in an Emulator but not on an actual device, ensure that you use your Debug.Keystore when building the App.

    Note: Ensure at all times that you are using backwards compatible support libraries across your entire app (If you are targeting multiple versions of android of course but I can't understand why anyone would limit their app to only a couple of versions?) and also that all your keys, credentials, Auths etc.. are properly set up in the Google developers console. https://console.developers.google.com/