我正在尝试使用MapView在片段中显示Google地图。地图不会显示,也不会调用onMapReady。
片段:
import android.Manifest;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import java.util.Objects;
import butterknife.BindView;
import butterknife.ButterKnife;
public class MapFragment extends Fragment implements OnMapReadyCallback {
private GoogleMap map;
@BindView(R.id.map)
MapView mapView;
public MapFragment() {
// Required empty public constructor
}
public static MapFragment newInstance() {
return new MapFragment();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// mapView.onCreate(savedInstanceState);
}
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View mainView = inflater.inflate(R.layout.fragment_map, container, false);
MapsInitializer.initialize(Objects.requireNonNull(this.getActivity()));
ButterKnife.bind(this, mainView);
if(mapView!=null){
mapView.onCreate(savedInstanceState);
mapView.onResume();
mapView.getMapAsync(this);
}
return mainView;
}
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
LatLng coordinate = new LatLng(0, 0);
map.animateCamera(CameraUpdateFactory.newLatLngZoom(coordinate, 10));
}
@Override
public void onResume() {
super.onResume();
mapView.onResume();
}
@Override
public void onPause() {
super.onPause();
mapView.onPause();
}
@Override
public void onStart() {
super.onStart();
mapView.onStart();
}
@Override
public void onStop() {
super.onStop();
mapView.onStop();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
@Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
}
XML(R.layout.fragment_map):
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.MapFragment">
<com.google.android.gms.maps.MapView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map" />
</FrameLayout>
我已经根据官方文档生成了API密钥,已启用它并将其包含在清单文件中。
Google地图仍然没有显示。