获得一条好的"路径"来自Android服务

时间:2015-08-19 19:34:47

标签: java android google-maps

我正在创建一个可以在地图上绘制用户路径的应用。这一切都很好。但是,当应用程序在后台时,我还想继续收集路径(当用户请求时)。这也工作得很好,但偶尔我会得到"故障"。请参阅此地图以获取示例:

GoogleMap

我相信正在发生的事情是,当我开始服务时,我将获得最后一个已知位置"而不是当前的位置。到目前为止,我尝试了几种没有太多运气的技术。我所拥有的其他一些想法:

  • 将GoogleApiClient从活动传递到已连接的服务(这可能吗?)
  • 忽略第一个" X"服务中的位置更新(" X"需要多大?)
  • 清理路径,删除"坏"位置(我需要什么样的算法来弄清楚什么是"坏"位置......在附图中很容易看到,但我不知道如何在代码中做到这一点)。

有关处理此方案的最佳方法的任何其他建议吗?

以下是一些代码,可以让您了解当前的功能:

Activity.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    setUpMapIfNeeded();  
    buildGoogleApiClient();  
    createLocationRequest();
    ...
}

private void buildGoogleApiClient() {
    googleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build();
}

private void createLocationRequest() {
    locationRequest = new LocationRequest();
    locationRequest.setInterval(Utils.LOCATION_UPDATE_MS);
    locationRequest.setFastestInterval(Utils.FASTEST_LOCATION_UPDATE_MS);
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

@Override
public void onConnected(Bundle bundle) {
    LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
    location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
    Log.v("Connected", String.format("Location:  %f, %f, %f", location.getLatitude(), location.getLongitude(), location.getAccuracy()));
    if (location != null) {
        updateMap();
    }
}

@Override
protected void onPause() {
    super.onPause();
    LocalBroadcastManager.getInstance(this).unregisterReceiver(pathReceiver);
    LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this);
    if (isNavigatingTo()) {
        Preferences.save(Preferences.PREFERENCE_NAVIGATE_TO, navigateToWaypoint.getId());
    } else {
        Preferences.remove(Preferences.PREFERENCE_NAVIGATE_TO);
    }
    if(isRecordingTrack()){
        // Start the PathSevice to continue recording the track.
        Intent pathServiceIntent = new Intent(this, PathService.class);
        pathServiceIntent.putExtra(Utils.PATH_EXTRA, path);
        startService(pathServiceIntent);
    }
}
...

PathService.java:

@Override
public void onCreate() {
    buildGoogleApiClient();
    createLocationRequest();
    googleApiClient.connect();
    super.onCreate();
}

private void buildGoogleApiClient() {
    googleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build();
}

private void createLocationRequest() {
    locationRequest = new LocationRequest();
    locationRequest.setInterval(Utils.LOCATION_UPDATE_MS);
    locationRequest.setFastestInterval(Utils.FASTEST_LOCATION_UPDATE_MS);
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

@Override
public void onConnected(Bundle bundle) {
    LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
}

@Override
public void onLocationChanged(Location location) {
    path.addPoint(location.getLatitude(), location.getLongitude());
}

1 个答案:

答案 0 :(得分:0)

所以解决方案就在我的问题中...因为我似乎正在添加"最后一个已知位置"从服务返回活动时(请参阅上面的Activity类中的onConnected)。

我现在只在onConnected中启动位置请求。