Turn on current location Android Studio

I am developing a App Android that contains a map and that takes my location, but in some devices it does not activate this function, for it to work you need to click the button Local for example Samsung J5. How can I make this button activate automatically when opening the map?

Note: it's a simple map, just a few markers and my location.

From Android 6.0 I already get the permission to open the map in the demarcated position, only my location does not. I put link with the image of what I want it to turn on automatically. Remembering that when I open the map it asks permission but does not activate the location only the map.

Enter image description here

This is my map:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
static final int MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(map);
    mapFragment.getMapAsync(this);


    ActivityCompat.requestPermissions(MapsActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
            MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);

}


@Override
public void onRequestPermissionsResult(
        int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(MapsActivity.this,
                        "Atenção! Ative o Local...",Toast.LENGTH_LONG).show();

            } else {
                Toast.makeText(MapsActivity.this,
                        "Permissão de Localização Negada, ...:(",Toast.LENGTH_LONG).show();
            }
            return;
        }
    }
}


@Override
public void onMapReady(GoogleMap map) {
    // Tipo do Mapa
    map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
  if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    //Minha Posição Atual
    map.setMyLocationEnabled(true);

    // Posicao inicial onde o mapa abre
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(
            new LatLng(-27.3246787, -53.4387937), (float) 14.5));

    LatLng sydney = new LatLng(-27.356857, -53.396316);

    map.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));

    map.moveCamera(CameraUpdateFactory.newLatLng(sydney));


    LatLng fw = new LatLng(-27.369282, -53.402667);
    map.addMarker(new MarkerOptions()
            .position(fw)
            .title("Marker in Sydney")
            .snippet("Population: 4,137,400"));
    }
}
Author: Tmc, 2016-11-02

1 answers

So, You cannot enable the "Fine Location" of your user's smartphone. This goes against good Android practices, where the software cannot interact with the hardware without the user actively allowing it (by clicking a button, for example)

From what I'm seeing from your app the 'Fine Location' is pretty important, right?

This method here solves your problem in an elegant way:

private void createNoGpsDialog(){
        DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which) {
                    case DialogInterface.BUTTON_POSITIVE:
                        Intent callGPSSettingIntent = new Intent(
                                android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        startActivity(callGPSSettingIntent);
                        break;
                }
            }
        };

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        mNoGpsDialog = builder.setMessage("Por favor ative seu GPS para usar esse aplicativo.")
                .setPositiveButton("Ativar", dialogClickListener)
                .create();
        mNoGpsDialog.show();

    }

Basically you create a Dialog and ask your user to activate his GPS. Alas, when it clicks on " Enable "it will go to the" Settings " of the phone and can activate the GPS.

Put this inside your onCreate or your onMapReady and it will work nice.

 4
Author: Leandro Borges Ferreira, 2016-11-03 12:46:36