GoogleMap演示项目

时间:2014-01-28 07:09:32

标签: android

在谷歌地图演示中,在Eclipse中我在清单文件

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

但我得到了反感

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

我的清单:

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

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

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

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

            <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" >
                <activity
                    android:name="com.example.googlemapsdemo.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.gms.version"
                    android:value="@integer/google_play_services_version" />
            </application>

        </manifest>

最后它还在Toast中显示其他部分...意味着它接收地图片段对象null 请告知我在哪里犯了错误:

package com.example.googlemapsdemo;

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

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends Activity 
{
    private GoogleMap googleMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try 
        {
                // Loading map
                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();
    }

}

4 个答案:

答案 0 :(得分:2)

在您添加google play service lib version之后添加api密钥作为此类元数据(根据google mapv2的新文档)

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

 <!-- add this meta-data tag for api key-->
<meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyBLHwjdXsQK0sszxfrkoncHlqU3d2mDJok" />

For more info read this ........ api key was taken from ur previous question

答案 1 :(得分:0)

您已经拥有正确的错误说明:

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 

您的清单缺少以下meta-tag

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="@your api key"
        />

您必须转到Google Api Console并创建一个项目。然后,您可以在Android Key中创建API Access

在Google开发者网站上,您可以很好地了解Obtain a Google Maps API Key

答案 2 :(得分:0)

点击“获取Google Maps Android v1 API密钥”链接。

Google Maps Android v1 API Key

将生成的API密钥添加到清单文件中。

答案 3 :(得分:0)

您忘记在清单文件中添加API密钥的代码。只需在标记内的清单文件中添加以下行。

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