简体   繁体   中英

Google Maps v2 Simple Application Not Working

I am following this tutorial. Here are all my related files: activity_main.xml

<?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>

MainActivity.java

package org.barisakkurt.googlemapsv2;

import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;

public class MainActivity extends FragmentActivity {

    // Google Map
    private GoogleMap googleMap;

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

        try {
            // Loading map
            initilizeMap();
        } catch (Exception e) {

        }
    }

    /**
     * function to load map. If map is not created it will create it for you
     * */
    private void initilizeMap() {
        if (googleMap == null) {

            googleMap = ((SupportMapFragment)  getSupportFragmentManager().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();
    }

}

AndroidManifest.xml

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

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

    <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" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="org.barisakkurt.googlemapsv2.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="AIzaSyAC7y4bP-MjeJljm6Fr9Og1FKxNe9ZiFkY" />
    </application>

</manifest>

I completed the tutorial until the title Placing a Marker . However I am getting the following error.

Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value.  Expected 4030500 but found 0.  You must have the following declaration within the <application> element:     <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

If I write my manifest file the following lines:

<meta-data
            android:name="com.google.android.gms.version"
            android:value="4030500" />

this time I am getting this error:

Caused by: java.lang.RuntimeException: API key not found.  Check that <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="your API key"/> is in the <application> element of AndroidManifest.xml

Is android kidding me? Why is that code crashes? What do i have to do to run that damn simple code? Why it is working in the tutorial and not on my machine. It annoyed me.

(By the way, I am using Windows 7 with a real device Nexus 7 and I copied a version.xml tried also the following meta-data

<meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

and again didn't work. Lastly, of course I searched SO and google looked at the popular tutorials) Please help me.

You need to have two meta-data fields. One is required for google play services in general, the second one on top for using maps.

Example snippet from the sample application you should have in [android-sdk]/extras/google/google_play_services/samples/maps/AndroidManifest.xml

  <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/demo_title"
    android:hardwareAccelerated="true">
    <!-- ** You need to replace the key below with your own key. **
         The example key below will not be accepted because it is not linked to the
         certificate which you will use to sign this application.
         See: https://developers.google.com/maps/documentation/android/start
         for instructions on how to get your own key. -->

    <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyCvGBr5in13NK2yYBR7lhXTtnxj3mrXQy4" />

    <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

you need

<application..>
    <meta-data
         android:name="com.google.android.gms.version"
         android:value="@integer/google_play_services_version" />
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="KEY" />

    ...
</application>

You need do give External Storage permission with both meta-data:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <application
    <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
         <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyCm5xJy0AwN4TfXM6ccpfj2GhqZneFCyiM" />
     </application>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM