Google Maps API V2'无法加载地图.无法联系Google服务器,即使我检查权限和密钥库

xin*_*Cai 7 android google-maps

我按照指南创建了一个谷歌地图样本,

但它总是抛出以下错误.

E/Google Maps Android API(27821): Failed to load map.  Could not contact Google servers.
Run Code Online (Sandbox Code Playgroud)

权限READ_GSERVICES以及调试和发布密钥库已经过测试.他们也无法解决上述问题.谁能告诉我为什么会抛出这个错误?

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.where.common"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />
    <permission
        android:name="com.where.common.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.where.common.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <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="AIzaSyDawEkMP7gdiB4nOOkXcdUcxSAvm0kfCmI" />
        <activity
            android:name="com.where.common.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>
</manifest>
Run Code Online (Sandbox Code Playgroud)

Java文件:

package com.where.common;

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

public class MainActivity extends Activity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
Run Code Online (Sandbox Code Playgroud)

查看文件:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
Run Code Online (Sandbox Code Playgroud)

小智 7

确保所有下面提到的要点都得到照顾:

1)启用了错误的服务.确保已启用" Google Maps Android API v2 ",而非" Google Maps API v2 "并重新生成API密钥.

2)将以下元素添加到清单中.将com.example.mapdemo替换为应用程序的包名称.

3)使用正确的证书和密钥.发布证书,当您在我的手机上运行应用程序时,调试期间无法正常工作.您必须使用调试密钥库证书指纹.


Emr*_*Koç 7

在尝试新的API KEY之前不要忘记清除程序缓存:)因为Android即使您更改API密钥也使用第一个API密钥.


Emi*_*Adz 4

您添加了所有其他需要的权限吗?

<permission android:name="com.eadesign.skygiraffefinalv2.permission.MAPS_RECEIVE" android:protectionLevel="signature"/>
<uses-permission android:name="com.eadesign.skygiraffefinalv2.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" />
Run Code Online (Sandbox Code Playgroud)

更新:

1.meta-data部分应该位于应用程序标签的最下部,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.where.common"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="16" />
<permission
    android:name="com.where.common.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />
<uses-permission android:name="com.where.common.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<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.where.common.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="AIzaSyDawEkMP7gdiB4nOOkXcdUcxSAvm0kfCmI" />
</application>
Run Code Online (Sandbox Code Playgroud)

2.我看到的另一个问题是您为 API V8 开发应用程序,因此您必须将SupportMapFragment和用作FragmentActivity您的Activity.

3.要使您的地图正常工作,您必须添加 Google 许可信息的许可活动。