【问题标题】:google maps not being displayed; grey tiles only谷歌地图未显示;仅灰色瓷砖
【发布时间】:2013-10-15 19:53:07
【问题描述】:

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

这是我的代码

以下是我再次尝试的编辑代码。请查看代码和 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.

【问题讨论】:

  • 您是如何生成该 apikey 的?你用过教程中的那个吗?
  • 不,我生成了 google map api V2 .. 不是已弃用的那个
  • 您正在使用 Google Maps V1 类。在这里查看我的答案stackoverflow.com/questions/15041200/…
  • 您可能也不应该在此处发布您的 API 密钥。我会生成一个新的。
  • @trevor-e 注册密钥时,您设置了应用程序包和密钥库指纹。这使得其他人很难使用相同的密钥,但我也会生成一个新密钥。

标签: android google-maps


【解决方案1】:

见这里,只需在清单文件中使用您的密钥更改 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"/>

收到后告诉我。

【讨论】:

  • activity_main 的第一行
  • k,从activity_main.xml 中删除以前的代码并将其与我发布的代码放在一起。只需通过右键单击您的项目 --> Android 工具 --> 清除 lint 标记来忽略 lint 警告。可能会有所帮助。
  • 所以你的 API 密钥有问题,用你的 SHA-1 密钥和包名(在 Android manifest.xml 中提到)创建新的 API 密钥,用半列分隔。
  • 谢谢。最后显示地图:) 顺便说一句,告诉我一件事。现在如果我想在我的其他项目中添加这个地图,我需要创建另一个键吗?
  • 是的,您需要再创建一个 API 密钥,使用相同的 sha-1 密钥,并用 android 清单文件中提到的包名称分隔
【解决方案2】:

地图无法正常工作,因为您的清单设置不正确。来自 Android 文档的用户 this tutorial 并确保启用所需的确切权限。此外,您的 API 密钥位于错误的位置。它在您的清单中,而不是在片段中。

【讨论】:

  • 好的,让我试试,我会告诉你的。
  • 我按照本教程进行操作,但在 activity_main.xml 文件中出现错误
  • 说“它给我一个错误”根本没有帮助。请告诉我们错误是什么。
  • 用现在的样子更新你的代码。就目前该帖子中的情况而言,这是完全错误的。
  • @ hina fathima 只需复制并粘贴您在您的答案文本框中遇到的错误,然后点击“发布您的答案”
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 2014-06-27
    • 1970-01-01
    • 2013-08-01
    • 2013-06-02
    • 1970-01-01
    • 1970-01-01
    • 2011-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多