【问题标题】:Android Google API "Failed to load Map"Android Google API“无法加载地图”
【发布时间】:2013-07-07 03:48:59
【问题描述】:

我正在尝试显示 Google 地图的地图。

我关注了这个tutorial

我修复了一些问题并且应用程序运行了,但我无法显示地图。

我做了什么:

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

这里是我的 .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>

还有我的清单:

<?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>

【问题讨论】:

  • android:minSdkVersion="11" 将此设为 12 或使用 supportfragment

标签: android api google-api


【解决方案1】:

基于nsvir的earlier edit生成新的debug.keystore,新的key解决了这个问题。

this article 中的信息有助于了解有关证书的更多信息。

【讨论】:

    【解决方案2】:

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

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

    将您的片段 XML 替换为:

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

    让您的活动扩展 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;
    

    【讨论】:

    • 片段使应用程序崩溃:/
    • 您尝试过我上次编辑的代码吗?我发布的代码适用于 Android 2.2.1
    • 我尝试了编辑后的代码并扩展到 FragmentActivity 并且它崩溃了。我尝试了扩展 Activity,它没有崩溃,但它不起作用。
    • 而且xml类也会崩溃
    • 顺便说一句,我无法将 SupportMapFragment cast 与 GoogleMap 一起使用,我只能使用 MapFragment cast 您的 mMap 是一个私有 GoogleMap 变量,不是吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多