简体   繁体   English

Android Location API无法正确追踪使用者

[英]Android Location API not properly tracking the user

I have a background service which should track the user movement with his car, and send the data to my server. 我有一个后台服务,该服务应跟踪用户的汽车行驶情况,并将数据发送到我的服务器。 I have two variables for sending the location, either 60 seconds have passed or the user has moved 100 meters. 我有两个用于发送位置的变量,要么是60秒过去了,要么用户已经移动了100米。

On my service here is how I start listening to locations: 在我的服务中,这是我如何开始收听位置的方法:

mLocationRequest = LocationRequest.create();
mLocationRequest.setSmallestDisplacement(100);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(60 * 1000);

mLocationClient = new LocationClient(context, connectionCallbacks, onConnectionFailedListener);
mLocationClient.connect();

and the listeners: 和听众:

ConnectionCallbacks connectionCallbacks = new ConnectionCallbacks() {

    @Override
    public void onConnected(Bundle arg0) {

        mLocationClient.requestLocationUpdates(mLocationRequest, locationListenerAPI);

        //process the last location, if available
        Location loc = mLocationClient.getLastLocation();
        if (loc != null) {
            prepareLocation(loc);
        }
    }

    @Override
    public void onDisconnected() {
    }
};

LocationListener locationListenerAPI= new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {
        //A location has been read, process it
        prepareLocation(location);
    }

};

So, while the service is running and I driver around with the car, onLocationChanged only fires a few times, it does not take into consideration either the time or meters to fire a location read. 因此,当服务运行并且我开车开车时,onLocationChanged仅触发几次,因此未考虑触发位置读取的时间或仪表。 I have data connection and the GPS icon is visible on notification bar the whole time the service is running. 我已建立数据连接,并且在整个服务运行期间,通知栏上都显示了GPS图标。

Any ideas why is not working ? 任何想法为什么不起作用?

In the end, I just did it like this: 最后,我只是这样做:

I've setInterval(5000) and setFastestInterval(5000) and then, for each read location, if distance between them is bigger than 100m, I send the location to my server. 我已经设置了setInterval(5000)setFastestInterval(5000) ,然后,对于每个读取位置,如果它们之间的距离大于100m,我会将位置发送到我的服务器。 I gave up on using smallestDisplacement . 我放弃了使用smallestDisplacement This seem to work reliably. 这似乎工作可靠。

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

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