Failed to load map. Error contacting Google server

2019-01-11 22:13发布

I have been trying to app a Google Map in my Android app using the v2 API for the past two days, with no success. All I get every time is a

Google Maps Android API(16603): Failed to load map. Error contacting Google servers. This is probably an authentication issue (but could be due to network errors).

I have followed Google's setup tutorial (https://developers.google.com/maps/documentation/android/start), tried multiple times with different projects in different workspaces, tried different Google accounts, gone through various answers and suggestions here in StackOverflow, but to no avail.

I am using Eclipse 4.2.2 with Android SDK Tools 22.01 and I have installed Google Play services (rev. 7), also I have imported google-play-services_lib into my workspace and added a reference to that to my android project.

Here is my code:

AndroidManifest.xml:

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

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

<permission
    android:name="com.example.googlemapdemo.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />
<uses-permission android:name="com.example.googlemapdemo.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.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"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.googlemapdemo.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="my_api_key" />
</application>

</manifest>

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/the_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"
/>

MainActivity.java

package com.example.googlemapdemo;

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

}

Also I created an API Project on https://code.google.com/apis/console/ where I have enabled the Google Maps Android API v2 service. Then I obtained my SHA1 debug certificate fingerprint using

keytool -list -v -keystore "C:\Users\my_user_name\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android

and entered that and the package name in the API console, got the API key and put it in the manifest (where my_api_key is). This procedure I repeated a number of times, regenerating the key but with the same result.

Could anyone help with this or suggest anything else I could try? Any help will be greatly appreciated!

17条回答
forever°为你锁心
2楼-- · 2019-01-11 22:40

I think it is the problem of your API Key.

Try the Android Sample project for maps(Google play service) from new project.

and add your API key into maps AndroidManifest.xml file.

If it is not working then it will be the problem of API Key.

查看更多
趁早两清
3楼-- · 2019-01-11 22:41

I have found new Solution that was my silly mistake.

May you have changed you application package name after Google Map Integration.

One SHA1 certificate fingerprint and package name (separated by a semicolon) per line. Example:

45:B5:E4:6F:36:AD:0A:98:94:B4:02:66:2B:12:17:F2:56:26:A0:E0;com.example

Check Google API Console

查看更多
时光不老,我们不散
4楼-- · 2019-01-11 22:42

Make sure you follow the instructions carefully in the documentation found at:

https://developers.google.com/maps/documentation/android/start#obtain_a_google_maps_api_key

As of the new ADT you should see an error message in the logcat viewer which gives you explicit instructions such as where to go, what to enable and the details you need to paste into the android fingerprint and app details. See below;

enter image description here

A few Gochas;

  1. Enable BOTH Google Maps Android API v2 AND Google Play Android Developer API in the developer console! This got me :)

  2. Make sure you are using the correct keystore to find the SHA1 hash (this can be found by going to window->preferences->android->build and look for SHA1) This will differ from your production keystore! Otherwise you can use keytool -list -v -keystore YOURKEYSTORE.keystore -alias YOURALIAS to view the SHA1

  3. Add your elements INSIDE your tags

  4. Add your elements INSIDE the <.manifest> tag (above the tag)

查看更多
Animai°情兽
5楼-- · 2019-01-11 22:42

The answer for my similar issue is to add:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
查看更多
放荡不羁爱自由
6楼-- · 2019-01-11 22:44

Make sure the "Google Maps Android API v2" is enabled, under Google APIs console, Services (see screenshot below. If it's not enabled, you will not get an error saying the service is disabled although you entered the SHA1 key.

Screenshot

查看更多
倾城 Initia
7楼-- · 2019-01-11 22:46

After spending several hours and following every tip on stackoverflow, I made it work trying one last thing.

I wanted my Map to display in another activity which I call mapActivity not the mainActivity. so when I created the sh1 key in google console api I typed package name for my mapActivity.

!!BUT it always needs to be the package name of the main activity even though you don't display the map there, so = packagename.appname from mainactivity

查看更多
登录 后发表回答