Google Maps v2简单应用程序不起作用

时间:2013-12-18 21:12:31

标签: java android google-maps android-manifest google-maps-api-2

我正在关注this教程。这是我的所有相关文件: 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>

我完成了教程,直到标题放置标记。但是我收到以下错误。

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

如果我将清单文件写成以下行:

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

这次我收到了这个错误:

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

安卓开玩笑吧?为什么代码崩溃了?我该怎么办才能运行该死的简单代码?为什么它在教程中工作而不在我的机器上。它让我恼火。

(顺便说一句,我使用Windows 7和真实设备Nexus 7并且我复制了一个version.xml,同时尝试了下面的元数据

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

再次没有奏效。最后,当然我搜索了SO和谷歌查看了热门的教程)请帮助我。

3 个答案:

答案 0 :(得分:7)

您需要有两个meta-data字段。一般来说,谷歌游戏服务需要一个,第二个是使用地图。

您应该在[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" />

答案 1 :(得分:3)

你需要

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

答案 2 :(得分:0)

您需要为两个元数据提供外部存储权限:

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