Google 地图未显示 Android

Google maps not showing Android

我想在我的应用中实现 google 地图。我添加了一个 google 映射 activity 并创建了一个密钥。我没有更改其他地方的代码中的任何内容。我应该工作,但没有。

MapsActivity

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}


/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.
 * This is where we can add markers or lines, add listeners or move the camera. In this case,
 * we just add a marker near Sydney, Australia.
 * If Google Play services is not installed on the device, the user will be prompted to install
 * it inside the SupportMapFragment. This method will only be triggered once the user has
 * installed Google Play services and returned to the app.
 */
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}

}

activity_maps.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.kevin.map.MapsActivity" />
  1. 检查您输入的 google API 密钥是否正确以及该密钥是否已启用
  2. 您是否允许位置访问权限,如果没有,请进入移动应用程序设置并单击权限选项并允许位置访问权限,然后 运行 您的代码将 运行

除其他帖子外,您可以参考此thread. The reason may be the API key used was created with the wrong keystore. You need to make sure you use your debug keystore when you create an API key in the Google API console. Based from this link,在Android使用GoogleMaps时,您需要两个键-调试和发布。 "debug" 键是一种误导性术语。开发应用程序时也会使用此密钥。所以本质上,使用调试密钥进行开发、测试、调试。当您准备好将应用程序启动到 Market 时,在 AndroidManifest.xml 中设置 android:debuggable="false" 并使用 Signed API 密钥。

要使用 Google 地图,您必须:

  1. 向片段添加逻辑或Activity。
  2. 将地图片段元素添加到图层。
  3. 获得一个API密钥(您可能需要将密钥关联到您的调试版本和您的签名版本的应用程序,每个人都有不同的签名,但两者可以共享相同的密钥)。
  4. 配置应用程序清单。在这里,要使用 Google 地图,您需要:Internet 访问、Open GL 规范,如果地图将存储在本地,您还需要访问设备存储,因为地图库包含在 google播放服务你也需要配置这个。

在清单级别:

<!-- Location if you will use this -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<!-- Maps API needs OpenGL ES 2.0. -->
<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />
<uses-permission android:name="com.example.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

在清单中的应用程序级别:

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

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="<YOUR MAP API KEY>" />

确保您也已将地图键添加到发布文件夹 (app>src>release>res>values>google_map_api.xml)。

即使按照官方文档中的步骤或完成 Android Studio 中的步骤后,您仍然看不到地图,请按照以下步骤确保这是否是一个问题是否使用 API 键:

  1. 在您的 logcat 中使用 'Google Maps' 进行过滤。
  2. 您将看到一条错误消息,其中包含正确的程序包名称和 SHA 密钥,它们应该存在于 API 密钥
  3. 转到 Google Cloud Console -> API -> Credentails,交叉验证以上两项,必要时更正。
  4. 等待 10-15 分钟以反映 API 关键更改