我刚刚创建了一个新项目,并添加了API密钥,当我尝试运行该项目时,谷歌地图不会加载
<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.mapproblem.zakaria.maptestprobleme.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));
}
}
E/Google地图Android API:授权失败。请参阅https://developers.google.com/maps/documentation/android-api/start了解如何正确设置地图。E/谷歌地图Android API:在谷歌开发者控制台(https://console.developers.google.com)确保"谷歌地图Android API v2"已启用。确保存在以下Android密钥:API密钥:AIzaSgYrFTjKL2hp0SLKaTd_-1rVdwxOQy1VDu0安卓应用程序(;(:55:FA:14:99:28:28:D7:43F:19:62:E4:10:7C:7A:67:8F:AA:10:6C;com.mapproblem.zakaria.maptestprobleme
仅仅添加密钥是不够的。您需要启用地图Android API,如错误所示。
打开Google云平台,选择您的项目,然后转到"API和服务">库。搜索Android的地图SDK并启用它。
这应该可以修复错误。
你应该转到你的谷歌开发者控制台,为你的项目启用谷歌地图api,在顶部工具栏上选择你的项目,然后在面板部分选择Enabled APIs and services
,搜索谷歌地图api,然后启用