Issue
Google Map not showing in marshmallow device when build an app at API level greater than 23 but it showing correctly below marshmallow version.I have used MapView.
Actally i am getting Current lat lang (0,0) in Marshmallow that's why i am facing issue in marshmallow.For getting current lat lang i am using this line:-
private void getCurrentLatLong() {
try {
gps = new GPSTracker(getActivity());
if (gps.canGetLocation()) {
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
System.out.println("latitude print==" + latitude);
lat = String.valueOf(latitude);
lng = String.valueOf(longitude);
System.out.println("lat long check====" + "lati :" + lat + " -- lng :" + lng);
if (lat.equalsIgnoreCase("0.0") || lng.equalsIgnoreCase("0.0")) {
getCurrentLatLong();
} else {
mMapView.onResume();
setMap();
}
} else {
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
gps.showSettingsAlert();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
Solution
first this permission must have
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
then, runtime permission is required when you want to use location map
if (ContextCompat.checkSelfPermission(Maps_Activity.this,Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(Maps_Activity.this, Manifest.permission.ACCESS_FINE_LOCATION)) {
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(Maps_Activity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},PERMISSION_REQUEST_CODE);
}
}
else{
//load your map here
}
And the overrride onRequestPermissionsResult.
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay!
// load your map here also
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
Answered By - ZeroOne
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.