【发布时间】:2020-11-07 06:11:37
【问题描述】:
我有一个 onCreate 活动,它计算您的位置与附近的事件之间的距离,我使用 lastKnownLocation 获取当前设备位置并将其标记在谷歌地图上,但我需要它在计算距离的方法之外写出经度和纬度。
我使用 LocationManager 来获取粗略的坐标,但这些坐标不够准确,而且距离不到半英里的地方有 50 英里的距离。我目前有它,所以会覆盖从 LocationManager 获得的经度和纬度,但它没有。
我也尝试过使用 LocationRequest,但没有帮助。
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setInterval(60000);
locationRequest.setFastestInterval(5000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationCallback locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult){
if(locationRequest==null){
return;
}
for(Location location : locationResult.getLocations()) {
if (location != null){
userLat=location.getLatitude();
userLng=location.getLongitude();
}
}
}
};
LocationServices.getFusedLocationProviderClient(EventLocator.this).requestLocationUpdates(locationRequest,locationCallback, null);
LocationServices.getFusedLocationProviderClient(EventLocator.this).getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if(location!=null){
double longitude=location.getLongitude();
double latitude=location.getLatitude();
userLng=longitude;
userLat=latitude;
}
}
});
所有权限都是正确的,正如我所说,我使用 getLastLocation() 来放置标记。
【问题讨论】:
-
您获得的某些时间位置不正确。您可以在计算距离之前检查准确性。 Location.getaccuracy()