简体   繁体   English

Google Play服务位置API-计数距离

[英]Google Play services location API - Counting distance

I've been following the https://developer.android.com/training/location/index.html tutorial. 我一直在关注https://developer.android.com/training/location/index.html教程。

Here's my class implementing the new Google's Location API: 这是我的类,用于实现新的Google的Location API:

public class GPSTracker implements LocationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
    private final Context context;
    private TextView distanceTextView;
    private GoogleApiClient mGoogleApiClient;
    private LocationRequest mLocationRequest;
    private Location mLastLocation;
    private double totalMeters = 0;

    public GPSTracker(Context context, TextView distanceTextView) {
        this.context = context;
        this.distanceTextView = distanceTextView;
        buildGoogleApiClient();
    }

    protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(context)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();

        mGoogleApiClient.connect();
    }

    @Override
    public void onLocationChanged(Location location) {
        double distance = mLastLocation.distanceTo(location);
        mLastLocation = location;
        totalMeters += distance;
        distanceTextView.setText(totalMeters + "");
    }

    @Override
    public void onConnected(Bundle bundle) {
        mLocationRequest = LocationRequest.create();
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setInterval(1000); // Update location every second

        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Toast.makeText(context, connectionResult.getErrorMessage(), Toast.LENGTH_SHORT).show();
    }
}

Everything seems to be working flawlessly, but when my phone is not moving at all the totalDistance is still rising by 0.2-0.3 meters every time an onLocationChanged method is executed. 一切似乎都可以正常工作,但是当我的手机完全不动时,每次执行onLocationChanged方法时,totalDistance仍会上升0.2-0.3米。

Any solution to prevent this situation is highly appreciated. 任何防止这种情况的解决方案都受到高度赞赏。

Don't forget that GPS is not pin point accurate. 不要忘记GPS不能精确定位。 There will often be off by a few meters so it's natural that there are incorrect readings like this. 通常会有几米的距离,所以很自然会出现这样的错误读数。 While the old LocationManager had a minDistance settings, LocationRequest unfortunately does not. 尽管旧的LocationManager具有minDistance设置,但不幸的是LocationRequest没有。

Several solutions: You can check the speed, if the speed is zero, that probably indicates that the user actually hasn't moved. 几种解决方案:您可以检查速度,如果速度为零,则可能表明用户实际上并没有移动。 You can also manually check if the old location is almost the same as the new one (in your case 0.3m seems a good threshold) and discard that reading. 您也可以手动检查旧位置是否与新位置几乎相同(在您的情况下,0.3m似乎是一个很好的阈值),然后丢弃该读数。 Last but not least you can set up an ActivityRecognitionApi activity listener to determine if the user is actually moving or still. 最后但并非最不重要的一点是,您可以设置ActivityRecognitionApi活动侦听器,以确定用户实际上是在移动还是在静止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM