我在这里有点挣扎,我正在使用 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
即使设备在同一个地方稳定,也会多次给出与过去位置的差异/距离约为 5-90 米的 lat-lng 位置。我错过了什么吗?
顺便说一句,这是返回我使用的两个 lat-lngs 距离的方法
private double getDistanceBetweenTwoLacation(Location origin, Location destination) {
return origin.distanceTo(destination);
}