1

I'm new to Android Development. One of my labs for school is that I need to get the users location. I am following this link our teacher gave us: http://www.techotopia.com/index.php/Working_with_the_Google_Maps_Android_API_in_Android_Studio

The issue that I am having is that when I use the setMyLocationEnabled(true); it is not allowing it. It's asking for a permission but I do not see anything about checking for permissions anywhere in that link. How do I go about checking permissions for this? Any help would be greatly appreciated!! Also, since I am not at school I am not using a tablet, therefore using the emulator. Thanks.

Here is my code for the onCreate function:

   public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    if (mMap != null) {

        {
            mMap.setMyLocationEnabled(true);
        }


        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney").snippet("Consider yourself located"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));

    }
}
  • That article was written mostly in June 2014, which is quite some time ago. I think that you need the `ACCESS_FINE_LOCATION` permission to use `setMyLocationEnabled()`. – CommonsWare Mar 11 '16 at 01:05
  • Possible duplicate of [How can I use Google Maps and LocationManager to show current location on Android M?](http://stackoverflow.com/questions/34582370/how-can-i-use-google-maps-and-locationmanager-to-show-current-location-on-androi) – Daniel Nugent Mar 24 '16 at 06:34
  • Note that since this is a school project, just set the target sdk and compile sdk to 22 instead of 23, and that will "fix" it for you. It will still run on Marshmallow, and you won't need to implement the extra runtime checking of permissions, which is a requirement for api-23 (Android 6.0). See here: http://developer.android.com/training/permissions/requesting.html – Daniel Nugent Mar 24 '16 at 06:46

1 Answers1

3

Checking permissions likes below:

if (ActivityCompat.checkSelfPermission(MainActivity.this,android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) return;
googleMap.setMyLocationEnabled(true);