【问题标题】:Authorization failed. Failed to load map. Could not contact Google servers. (Google Map for Android)授权失败。加载地图失败。无法联系 Google 服务器。 (Android 版谷歌地图)
【发布时间】:2013-05-08 07:55:00
【问题描述】:

我已经被这个问题困扰了一天。我已按照教程进行操作,甚至更改为完全一样的教程。我似乎无法理解有什么问题。每次我在我的设备上运行它时,地图都不会加载,并且在 LogCat 中它会显示:

05-08 16:21:02.130: E/Google Maps Android API(28021): Failed to load map. Could not contact Google servers.

这是我的布局 main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    map:cameraTilt="45"
    map:cameraZoom="14" >

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

这是 AndroidManifest.xml:

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

<permission
  android:name="com.example.permission.MAPS_RECEIVE"
  android:protectionLevel="signature"/>
<uses-permission android:name="com.example.permission.MAPS_RECEIVE"/>
<uses-permission android:name="com.example.gmapactivity.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name="com.example.gmapactivity.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="the-generated-api-key-from-console" />   
</application>

</manifest>

最后是我的 MainActivity.java:

package com.example.gmapactivity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;

public class MainActivity extends FragmentActivity {

@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;
}
}

我删除了我的 android 密钥并从我的调试密钥库中重新生成,这也是 Eclipse 中的默认调试密钥库。将生成的 SHA1 密钥与 Google Maps Android API v2 服务一起用于 Google API,并将 API 密钥应用于清单。仍然没有运气。如果有人能提出解决方案,我将不胜感激。

【问题讨论】:

  • 可能是一个转储问题,您的设备有一个有效且有效的互联网连接?
  • 您是否在android:value 中添加了密钥?您提供的清单中没有密钥还是仅用于说明目的?
  • 我已经将生成的 API 密钥从控制台应用到了 android:value。我只是出于安全目的没有显示它。

标签: android google-maps google-maps-android-api-2 android-maps-v2 google-play-services


【解决方案1】:

您应用了错误的权限:

<permission
  android:name="com.example.permission.MAPS_RECEIVE"
  android:protectionLevel="signature"/>
<uses-permission android:name="com.example.permission.MAPS_RECEIVE"/>
<uses-permission android:name="com.example.gmapactivity.permission.MAPS_RECEIVE"/>

应该是这样的:

<permission
 android:name="com.example.gmapactivity.permission.MAPS_RECEIVE"
 android:protectionLevel="signature"/>
<uses-permission android:name="com.example.gmapactivity.permission.MAPS_RECEIVE"/>

在你的布局文件中: 这些参数:

map:cameraTilt="45"
map:cameraZoom="14"

应该应用于片段而不是RelativeLayout

【讨论】:

  • 传输map:cameraTilt="45" map:cameraZoom="14" 会给出错误“为标签片段找到意外的命名空间前缀“map””
  • 这是因为您必须将此行: xmlns:map="schemas.android.com/apk/res-auto" 也移动到片段部分,而不是在主 RelativeLayout 中。
  • 我以前做过,但会为xmlns:map="http://schemas.android.com/apk/res-auto"添加另一个错误Unexpected namespace prefix "xmlns" found for tag fragment
【解决方案2】:

您是否已将您的包添加到 Google APIS 中的 API Access 中?

您应该在“编辑允许的 Android 应用”中有类似的内容:

FD:F4:....:D2;com.example.gmapactivity

第一个参数是你说的签名密钥的SHA-1,第二个是你的应用程序包

此外,从您的代码中,您将 main.xml 加载到选项菜单,而不是片段的视图(即 activity_main.xml)。改变你的

setContentView(R.layout.activity_main);

setContentView(R.layout.main);

【讨论】:

  • 我确实应用了我的包,但仅在 com.example 之前:FD:F4:....:D2;com.example 不是 FD:F4:....:D2;com.example.gmapactivity 此外,我的布局中没有 main.xml,因此会导致错误 main could not be resolved or is not a field
  • 该包必须与您的 AndroidManifest.xml 中定义的相同。根据你的说法,完整的包是“com.example.gmapactivity”,所以它需要是这个(而不是“com.example”)
【解决方案3】:

好的,所以我一直在做正确的事情。只是我一直把它编译成3.2的android设备而不是4.0的设备,现在可以了!我只是不明白为什么当我的 targetSdkVersion 为 17 时,API 级别为 15 的 3.2 版本不会给出结果?

【讨论】:

【解决方案4】:

不要惊慌。你可以让它成功的简单方法。

如果您非常确定清单文件和 google play 服务库中的 API 密钥,那么。 按照步骤操作。

1) 从设备上卸载您的应用 2)清理项目 3) 再次运行应用程序

这应该可以。这个解决方案对我有用..cheers :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-11
    相关资源
    最近更新 更多