Google Map Doesn't Load Into My App Device
I am newbie to android development. I am learning on my own using internet and following tutorials. I am making an app which have 3 tabs, one is for location second is for camera a
Solution 1:
For the Android Version >= M
, the permission has to be asked at Runtime, which can not be done automatically. A general Runtime Permission
asking code. Use getActivity()
if inside Fragment
instead of this
.
@OverridepublicvoidonMapReady(GoogleMap googleMap) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if(ActivityCompat.checkSelfPermission
(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&&
ActivityCompat.checkSelfPermission
(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{
requestPermissions(newString[]{
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION
}, 1);
//return;
}
else{
googleMap.setMyLocationEnabled(true);
LatLngloc=newLatLng(31.492370,74.329060);
googleMap.addMarker(newMarkerOptions().position(loc).title("I am here"));
// For zooming automatically to the location of the markerCameraPositioncameraPosition=newCameraPosition.Builder().target(loc).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
}
Handle permission:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
case1:
if (grantResults[0] != PackageManager.PERMISSION_GRANTED){
// permission not granted
}
else {
// permission granted
}
break;
//default://super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
//}
}
Post a Comment for "Google Map Doesn't Load Into My App Device"