I want to display the location on a map. But every time I reinstall the app, it will ask for location permission twice, the first time I launch the app.
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng googleHeadquater = new LatLng(37.421949, -122.084261);
mMap.addMarker(new MarkerOptions().position(googleHeadquater).title("Google Headquater"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(googleHeadquater, 16));
//mMap.setOnMyLocationButtonClickListener(this);
enableMyLocation();
}
private void enableMyLocation() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE);
} else if (mMap != null) {
// Access to the location
mMap.setMyLocationEnabled(true);
}
}
Manifest
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Get location:
protected synchronized void builGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
@Override
public void onConnected(Bundle bundle ) {
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setFastestInterval(3000); // 3 second
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE);
} else {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
}
And after asking for permission, it doesn't update the location unless I go to another activity and come back.