Android 融合位置 API 在 onLocationChanged 中给出了不准确的位置
Android Fused Location API gives inaccurate Location in onLocationChanged
我在这里有点挣扎,我正在使用 Fused API 来获取位置更新。我的目的是在用户行走时在地图上绘制一条路径。
我已经实现如下:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.map_layout, container, false);
// some other initialization
//....
//
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(mContext)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
return view;
}
然后我按照下面的方法开始这些东西
private void startReceivingLocationUpdates() {
if (checkGPSPermission() && mGoogleApiClient.isConnected() && !isReceivingLocationUpdates) {
LocationRequest locationRequest = new LocationRequest();
locationRequest.setInterval(5000);
locationRequest.setFastestInterval(5000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
locationRequest, this);
isReceivingLocationUpdates = true;
}
}
& 我收到位置更新
@Override
public void onLocationChanged(Location location) {
if(getDistanceBetweenTwoLacation(mCoordinates.get(mCoordinates.size() - 1), location) > 3d) {
for(int i= 0; i < mCoordinates.size(); i++) {
Location locationTemp = mCoordinates.get(i);
Log.d(TAG, "i => " + i + " Lat => " + String.valueOf(locationTemp.getLatitude()) + " Lng => " + String.valueOf(locationTemp.getLongitude()));
}
if(mCoordinates.size() > 0)
Log.d(TAG, "lat difference is => " + getDistanceBetweenTwoLacation(mCoordinates.get(mCoordinates.size() - 1), location));
mCoordinates.add(location);
}
}
现在的问题是,即使设备稳定在同一个地方,onLocationChanged
也会多次给出与过去位置的 difference/distance 大约 5-90 米的经纬度位置。我错过了什么吗?
顺便说一句,这是 returns 我使用的两个经纬度距离的方法
private double getDistanceBetweenTwoLacation(Location origin, Location destination) {
return origin.distanceTo(destination);
}
不准确(和 moving/drifting/jumping)在室内时 GPS 定位很常见。没有清晰的天空视野,就不可能进行准确的 GPS 定位。在您的 Location
对象中,有一个 getAccuracy()
方法,其中 returns 一个浮点数。该值是以米为单位的定位精度,置信度为 68%(1 个标准偏差),代表圆的半径。
在室内时,您可能会看到 20、30 甚至 50 米的精度值,而纬度和经度会在该距离内跳来跳去。一旦在室外,准确度值应该会下降到 10 米以下,通常是 5 米以下,并且您的位置跳动的频率也会大大降低。
tl;dr:GPS 无法在室内提供准确的结果。
我在这里有点挣扎,我正在使用 Fused API 来获取位置更新。我的目的是在用户行走时在地图上绘制一条路径。
我已经实现如下:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.map_layout, container, false);
// some other initialization
//....
//
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(mContext)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
return view;
}
然后我按照下面的方法开始这些东西
private void startReceivingLocationUpdates() {
if (checkGPSPermission() && mGoogleApiClient.isConnected() && !isReceivingLocationUpdates) {
LocationRequest locationRequest = new LocationRequest();
locationRequest.setInterval(5000);
locationRequest.setFastestInterval(5000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
locationRequest, this);
isReceivingLocationUpdates = true;
}
}
& 我收到位置更新
@Override
public void onLocationChanged(Location location) {
if(getDistanceBetweenTwoLacation(mCoordinates.get(mCoordinates.size() - 1), location) > 3d) {
for(int i= 0; i < mCoordinates.size(); i++) {
Location locationTemp = mCoordinates.get(i);
Log.d(TAG, "i => " + i + " Lat => " + String.valueOf(locationTemp.getLatitude()) + " Lng => " + String.valueOf(locationTemp.getLongitude()));
}
if(mCoordinates.size() > 0)
Log.d(TAG, "lat difference is => " + getDistanceBetweenTwoLacation(mCoordinates.get(mCoordinates.size() - 1), location));
mCoordinates.add(location);
}
}
现在的问题是,即使设备稳定在同一个地方,onLocationChanged
也会多次给出与过去位置的 difference/distance 大约 5-90 米的经纬度位置。我错过了什么吗?
顺便说一句,这是 returns 我使用的两个经纬度距离的方法
private double getDistanceBetweenTwoLacation(Location origin, Location destination) {
return origin.distanceTo(destination);
}
不准确(和 moving/drifting/jumping)在室内时 GPS 定位很常见。没有清晰的天空视野,就不可能进行准确的 GPS 定位。在您的 Location
对象中,有一个 getAccuracy()
方法,其中 returns 一个浮点数。该值是以米为单位的定位精度,置信度为 68%(1 个标准偏差),代表圆的半径。
在室内时,您可能会看到 20、30 甚至 50 米的精度值,而纬度和经度会在该距离内跳来跳去。一旦在室外,准确度值应该会下降到 10 米以下,通常是 5 米以下,并且您的位置跳动的频率也会大大降低。
tl;dr:GPS 无法在室内提供准确的结果。