我正在尝试在片段中创建一个地图视图,通过单击导航栏选项显示该视图。因此,当我尝试使用地图视图打开我的片段时,我能够看到地图标记而不是地图。以下是我的代码。
片段代码:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_map, container, false);
MapView mapView = (MapView) v.findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
// Gets to GoogleMap from the MapView and does initialization stuff
mapView.getMapAsync(this);
//intiMap(mapView);
return v;
}
private void intiMap(MapView mapView) {
try {
mapView.getMapAsync(this);
}catch (Exception e){
Log.d("InitMap",e.getMessage());
}
}
public boolean googleServiceAvailable() {
GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
int isAvailable = apiAvailability.isGooglePlayServicesAvailable(this.getContext());
if(isAvailable == ConnectionResult.SUCCESS) {
return true;
} else if (apiAvailability.isUserResolvableError(isAvailable)){
Dialog dialog = apiAvailability.getErrorDialog(this.getActivity(),isAvailable,0);
dialog.show();
} else {
Toast.makeText(this.getContext(), "Cant connect to play services", Toast.LENGTH_LONG);
}
return false;
}
@Override
public void onMapReady(GoogleMap map) {
_googleMap = map;
_googleMap.getUiSettings().setMyLocationButtonEnabled(false);
if (ActivityCompat.checkSelfPermission(this.getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this.getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
_googleMap.setMyLocationEnabled(true);
_googleMap.getUiSettings().setZoomControlsEnabled(true);
MapsInitializer.initialize(this.getActivity());
// Updates the location and zoom of the MapView
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
map.animateCamera(cameraUpdate);
//_googleMap.addMarker(new MarkerOptions().position(new LatLng(0,0)));
}
主要活动代码:
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
Fragment fragment = null;
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
fragment = new MapFragment();
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
if(fragment != null){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
FragmentManager fm = this.getSupportFragmentManager();
for(int i = 0; i<fm.getBackStackEntryCount(); i++){
fm.popBackStack();
}
ft.replace(R.id.context_main,fragment);
ft.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
下面是我可以看到的地图:
由于地图已加载,我相信不存在任何与密钥或权限相关的问题。