安卓地图未显示,仅显示网格。

4
问题已解决 解决方案简述:确保你没有使用api v1的类。目前当我在工作时,模拟器对于测试应用程序是无用的。所以请在实际设备上测试它。 最后,如果应用程序可以正确运行但不显示地图,则你的问题可能与密钥有关。 我在这里注意到的是,catlog没有说密钥错误,应用程序运行正常,但地图没有显示。

我尝试了两天制作一个简单的谷歌地图android应用程序,只是在Activity上显示地图,但失败了。尝试了每个教程直到谷歌的第二页。没有什么作用。唯一有效的是我在书中跟随的应用程序,但它只显示网格而没有地图。通常人们会回答密钥是错误的,但事实并非如此。我的密钥是正确的,我准确地生成了密钥。 我正在使用Google Maps Android API v2密钥。

这是我的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <com.google.android.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:apiKey="0AeGR0UwGH4pYmhcwaA9JF5mMEtrmwFe8RobTHA"
        android:clickable="true"
        android:enabled="true" />

</LinearLayout>

这是我的MainActivity.java文件

package com.example.lbs;

import android.os.Bundle;
import android.view.Menu;

import com.google.android.maps.MapActivity;

public class MainActivity extends MapActivity {

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

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }

}

这是我的AndroidManifest.xml文件。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.lbs"
    android:versionCode="1"
    android:versionName="1.0" >

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

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

    <uses-permission android:name="android.permission.INTERNET" />

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

    <uses-permission android:name="com.example.lbs.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <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" >
        <uses-library android:name="com.google.android.maps" />

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

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyB1RpoULFVTRkXREZX0ZAwxcz4_75Y0HYc" />

</manifest>

在 Catlog 上,当应用程序运行时我得到这个:

enter image description here

IOException processing: 26
java.io.IOException: Server returned: 3
    at android_maps_conflict_avoidance.com.google.googlenav.map.BaseTileRequest.readResponseData(BaseTileRequest.java:115)

更新1: 在上述所有操作之后,我在某些地方阅读到的代码可能仅适用于API密钥v1。因此,我完全按照https://blog-emildesign.rhcloud.com/?p=435的教程进行了操作,最终得到了下面的结果:

enter image description here

更新2: 现在,我尝试在实际的Android设备上运行它。它是Android 2.3。 所以我做了更多的工作。最终,在我的活动中得到了以下结果:

enter image description here

而我在CatLog中看到的是

enter image description here

所以地图仍然没有显示……请帮忙解决。

更新3: 问题在于API密钥。我在logcat中看不到任何关于错误密钥的信息。应用程序可以运行,但不显示地图。所以我重新生成了密钥。 这就是结果: enter image description here

3个回答

1

你的问题在于你生成了Google Maps API V2的密钥,但是你正在尝试使用Google Maps API V1对象MapView

  <com.google.android.maps.MapView
    android:id="@+id/mapView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:apiKey="0AeGR0UwGH4pYmhcwaA9JF5mMEtrmwFe8RobTHA"
    android:clickable="true"
    android:enabled="true" />

相反,您应该使用MapFramgentSupportMapFragment,具体取决于您所针对的API级别。您可以在我撰写的以下指南中获得有关如何将Google Maps API V2集成到您的应用程序中的完整说明:

Google Maps API V2

更新:

第二个屏幕截图是您应该在模拟器上获得的内容。Google Play服务未安装在模拟器上,这就是您看到的原因。请尝试在已安装Google Play服务的设备上测试此应用程序,它应该可以正常工作。


与此同时,当您回答时,我已经按照教程进行了操作,并且我在问题的最后部分得到了结果。我编辑了问题,请看一下问题的最后部分。 - Eres
你不觉得按照这个教程 https://blog-emildesign.rhcloud.com/?p=527 并安装教程中提到的那些apk文件会让它在这个模拟器上运行吗? - Eres
它应该可以,但不是使用Android SDK Manager下载的当前Google Play Services库。如果您遵循此帖子中的评论,您将找到此链接:http://venomvendor.blogspot.co.il/2012/03/android-sdk-extras-by-google-inc.html 如果您从那里下载第4版并将其用于项目,并在模拟器上安装这些文件,则应该可以正常工作。 - Emil Adz
现在我已经在实际的安卓设备上尝试了,应用程序正在运行。但是地图没有显示出来。请看一下我问题编辑中的最后两张图片(我的问题更新-2)。 - Eres
现在问题出在 API 密钥上。我重新生成了它,然后它就可以工作了。谢谢。 - Eres
欢迎,@ArslanAfzal。很高兴你能让它运行起来。 - Emil Adz

1

请检查!我也遇到了类似的问题。这对我有用! 1) 打开Google API控制台,API密钥是在此生成的。 2) 点击“服务”(显示在左侧选项中)。 3) 悬停并搜索Google地图Android API v2。 4) 启用它(最初它是关闭的,因此只会显示灰色瓦片)。


0
在Google APIs控制台中,在您的Android应用程序密钥下,确保您添加了调试和发布的指纹。

它要求输入SHA1代码和包名。我已经插入了这些信息,控制台给了我一个密钥。我还有什么遗漏吗? - Eres
有两个不同的sha1,一个是应用程序发布版本的(在您的密钥库中),另一个是调试版本的(位于~/.android/debug.keystore)。在发布密钥之后也添加调试密钥。我以前做第一个地图应用程序时也遇到了类似的问题,那就是原因。 - Naroh
现在我已经得到了密钥,但是在控制台中我看不到任何按钮或文本框来添加另一个密钥。目前我只想在模拟器上运行它。如果您认为有必要添加其他密钥,那么应该如何添加? - Eres
请使用“允许编辑Android应用程序”链接。 - Naroh

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