融合位置提供商 - 位移更新因排量无效

时间:2015-09-25 05:58:55

标签: android gps google-play-services fusedlocationproviderapi android-fusedlocation

我有一个Android应用程序,它将从后台跟踪位置。我使用融合位置api和以下配置

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

我只有在设备移动时才需要位置更新。有时一切都运行正常。但有时我会收到位置更新,即使设备处于静止状态。

为什么会出现这个问题?如果我设置位移,我是否需要设置间隔和最快间隔?此问题是否与Google Play服务版有关?

3 个答案:

答案 0 :(得分:0)

如果您的移动设备移动或不是您的给定间隔持续时间,它将每60秒更新一次您的位置。

答案 1 :(得分:0)

使用GoogleApiClient申请您的位置更新,例如像这样并使用Toasts来验证您的实现:

var checkInputs=function(){
    var isError=0;
      // Use an Each Loop to cycle through elements
      // Note that since these are classes, there could be more than three total
      // Consider using IDs
    $(".username-input, .password-input, .email-input").each(function(){
        if( $(this).val() == '' ){
            $(this).addClass('error');
            isError++;
        };
    });
      // If no errors, animate ".success-wrapper"
      // Note that you can chain .animate() and that the next effect won't occur
      //   until the last ends, unless the "queue" option is set to false
    if( isError == 0 ){
        $(".success-wrapper").animate({bottom:"0%"},"slow").animate({bottom:"-100%"},"slow");
    };
};

$(".btn").click(checkInputs);
$(".close").click(function(){
    $(".success-wrapper").animate({bottom:"-100%"},{duration:"slow",queue:"false");
});

在这里,您可以使用变量 if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { float LOCATION_REFRESH_DISTANCE = 5000; long LOCATION_REFRESH_TIME = 0; locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, LOCATION_REFRESH_TIME, LOCATION_REFRESH_DISTANCE, mlocationListener); mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); if(mLastLocation!=null){ Toast.makeText(getApplicationContext(), "YES! mLastLocation!=null", Toast.LENGTH_SHORT).show(); double latitude = mLastLocation.getLatitude(); double longitude = mLastLocation.getLongitude(); Toast.makeText(getApplicationContext(), "Latitude = " + latitude + "\nLongitude = " + longitude, Toast.LENGTH_SHORT).show(); } } LOCATION_REFRESH_DISTANCE(以毫秒为单位)参数化您的位置请求

您当然需要GoogleApiClient LOCATION_REFRESH_TIME需要获取最后已知/当前位置的LocationListener对象mLocationListener和位置对象(我称之为mGoogleApiClient)存储位置它。例如:

mLastLocation

答案 2 :(得分:0)

您可能需要的是FusedLocationProviderApi的PendingIntent version。此方法适用于后台用例,更具体地说,适用于接收位置更新,即使应用程序已被系统杀死也是如此。

相关问题