我需要lat很长的当前位置。虽然我的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);
}