安卓谷歌 API "Failed to load Map"

标签 android api google-api

我正在尝试显示 Google Map 的 map 。

我关注了这个tutorial .

我修正了一些问题,应用程序运行了,但我无法显示 map 。

这是我做的:

  • 在 Google 控制台上创建了一个 API 项目
  • 创建了一个 ID
  • 激活“Google Maps Android API v2”服务
  • 生成新的 Android key
  • 在 list 中导入此 key
  • 按照此视频安装库

这里是我的 .java:

public class MainActivity extends FragmentActivity {

private static GoogleMap mMap = null;

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

    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());

    if(status == ConnectionResult.SUCCESS){
       if (mMap == null) {

          mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

          if (mMap != null) {
            UiSettings settings = mMap.getUiSettings();

            settings.setZoomControlsEnabled(true);
            settings.setCompassEnabled(true);
            settings.setRotateGesturesEnabled(true);
            settings.setTiltGesturesEnabled(true);
            settings.setScrollGesturesEnabled(true);
            settings.setZoomControlsEnabled(true);
            settings.setZoomGesturesEnabled(true);
            settings.setMyLocationButtonEnabled(false);

            mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            mMap.setMyLocationEnabled(false);
            }
          }
       }
    }
    protected void onResume() {
        super.onResume();

        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());

        if(status == ConnectionResult.SUCCESS){
           if (mMap == null) {

              mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

              if (mMap != null) {
                UiSettings settings = mMap.getUiSettings();

                settings.setZoomControlsEnabled(true);
                settings.setCompassEnabled(true);
                settings.setRotateGesturesEnabled(true);
                settings.setTiltGesturesEnabled(true);
                settings.setScrollGesturesEnabled(true);
                settings.setZoomControlsEnabled(true);
                settings.setZoomGesturesEnabled(true);
                settings.setMyLocationButtonEnabled(false);

                mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                mMap.setMyLocationEnabled(false);
              }
          }
     }
 }

我的 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" >
    <fragment
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          class="com.google.android.gms.maps.SupportMapFragment" />


</LinearLayout>

还有我的 list :

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

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

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

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

    <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.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="<MyKey>"/>

    </application>

</manifest>

最佳答案

在您的应用程序标签内(在 list 中)添加:

<uses-library android:name="com.google.android.maps" />

将您的 fragment XML 替换为:

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

让您的 Activity 扩展 FragmentActivity 和 onCreate 和 onResume 调用此代码:

int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());

if(status == ConnectionResult.SUCCESS){
   if (mMap == null) {

      mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

      if (mMap != null) {
        UiSettings settings = mMap.getUiSettings();

        settings.setZoomControlsEnabled(true);
        settings.setCompassEnabled(true);
        settings.setRotateGesturesEnabled(true);
        settings.setTiltGesturesEnabled(true);
        settings.setScrollGesturesEnabled(true);
        settings.setZoomControlsEnabled(true);
        settings.setZoomGesturesEnabled(true);
        settings.setMyLocationButtonEnabled(false);

        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        mMap.setMyLocationEnabled(false);
      }
    }
}

不要忘记像这样声明您的 mMap 变量:

private GoogleMap mMap;

关于安卓谷歌 API "Failed to load Map",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17549908/

相关文章:

android - 在 Android : how to use gluUnProject? 上打开 GL ES

c# - 使用 PayPal 帐户付款而无需重定向到 PayPal 的网站

php - 无法验证 Google 访问 token (段数错误)

java - Google 如何验证 API 调用中的 SHA1 和程序包名称?

android - 谷歌纸板应用-小屏幕

android - 查看 Firebase 上的 childEventListener 是否已完成所有数据的加载

javascript - 使用 Express 发出 POST 请求时出错

php - 谷歌API文件权限

android - 为什么振动在 Android P (API 28) 上不起作用?

php - 如何在Elasticsearch中具有子字段的字段上运行术语过滤器