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.
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"));
}
}