谷歌地图没有显示;只有灰色瓷砖

时间:2013-10-15 19:53:07

标签: android google-maps

我正在尝试谷歌地图使用教程,但它没有显示地图;只是灰色的瓷砖。请帮助我..我的项目因此而陷入困境。

这是我的代码

以下是我再次尝试的EDITED代码。请看一下代码和logcat文件。

清单文件

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

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

     <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <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" />
    <!--
     The following two permissions are not required to use
     Google Maps Android API v2, but are recommended.
    -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

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

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

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

    </application>


</manifest>

activity_main.xml中

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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" >

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

</RelativeLayout>

MainActivity.java

package com.example.googlemapsv2;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.Menu;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;


public class MainActivity extends Activity {

     GoogleMap map;
     @SuppressLint("NewApi")

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

         map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
                    .getMap();
    }



}

logcat的

10-17 19:16:53.750: E/Google Maps Android API(9452): Authorization failure.  Please see https://developers.google.com/maps/documentation/android/start for how to correctly set up the map.
10-17 19:16:53.750: E/Google Maps Android API(9452): Ensure that the following correspond to what is in the API Console: Package Name: com.example.googlemapsv2, API Key: AIzaSyCmYChMyMqZCj5nZl-lykGioJwNGW-hTCA, Certificate Fingerprint: 5A6683F0B22028AFBFF49C0EC90FCFA015AA20C6
10-17 19:16:53.773: I/Google Maps Android API(9452): Failed to contact Google servers. Another attempt will be made when connectivity is established.

3 个答案:

答案 0 :(得分:2)

请参阅此处,只需使用清单文件中的密钥更改api密钥,然后按照以下步骤操作: 并确保您的google_play_services_lib项目仅存在于项目的工作空间中。

清单文件:              

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

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.geeklabs.map.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="replace with your API key"/>

    </application>

</manifest>

MainActivity.java:

    package com.geeklabs.map;

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

}

activity_main.xml中:

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

得到这个后告诉我。

答案 1 :(得分:0)

地图无效,因为您的清单未正确设置。 Android文档中的用户this tutorial,并确保您启用了所需的确切权限。您的API密钥也在错误的位置。它出现在你的清单中,而不是碎片中。

答案 2 :(得分:-1)

在某些设备上加载Google地图版本2时存在一些问题,您只需重启设备即可开始工作。