2017-04-06 4 views
1

Ich versuche, das aktuelle Standortsymbol oben auf der Karte anzuzeigen. Aber wenn ich die App starte, sieht die Karte aus;Wird bei der Implementierung von SetMyLocationEnabled nicht 'Berechtigungsüberprüfung hinzufügen' angezeigt

Here is the map's screenshot

Der andere Fall ist, wenn ich diese Zeile geschrieben;

mGoogleMap.setMyLocationEnabled(true); 

Ich konnte keine 'Berechtigungsprüfung hinzufügen' Warnung erhalten wie; enter image description here

. Also habe ich diese Berechtigungen von Hand geschrieben.

Ich weiß nicht seinen richtigen Weg, das zu tun oder nicht. Hier ist mein vollständiger Code;

private Activity activity = this; 
    ActionBarDrawerToggle drawerToggle; 
    DrawerLayout drawerLayout; 
    RecyclerView recyclerMenu; 
    AdapterMenu obAdapter; 
    private Tracker mTracker; 
    GoogleMap mGoogleMap; 
    GoogleApiClient mGoogleApiClient; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_harita); 

     if (googleServicesAvailable()) { 

      initmap(); 
     } else { 
      //No google maps layout 
     } 


    } 


    public boolean googleServicesAvailable() { 
     GoogleApiAvailability api = GoogleApiAvailability.getInstance(); 
     int isAvailable = api.isGooglePlayServicesAvailable(this); 
     if (isAvailable == ConnectionResult.SUCCESS) 
      return true; 
     else if (api.isUserResolvableError(isAvailable)) { 
      Dialog dialog = api.getErrorDialog(this, isAvailable, 0); 
      dialog.show(); 
     } else 
      Toast.makeText(this, "Can't connect to play services", Toast.LENGTH_LONG).show(); 

     return false; 
    } 

    private void initmap() { 
     MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.fragment); 
     mapFragment.getMapAsync(this); 
    } 

    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     mGoogleMap = googleMap; 

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
      if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) 

       if (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
        return; 
       } 

     } 
     mGoogleMap.setMyLocationEnabled(true); 


    } 

    public void geoLocate(View view) throws IOException { 
     EditText searchtext = (EditText) findViewById(R.id.editAra); 
     String location = searchtext.getText().toString(); 

     Geocoder geocoder = new Geocoder(this); 
     List<Address> list = geocoder.getFromLocationName(location, 1); 
     Address address = list.get(0); 
     String locality = address.getLocality(); 

     Toast.makeText(this, locality, Toast.LENGTH_LONG).show(); 

     double latitude = address.getLatitude(); 
     double longitude = address.getLongitude(); 

     goToLocationZoom(latitude, longitude, 15); 
    } 

    private void goToLocationZoom(double latitude, double longitude, float zoom) { 

     LatLng LatLang = new LatLng(latitude, longitude); 
     CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LatLang, zoom); 
     mGoogleMap.moveCamera(update); 
    } 
} 
+0

Sie Screenshots hier anhängen. Es wird außerdem empfohlen, in Zukunft fehlerhafte Links zu vermeiden. –

+0

Können Sie Ihre Protokolle einfügen oder prüfen, ob Protokolle zu Berechtigungsrechten angezeigt werden? @DAG –

+0

Mögliches Duplikat von [Berechtigung für Android-Marshmallow-Anfragen?] (Http://stackoverflow.com/questions/) 33666071/android-marshmallow-request-permission) –

Antwort

1

Wenn der Benutzer für den Standort Berechtigung nicht erteilt hat, sollten Sie es explizit anfordern (nur für Android M erforderlich und höher). Dann sollten Sie onRequestPermissionsResult überschreiben und die Ergebnisse verwalten.

Hier haben Sie ein funktionierendes Beispiel für Sie fordern:

public class MainActivity extends FragmentActivity implements OnMapReadyCallback { 
private static final String[] LOCATION_PERMISSIONS = { 
     Manifest.permission.ACCESS_FINE_LOCATION, 
     Manifest.permission.ACCESS_COARSE_LOCATION 
}; 
private static final int LOCATION_PERMISSIONS_REQUEST_CODE = 1; 

private GoogleMap mGoogleMap; 

@Override 
protected void onCreate(final Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
} 

@Override 
public void onMapReady(final GoogleMap googleMap) { 
    this.mGoogleMap = googleMap; 
    if (hasGrantedLocationPermissions()) { 
     showCurrentLocationMapControl(); 
    } else { 
     requestForLocationPermissions(); 
    } 
} 

@Override 
public void onRequestPermissionsResult(final int requestCode, final @NonNull String[] permissions, final @NonNull int[] grantResults) { 
    if (requestCode == LOCATION_PERMISSIONS_REQUEST_CODE) { 
     if (grantResults.length == LOCATION_PERMISSIONS.length) { 
      for (final int grantResult : grantResults) { 
       if (PackageManager.PERMISSION_GRANTED != grantResult) { 
        return; 
       } 
      } 
      showCurrentLocationMapControl(); 
     } 
    } 
} 

private boolean hasGrantedLocationPermissions() { 
    return ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && 
      ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED; 
} 

private void requestForLocationPermissions() { 
    ActivityCompat.requestPermissions(this, LOCATION_PERMISSIONS, LOCATION_PERMISSIONS_REQUEST_CODE); 
} 

private void showCurrentLocationMapControl() { 
    if (null != this.mGoogleMap) { 
     this.mGoogleMap.setMyLocationEnabled(true); 
    } 
} 

} 

Vergessen Sie nicht benötigte Berechtigungen in Manifest-Datei zu definieren:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
+0

Vielen Dank, es hat das Problem gelöst !! – dizel

0
// Here, thisActivity is the current activity 
if (ContextCompat.checkSelfPermission(thisActivity,Manifest.permission.ACCESS_FINE_LOCATION) 
     != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(thisActivity,Manifest.permission. ACCESS_COARSE_LOCATION) 
     != PackageManager.PERMISSION_GRANTED) { 

    // Should we show an explanation? 
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, 
      Manifest.permission.READ_CONTACTS)) { 

     // Show an explanation to the user *asynchronously* -- don't block 
     // this thread waiting for the user's response! After the user 
     // sees the explanation, try again to request the permission. 

    } else { 

     // No explanation needed, we can request the permission. 

     ActivityCompat.requestPermissions(thisActivity, 
       new String[]{Manifest.permission.READ_CONTACTS}, 
       MY_PERMISSIONS_REQUEST_READ_CONTACTS); 

     // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an 
     // app-defined int constant. The callback method gets the 
     // result of the request. 
    } 
} 

Dann Erlaubnis Ergebnis

@Override 
public void onRequestPermissionsResult(int requestCode, 
     String permissions[], int[] grantResults) { 
    switch (requestCode) { 
     case MY_PERMISSIONS_REQUEST_READ_CONTACTS: { 
      // If request is cancelled, the result arrays are empty. 
      if (grantResults.length > 0 
       && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

       // permission was granted, yay! Do the 
       // contacts-related task you need to do. 

      } 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 
    } 
} 
Verwandte Themen