2

我正在尝试使用谷歌地图创建一个应用程序。我使用了谷歌地图 api v2,但地图没有显示出来。我只看到一个带有缩放按钮的灰色屏幕。

有任何想法吗?我很确定错误不在 API 密钥中。

这是我的代码..

安卓清单:

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

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

    <permission
        android:name="com.examplemaps.rs.MAPS_RECEIVE"
        android:protectionLevel = "signature"/>

    <uses-permission android:name="com.examplemaps.rs.MAPS_RECEIVE" />
    <uses-feature android:glEsVersion="0x00020000"
        android:required="true"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="com.examplemaps.rs.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.examplemaps.rs.Home"
            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="AIzaSyB5aBact***myAPI key"/>


    </application>

</manifest>

主页.java:

 package com.examplemaps.rs;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;

public class Home extends FragmentActivity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_home, menu);
        return true;
    }

}

和activity_home.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"
    tools:context=".Home" >

  <fragment xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/map"
       android:name="com.google.android.gms.maps.SupportMapFragment"
       android:layout_width="wrap_content"
       android:layout_height="match_parent" />

</RelativeLayout>

谢谢!

4

4 回答 4

2

我发现了错误。在 Android Manifest 中,我已将 READ_GSERVICES 替换为:

 <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>

现在,它工作正常!

于 2013-01-28T19:16:41.620 回答
0

我从未尝试过,但我很确定 MapView 应该在 MapFragment 内(如果是 Activity,则应该是 MapActivity)。

你试过谷歌地图视图片段吗?

于 2013-01-28T15:58:10.863 回答
0

您说您确定错误不在于 API 密钥,但您是否检查过您的 API 密钥是否与您用于编译项目的帐户相对应?(即您是否使用调试令牌或发布令牌获得了 API 密钥)。

另一件要尝试的事情(当然,如果你还没有的话)是设备上的测试。我以前在模拟器上遇到过灰色瓷砖问题,但在真实设备上没有这样的问题。

否则,Asaf Nevo的答案可能更准确,MapViews应该倾向于 aMapActivity或类似,并确保您正在构建 Android 的Google API版本。请参阅MapFragment

取自谷歌地图

  • 地图现在封装在 MapFragment 类中,它是 Android 的 Fragment 类的扩展。现在您可以将地图添加为更大活动的一部分。使用 MapFragment 对象,您可以在较小的屏幕(例如手机)上单独显示地图,或者在较大屏幕的设备(例如平板电脑)上作为更复杂 UI 的一部分。
  • 因为地图封装在 MapFragment 类中,所以可以通过扩展 Android 标准 Activity 类来实现,而不是扩展版本 1 中使用的 MapActivity。
于 2013-01-28T17:34:19.850 回答
0

您是否确保设备上实际安装了“地图”应用程序?即https://play.google.com/store/apps/details?id=com.google.android.apps.maps&hl=en

我有一些没有安装地图的旧设备,并且有相同的症状(带有缩放按钮但没有地图内容的灰屏)。

于 2013-01-28T19:12:05.667 回答