Мне нужен lat long текущего местоположения. Я получаю lat long на эмуляторе, но не на реальных устройствах, хотя мой gps включен.
Вот так я получаю текущее местоположение.
public void getLocation() {
try {
if (ContextCompat.checkSelfPermission(listener, android.Manifest.permission.ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) ==
PackageManager.PERMISSION_GRANTED) {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 5, this);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Logger.log("LastKnownLocation"+location);
gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
Logger.log("GPSEnabled"+gps_enabled);
} else {
ActivityCompat.requestPermissions(this, new String[] {
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION },
1);
}
}
catch(SecurityException e) {
e.printStackTrace();
}
}
LocationListener listener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Toast.makeText(this, "Current location"+ location.getLatitude() + ", " + location.getLongitude(), Toast.LENGTH_SHORT).show();
Logger.log("Current location"+ location.getLatitude() + ", " + location.getLongitude());
drawMarker(location);
}