Connect to Current Location Android Studio

1

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, so that it works you have to click on the Local button, for example Samsung J5 . How can I make this button to turn on automatically when I open the map?

  

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

From Android 6.0 I already have the permission to open the map in the demarcated position, only my location does not. I linked with the image of what I want it to call automatically. Remembering that when I open the map it asks for permission but does not activate the location just 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"));
    }
}
    
asked by anonymous 02.11.2016 / 18:05

1 answer

4

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

From what I'm seeing from your app to 'Fine Location' it's 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. Then, when he clicks "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 great.

    
03.11.2016 / 13:46