1

I'm trying to display Google Map's map.

I followed this tutorial.

I Fixed some problems and the application runs, but I could not display the map.

Here what I did:

  • Created an API project on the google console
  • Created an ID
  • Activated "Google Maps Android API v2" service
  • Generated new Android key
  • Imported this key in manifest
  • Followed this video to install the library

And here my .java:

public class MainActivity extends FragmentActivity {

private static GoogleMap mMap = null;

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

    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());

    if(status == ConnectionResult.SUCCESS){
       if (mMap == null) {

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

          if (mMap != null) {
            UiSettings settings = mMap.getUiSettings();

            settings.setZoomControlsEnabled(true);
            settings.setCompassEnabled(true);
            settings.setRotateGesturesEnabled(true);
            settings.setTiltGesturesEnabled(true);
            settings.setScrollGesturesEnabled(true);
            settings.setZoomControlsEnabled(true);
            settings.setZoomGesturesEnabled(true);
            settings.setMyLocationButtonEnabled(false);

            mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            mMap.setMyLocationEnabled(false);
            }
          }
       }
    }
    protected void onResume() {
        super.onResume();

        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());

        if(status == ConnectionResult.SUCCESS){
           if (mMap == null) {

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

              if (mMap != null) {
                UiSettings settings = mMap.getUiSettings();

                settings.setZoomControlsEnabled(true);
                settings.setCompassEnabled(true);
                settings.setRotateGesturesEnabled(true);
                settings.setTiltGesturesEnabled(true);
                settings.setScrollGesturesEnabled(true);
                settings.setZoomControlsEnabled(true);
                settings.setZoomGesturesEnabled(true);
                settings.setMyLocationButtonEnabled(false);

                mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                mMap.setMyLocationEnabled(false);
              }
          }
     }
 }

my activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <fragment
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          class="com.google.android.gms.maps.SupportMapFragment" />


</LinearLayout>

And my manifest:

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

    <permission android:name="com.example.map.permission.MAPS_RECEIVE" android:protectionLevel="signature"/>
    <uses-permission android:name="com.example.map.permission.MAPS_RECEIVE"/>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>

    <uses-sdk
        android:minSdkVersion="12"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <uses-library android:name="com.google.android.maps" />
        <activity
            android:name="com.example.map.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>
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="<MyKey>"/>

    </application>

</manifest>
Servy
  • 202,030
  • 26
  • 332
  • 449
nsvir
  • 8,781
  • 10
  • 32
  • 47

2 Answers2

1

Inside your application tag (in manifest) add:

<uses-library android:name="com.google.android.maps" />

Replace your fragment XML by this:

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

Let your activity extend FragmentActivity and onCreate and onResume call this code:

int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());

if(status == ConnectionResult.SUCCESS){
   if (mMap == null) {

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

      if (mMap != null) {
        UiSettings settings = mMap.getUiSettings();

        settings.setZoomControlsEnabled(true);
        settings.setCompassEnabled(true);
        settings.setRotateGesturesEnabled(true);
        settings.setTiltGesturesEnabled(true);
        settings.setScrollGesturesEnabled(true);
        settings.setZoomControlsEnabled(true);
        settings.setZoomGesturesEnabled(true);
        settings.setMyLocationButtonEnabled(false);

        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        mMap.setMyLocationEnabled(false);
      }
    }
}

Don't forget to declare your mMap variable like this:

private GoogleMap mMap;
red_alert
  • 1,738
  • 14
  • 24
  • The fragment makes the application crash :/ – nsvir Jul 09 '13 at 14:08
  • Have you tried with my last edited code? The code I've posted works for me from Android 2.2.1 – red_alert Jul 09 '13 at 14:10
  • I tried the edited code and extend to FragmentActivity and it crashs. I tried whith extend Activity and it doesn't crash but it doesn't work. – nsvir Jul 09 '13 at 14:20
  • And the xml class makes crash too – nsvir Jul 09 '13 at 14:36
  • Btw i could not use SupportMapFragment cast with GoogleMap I only could use MapFragment cast Your mMap is a privation GoogleMap variable isn't it ? – nsvir Jul 09 '13 at 14:41
  • My variable mMap is declared like this: private GoogleMap mMap; I've forgoted to say that and maybe it's because of that it crashes... – red_alert Jul 09 '13 at 14:43
  • @nsvir take a look at this http://developer.android.com/about/dashboards/index.html. make your min sdk 12. no point in using 11. most of them have upgraded to 12 and above. – Raghunandan Jul 09 '13 at 14:48
  • Raghunandan: thanks. red_alert: Why SupportMapFragment cast show syntaxe error with GoogleMap variable ? Could it be the lib ? – nsvir Jul 09 '13 at 14:54
  • `Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment` this is the cause. check your imports. also make sure yo u have change to support fragment `android:name="com.google.android.gms.maps.SupportMapFragment"` and you extend `FragmentActivity`. only api below 12. – Raghunandan Jul 09 '13 at 15:00
  • Yes the probleme is that the fragment's xml has a fragment. It does not work on my app but does it usualy work ? – nsvir Jul 09 '13 at 15:05
  • Ok I used FragmentActivity and android:name="<...>.SupportMapFragment" and the error is: java.lang.NullPointerException – nsvir Jul 09 '13 at 15:08
  • Ok I will try to found it. is it in logcat ? – nsvir Jul 09 '13 at 15:12
  • It says source not found. I see the stack in the top right corner but how can I past you ? – nsvir Jul 09 '13 at 15:23
  • Ok the error is this line: `mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();` – nsvir Jul 09 '13 at 15:30
  • I recommend you tu use SupportMapFragment instead MapFragment: mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); and fragment class (XML) class="com.google.android.gms.maps.SupportMapFragment" – red_alert Jul 09 '13 at 15:32
  • I really would like to but Eclipse says "Cannot cast from Fragment to SupportMapFragment" and it looks insane because R.id.map is a SupportMapFragment – nsvir Jul 09 '13 at 15:32
  • You can't use cast (MapFragment) and in XML declare the class SupportMapFragment in your fragment – red_alert Jul 09 '13 at 15:33
  • Yes I used the appropriate cast whith the appropriate id's class – nsvir Jul 09 '13 at 15:36
  • Ok got it. I was writting (SupportMapFragment) getFragmentMananager instead of getSupportFragmentManager. So now it works with an extends FragmentActivity and SupportMapFragment but the map is still not displayed – nsvir Jul 09 '13 at 15:39
  • I checked on debug and it goes in "nMap != null"'s conditional. – nsvir Jul 09 '13 at 15:49
  • You need also to call the code onResume (the code that you have in onCreate) – red_alert Jul 09 '13 at 15:51
  • Have you add inside your application tag (in manifest)? You need also to test this on a real phone and with internet turned on – red_alert Jul 09 '13 at 16:13
  • Right now the only diffrence to my code is that I declare other target versions: Try this and if it's still not working it could be the API KEY generated. – red_alert Jul 09 '13 at 16:33
  • Right now there are no errors but the map stil not loading, right? – red_alert Jul 09 '13 at 16:40
  • Yes it is. I have just generated a new Android key Simple API access and not working. keytstore file location is it important ? – nsvir Jul 09 '13 at 16:44
  • I have a warning for the minSdkVersion – nsvir Jul 09 '13 at 16:47
  • The warning is important if you want to allow compatibility below API Level 8 (Android 2.1 for example). But it is not the cause of the problem. You need realy to generate a valid key. See this post http://stackoverflow.com/questions/6305938/how-can-i-get-the-md5-fingerprint-from-javas-keytool-not-only-sha-1 – red_alert Jul 09 '13 at 16:59
  • Yea that what I did :/ Google asks for SHA1 and i gave him this exception: ";com.example" – nsvir Jul 09 '13 at 17:17
  • What hell I forgot to do :/ – nsvir Jul 09 '13 at 17:45
  • I get it! :D Look on the edit. Thank you very much for your help!! :) – nsvir Jul 10 '13 at 08:32
0

Based on nsvir's earlier edit generating a new debug.keystore and a new key solves this problem.

The information in this article was helpful in understanding more about certificates.

Community
  • 1
  • 1
Servy
  • 202,030
  • 26
  • 332
  • 449