Answer the question
In order to leave comments, you need to log in
How to correctly prescribe the check of permissions for geolocation?
I'm trying to figure out and run the code for an application that displays a map and my current location. In this case, all this is done through a request for permission to access geolocation, which must be approved.
I run the code on my device through the studio (on an API 28 device), and immediately after launch, the application crashes. I get an error in logcat:
java.lang.SecurityException: my location requires permission ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="####.########.mygooglemap">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<uses-library android:name="org.apache.http.legacy" android:required="false" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIza#################hmkAg" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
</manifest>
public class MainActivity extends AppCompatActivity implements LocationListener {
private GoogleMap myMap;
private ProgressDialog myProgress;
private static final String MYTAG = "MYTAG";
public static final int REQUEST_ID_ACCESS_COURSE_FINE_LOCATION = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myProgress = new ProgressDialog(this);
myProgress.setTitle("Map Loading ...");
myProgress.setMessage("Please wait...");
myProgress.setCancelable(true);
myProgress.show();
SupportMapFragment mapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.fragment);
mapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
onMyMapReady(googleMap);
}
});
}
private void onMyMapReady(GoogleMap googleMap) {
myMap = googleMap;
myMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
myProgress.dismiss();
askPermissionsAndShowMyLocation();
}
});
myMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
myMap.getUiSettings().setZoomControlsEnabled(true);
myMap.setMyLocationEnabled(true);
}
private void askPermissionsAndShowMyLocation() {
if (Build.VERSION.SDK_INT >= 23) {
int accessCoarsePermission =
ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION);
int accessFinePermission =
ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
if (accessCoarsePermission != PackageManager.PERMISSION_GRANTED
|| accessFinePermission != PackageManager.PERMISSION_GRANTED) {
String[] permissions = new String[] {
Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION
};
ActivityCompat.requestPermissions(this, permissions, REQUEST_ID_ACCESS_COURSE_FINE_LOCATION);
return;
}
}
this.showMyLocation();
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_ID_ACCESS_COURSE_FINE_LOCATION: {
if (grantResults.length > 1
&& grantResults[0] == PackageManager.PERMISSION_GRANTED
&& grantResults[1] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission granted!", Toast.LENGTH_LONG).show();
this.showMyLocation();
} else {
Toast.makeText(this, "Permission denied!", Toast.LENGTH_LONG).show();
}
break;
}
}
}
private String getEnabledLocationProvider() {
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, true);
boolean enabled = locationManager.isProviderEnabled(bestProvider);
if (!enabled) {
Toast.makeText(this, "No location provider enabled!", Toast.LENGTH_LONG).show();
Log.i(MYTAG, "No location provider enabled!");
return null;
}
return bestProvider;
}
private void showMyLocation() {
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
String locationProvider = this.getEnabledLocationProvider();
if (locationManager == null) {
return;
}
final long MIN_TIME_BW_UPDATES = 1000;
final float MIN_DISTANCE_CHANGE_FOR_UPDATES = 1;
Location myLocation = null;
try {
locationManager.requestLocationUpdates(
locationProvider,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES,
(LocationListener)this);
myLocation = locationManager.getLastKnownLocation(locationProvider);
} catch (SecurityException e) {
Toast.makeText(this, "Show My Location Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
Log.e(MYTAG, "Show My Location Error: " + e.getMessage());
e.printStackTrace();
return;
}
if (myLocation != null) {
LatLng latLng = new LatLng(myLocation.getLatitude(), myLocation.getLongitude());
myMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 13));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(latLng)
.zoom(15)
.bearing(90)
.tilt(40)
.build();
myMap.animateCamera(CameraUpdateFactory.newCameraPosition((cameraPosition)));
MarkerOptions option = new MarkerOptions();
option.title("My Location");
option.snippet("....");
option.position(latLng);
Marker currentMarker = myMap.addMarker(option);
currentMarker.showInfoWindow();
} else {
Toast.makeText(this, "Location not found!", Toast.LENGTH_LONG).show();
Log.i(MYTAG, "Location not found");
}
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
Answer the question
In order to leave comments, you need to log in
On Android, you need to think asynchronously.
and You
have 1. If there is no permission, then we ask
2. We request the position - we use it
And for the rest, as you wrote.
Coordinates can come in a minute.
Correctly .
If there is a permission, then we start the service, otherwise we ask for permission. The permission
hook is given with the start of the service.
Callback position received from service
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question