安卓地图:加载地图失败。无法连接谷歌服务器。

7

这个bug存在已久。(我确保“Google Maps Android API v2”是打开的。)

以下是MainActivity.java文件:

package com.example.maptest;

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

清单文件:

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

     <permission
        android:name="com.example.maptest.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />
    <uses-permission 
        android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>


    <uses-permission android:name="com.example.maptest.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="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />


    <uses-feature
        android:name="android.hardware.location"
        android:required="true" />
    <uses-feature
        android:name="android.hardware.location.network"
        android:required="true" />
    <uses-feature android:name="android.hardware.location.gps" />
    <uses-feature
        android:name="android.hardware.wifi"
        android:required="true" />

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

        <uses-library android:name="com.google.android.maps" />

        <activity
            android:name="com.example.maptest.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="AIzaSyAoDwhSzOopQ3g8NBe7d0WblR72TkmnVPU" />


    </application>

</manifest>

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".MainActivity" >


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

</RelativeLayout>

请帮忙!我不确定出了什么问题...尽管我最终合并了各种教程,但它们单独运行时均无法正常工作。


  1. 您的签名密钥和包名是否已正确注册该API密钥?
  2. 您等了多久?API密钥并非总是立即生效。
- j__m
1个回答

6

1. 我看到的第一个问题在这里:

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

请删除以下代码:

class="com.google.android.gms.maps.SupportMapFragment"

并将 android:name 设置为:

 android:name="com.google.android.gms.maps.SupportMapFragment"

2. 从清单文件中删除此行:

<uses-library android:name="com.google.android.maps" />

这是Google Maps API V1的许可证,不应在API V2中使用。

请查看此博客文章,并确保您正确执行了所有步骤:

Google Maps API V2

3. 您所描述的问题通常源于使用API控制台生成或注册API密钥时出现问题。如果您确定已经正确执行了所有步骤,则建议您删除 debug.keystore 文件夹,在Eclipse中编译一些项目(这将导致生成新的SHA1密钥),然后再次使用控制台注册密钥。请查看我撰写的这篇博客文章,并确保您正确执行了所有步骤:

Google Map API V2 Key


嗨,Emil Adz,我已经按照那些步骤成功创建了一个地图示例应用程序,并且它在大多数设备上都可以运行(我在Galaxy Ace Plus、Galaxy 7英寸平板电脑和Sony Xperia Z上进行了测试)。但是当我尝试在HTC Desire上测试相同的应用程序时,它显示错误,如“无法找到类'maps.i.k',从方法maps.z.ag.a引用。加载地图失败。无法联系Google服务器”。我已经在该设备上交叉验证了Google Play服务是否安装。所以你能帮我解决这个问题吗? - Raj
@Raj,请开一个新的问题,我会在那里尝试帮助你。不要在评论中写下你的问题。 - Emil Adz
谢谢Emil Adz,现在在我更新设备上的地图应用程序后它可以工作了。无论如何,感谢您的支持。 - Raj
很高兴你解决了这个问题。考虑到你的应用在除了这个设备以外的所有设备上都能正常工作,这确实引起了警示,说明设备配置存在问题,而不是你的应用程序。 - Emil Adz
如果有人仍在寻找答案,请查看此处 - 相同的问题:https://dev59.com/02Mm5IYBdhLWcg3wX-AW#17947755 - Oleksii K.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接