更改位置引擎请求间隔

时间:2019-04-30 15:36:18

标签: java android mapbox

我正在 App 中使用 mapbox 。在为我的locationEngine生成请求时,我经过了 20000 毫秒,但是在某个特定的时刻,我需要更改该间隔并每隔 5000 毫秒通知一次位置更改。

是否可以更改该间隔或为我的locationEngine建立另一个请求?

这是我尝试过的:

这是我初始化位置引擎的地方

   @SuppressLint("MissingPermission")
    private void initializeLocationEngine(int interval) {
        locationEngine =LocationEngineProvider.getBestLocationEngine(parentActivity);
        LocationEngineRequest request = buildEngineRequest(interval);
        locationEngine.requestLocationUpdates(request, callback, null);
        mapboxNavigation.setLocationEngine(locationEngine);
    }

当我想更改间隔时,我调用此方法:

  private void changeLocationEngineInterval(int interval) {
        removeLocationEngineListener();
        LocationEngineRequest request = buildEngineRequest(interval);
        if (ActivityCompat.checkSelfPermission(parentActivity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(parentActivity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        locationEngine.requestLocationUpdates(request, callback, null);
        mapboxNavigation.setLocationEngine(locationEngine);
    }

1 个答案:

答案 0 :(得分:0)

时间间隔是对象构造函数的参数,因此您需要销毁第一个LocationEngineRequest并使用要使用的新时间间隔创建另一个。

致谢

编辑

我在这里放了一些示例代码。将对象作为全局变量放置是一种好习惯,这样您就可以从这两种方法中进行访问,并且可以覆盖它而无需删除并重新创建。

public class ExampleActivity extends AppCompatActivity {

LocationEngineRequest request;

@SuppressLint("MissingPermission")
private void initializeLocationEngine(int interval) {
    locationEngine =LocationEngineProvider.getBestLocationEngine(parentActivity);
    request = buildEngineRequest(interval);
    locationEngine.requestLocationUpdates(request, callback, null);
    mapboxNavigation.setLocationEngine(locationEngine);
}

private void changeLocationEngineInterval(int interval) {
    //removeLocationEngineListener(); <-- I don't know what u doing here, but maybe you don't need
    request = buildEngineRequest(interval);
    if (ActivityCompat.checkSelfPermission(parentActivity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(parentActivity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    locationEngine.requestLocationUpdates(request, callback, null);
    mapboxNavigation.setLocationEngine(locationEngine);
}

希望这对您有所帮助。