有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Android中的java地图视图

我尝试使用mapView显示地图。但是,当我运行我的应用程序时,它就崩溃了

当我打开应用程序时,它崩溃了,什么也没有显示

地图视图不显示

我的主要活动源代码

private void initGoogleMap(Bundle savedInstanceState){
    // *** IMPORTANT ***
    // MapView requires that the Bundle you pass contain _ONLY_ MapView SDK
    // objects or sub-Bundles.
    Bundle mapViewBundle = null;
    if (savedInstanceState != null) {
        mapViewBundle = savedInstanceState.getBundle(MAP_VIEW_BUNDLE_KEY);
    }

    mMapView.onCreate(mapViewBundle);

    mMapView.getMapAsync(this);
}



@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    Bundle mapViewBundle = outState.getBundle(MAP_VIEW_BUNDLE_KEY);
    if (mapViewBundle == null) {
        mapViewBundle = new Bundle();
        outState.putBundle(MAP_VIEW_BUNDLE_KEY, mapViewBundle);
    }

    mMapView.onSaveInstanceState(mapViewBundle);
}

这是我的错误日志

Caused by: java.lang.RuntimeException: API key not found.  Check that <meta-data 安卓:name="com.google.安卓.geo.API_KEY" 安卓:value="your API key"/> is in the <application> element of AndroidManifest.xml
    at com.google.maps.api.安卓.lib6.drd.p.b(:com.google.安卓.gms.dynamite_mapsdynamite@201215028@20.12.15 (020304-0):6)
    at com.google.maps.api.安卓.lib6.auth.f.a(:com.google.安卓.gms.dynamite_mapsdynamite@201215028@20.12.15 (020304-0):1)
    at com.google.安卓.gms.maps.internal.CreatorImpl.a(:com.google.安卓.gms.dynamite_mapsdynamite@201215028@20.12.15 (020304-0):20)
    at com.google.安卓.gms.maps.internal.CreatorImpl.newMapViewDelegate(:com.google.安卓.gms.dynamite_mapsdynamite@201215028@20.12.15 (020304-0):2)
    at com.google.安卓.gms.maps.internal.g.a(:com.google.安卓.gms.dynamite_mapsdynamite@201215028@20.12.15 (020304-0):18)
    at ch.onTransact(:com.google.安卓.gms.dynamite_mapsdynamite@201215028@20.12.15 (020304-0):4)
    at 安卓.os.Binder.transact(Binder.java:385)
    at crx.a(:com.google.安卓.gms@201215007@20.12.15 (020304-302916295):2)
    at afiu.newMapViewDelegate(:com.google.安卓.gms@201215007@20.12.15 (020304-302916295):1)
    at com.google.安卓.gms.maps.internal.CreatorImpl.newMapViewDelegate(:com.google.安卓.gms@201215007@20.12.15 (020304-302916295):3)
    at afiv.a(:com.google.安卓.gms@201215007@20.12.15 (020304-302916295):18)
    at cry.onTransact(:com.google.安卓.gms@201215007@20.12.15 (020304-302916295):12)
    at 安卓.os.Binder.transact(Binder.java:385)
    at com.google.安卓.gms.maps.MapView$zzb.zza(Unknown Source)
    at com.google.安卓.gms.dynamic.zza.zza(Unknown Source)
    at com.google.安卓.gms.dynamic.zza.onCreate(Unknown Source)
    at com.google.安卓.gms.maps.MapView.onCreate(Unknown Source)
    at com.istiak.locationlatitudelongitude.MainActivity.initGoogleMap(MainActivity.java:91)
    at com.istiak.locationlatitudelongitude.MainActivity.onCreate(MainActivity.java:77)
    at 安卓.app.Activity.performCreate(Activity.java:6156)
    at 安卓.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
    at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2477)
    at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2610) 

图书馆

compile 'com.google.安卓.gms:play-services-maps:11.0.4'
compile 'com.google.安卓.gms:play-services-places:11.0.4'

提前谢谢


共 (2) 个答案

  1. # 1 楼答案

    首先,请导航到此链接Get Your API Key
    然后进入控制台,点击导航菜单,然后点击Api&;服务和转到凭据。然后点击create credentials,这样它就会自动创建一个API密钥。将出现一个弹出窗口,如果不想限制API密钥,请单击“取消”。请不要忘记从API&;的库部分启用适用于Android的Maps SDK;服务菜单
    获取Api密钥后,请将其粘贴到下面的代码中,并按下面所述添加此代码

    <application>
    ...
    <meta-data android:name="com.google.android.geo.API_KEY"
               android:value="your API key"/>
    ...
    </application>
    
  2. # 2 楼答案

    Caused by: java.lang.RuntimeException: API key not found. Check that is in the element of AndroidManifest.xml

    Specify the Google Play services version number

    Add the following declaration within the application element of AndroidManifest.xml. This embeds the version of Google Play services that the app was compiled with.

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    

    https://developers.google.com/maps/documentation/android-sdk/config