Google地图未显示Android

问题描述 投票:0回答:5

我想在我的应用程序中实现谷歌地图。我添加了谷歌地图活动并创建了一个密钥。我在其他地方的代码中没有改变任何东西。我应该工作,但事实并非如此。

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" />
java android google-maps google-maps-android-api-2
5个回答
5
投票

即使您按照官方文档中的步骤操作,或者您已完成Android Studio中的步骤,并且仍然无法看到地图,请按照以下步骤操作,以确保它是否与API密钥有关:

  1. 在logcat中使用“Google地图”进行过滤。
  2. 您将看到一条错误消息,其中包含正确的软件包名称和SHA密钥,该密钥应该存在于API密钥中
  3. 转到Google云端控制台 - > API - >凭据,然后交叉验证以上内容,必要时进行更正。
  4. 等待10-15分钟,以反映API密钥更改

1
投票

除了其他帖子,您可以参考这个thread。原因可能是使用的密钥是使用错误的密钥库创建的。在Google API控制台中创建API密钥时,需要确保使用调试密钥库。基于这个link,当使用GoogleMaps for Android时,你需要两个键 - 调试和发布。 “调试”键是一种误导性术语。在开发应用程序时也可以使用此密钥。基本上,使用调试密钥进行开发,测试和调试。当您准备将应用程序启动到Market时,请在AndroidManifest.xml中设置android:debuggable =“false”并使用签名的API密钥。


1
投票

要使用Google地图,您必须:

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

在清单级别:

<!-- 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>" />

0
投票
  1. 检查您输入的Google API密钥是否正确以及该密钥是否已启用
  2. 您是否允许位置访问权限,如果没有,请进入移动应用设置,然后单击权限选项并允许位置访问权限,然后运行您将运行的代码

0
投票

确保您已在发布文件夹中添加了地图密钥(app> src> release> res> values> google_map_api.xml)。

© www.soinside.com 2019 - 2024. All rights reserved.