谷歌地图片段显示空地图

时间:2017-12-03 11:03:03

标签: android google-maps google-maps-api-3

我想在我的应用中添加地图,并且我已按照google maps api网站上的所有说明操作。 1.我制作了这个项目并添加了一个新的谷歌地图活动。 2.我从google_maps_api.xml中提供的链接获得了一个api密钥并输入了它。 3.我添加了权限:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission
    android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET"/>

4。我添加了元数据:  <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> 但是当我打开活动时,谷歌徽标就在那里,但它显示了一张空地图。为什么会发生这种情况以及解决方案是什么? 这是我的代码:

的AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.barlificent.transport">

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission
    android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">


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

    <activity
        android:name=".MapsActivity"
        android:label="@string/title_activity_maps">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

MapActivity.java

package com.barlificent.transport;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
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);
}


@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));
}

}

1 个答案:

答案 0 :(得分:0)

以上评论似乎是正确的。我遇到了同样的明显问题,当我进一步查看开发人员控制台时,您可能确实需要创建一个新的密钥或项目。由于您的密钥也可以限制在Android,iOS或某些Http Referers中使用,因此,您尝试使用的密钥可能会受到限制,从而无法与Android应用配合使用。就我而言,我创建了一个新项目,并为其创建了一个密钥。您也可以尝试更改当前API密钥的密钥限制,但对我来说,最好是为单独的活动使用单独的密钥。

  1. 登录到开发人员控制台: https://console.developers.google.com

  2. 创建一个项目,并确保在下拉菜单中选择了它 Google API徽标的右侧。

  3. 单击下拉菜单,位于GoogleAPIs徽标的左侧

  4. 选择API和服务>凭据

  5. 点击创建凭据> API密钥

  6. (可选)限制您的密钥。

相关问题