Location Button Doesn't Work Googlemaps V2
I'm trying to get my current location with the location button on Google Maps but I have some problems. With the new runtime permissions (API level 23), I don't know if i am doing
Solution 1:
In api level 23, you need check permissions programmatically. You are checking the location permission and if it's granted, setting the location enabled. You can ask for permission if it is not granted.
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
} else {
ActivityCompat.requestPermissions(MapActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},REQUEST_CODE);
}
Then you can handle the result by overriding this method.
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//do something
} else {
finish();
}
break;
}
}
}
Also you can give permission from the emulator's settings for your application.
Let me know after trying. Good luck.
EDIT: You need to implement a clicklistener to mylocation button.
final GoogleMap mMap = map;
map.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() {
@Override
public boolean onMyLocationButtonClick() {
LatLng loc = new LatLng(mMap.getMyLocation().getLatitude(),mMap.getMyLocation().getLongitude());
mMap.moveCamera(CameraUpdateFactory.newLatLng(loc));
return true;
}
});
Post a Comment for "Location Button Doesn't Work Googlemaps V2"