1

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.

dda
  • 6,030
  • 2
  • 25
  • 34
Joseph
  • 41
  • 1
  • 2
  • 8
  • you need to refresh the activity(or call `Oncreate` again) after you Granted the permission – cpt. Sparrow Nov 03 '16 at 10:35
  • 1
    Have you override `onRequestPermissionsResult` ? see full demo https://stackoverflow.com/questions/38141523/directory-creation-not-working-in-marshmallow-android/38141778#38141778 – Sohail Zahid Nov 03 '16 at 10:37

0 Answers0