【发布时间】:2018-05-16 23:50:22
【问题描述】:
我想在我的应用程序中添加地图,并且我已按照 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));
}
}
【问题讨论】:
-
看来你的代码没问题。请仔细检查您的 api 密钥。
-
我同意你的看法@Andrii Omelchenko。可能是api键的问题。请搜索到 youtube。第一次也遇到了麻烦。但是看着我解决了我的问题......
-
@AndriiOmelchenko 我已经仔细检查了我的 api 密钥,但似乎没有任何效果。我什至使用了 google_maps_api.xml 中的链接,而不是手动获取。
-
您应该通过Google APIs Console为您的应用程序包生成它,而不是从
google_maps_api.xm获取。看看this 教程。看来你的问题是错误的关键。 -
@birukster741 - 你找到解决这个问题的方法了吗?
标签: android google-maps google-maps-api-3