【问题标题】:Too many points captured by onLocationChanged in android google mapsandroid谷歌地图中onLocationChanged捕获的点太多
【发布时间】:2024-01-01 05:12:01
【问题描述】:

我正在开发一个 android 应用程序,用于跟踪用户的当前位置(使用 GPS)并在位置发生变化时在谷歌地图上制作路线。

为了保存路线,我获取每个位置的纬度和经度并将其存储在列表中,单击“保存”按钮后,列表将转换为 JSON 并发送到服务器进行保存。它工作正常。

public void onLocationChanged(Location location) {
    showLocation.setText("" + location.getLatitude() + " , " + location.getLongitude());
    drawLine(location, latLng);
    latLongsForRoute.add(latLng);
    for (LatLng latLong : latLongsForRoute) {
        Log.i("Latitude is: ", "" + latLong.latitude);
        Log.i("Longitude is: ", "" + latLong.longitude);
    }
}

但是,问题是即使用户移动了 1 公里,它也会给我数千个纬度和经度,这是太多无法保存的数据。并在 10-15 公里长的路线上休息。

[
   {
      "latitude": 30.7084098,
      "longitude": 76.7030218,
      "zzCY": 1
   },
   {
      "latitude": 30.7084009,
      "longitude": 76.7030315,
      "zzCY": 1
   },
   {
      "latitude": 30.7083951,
      "longitude": 76.7030796,
      "zzCY": 1
   },
   {
      "latitude": 30.7084004,
      "longitude": 76.7029865,
      "zzCY": 1
   },
   {
      "latitude": 30.7083882,
      "longitude": 76.703018,
      "zzCY": 1
   },
   {
      "latitude": 30.708417,
      "longitude": 76.7030046,
      "zzCY": 1
   },
   {
      "latitude": 30.7083921,
      "longitude": 76.7030199,
      "zzCY": 1
   },
   {
      "latitude": 30.7084087,
      "longitude": 76.7029926,
      "zzCY": 1
   },
   {
      "latitude": 30.7083846,
      "longitude": 76.7029833,
      "zzCY": 1
   }
]

(这些分数是在15-20米的长度上获得的)

我怎样才能获得更少的数据,即更少的纬度经度,但准确度高且路线正确。

【问题讨论】:

    标签: android google-maps gps


    【解决方案1】:

    如果您使用的是 Google Play 服务位置 API,那么在创建 LocationRequest 对象时,您可以调整以下参数。

        //the rate at which app prefers to receive location updates
        LocationRequest.setInterval(10000);
        // the fastest rate with which the app can handle location updates
        LocationRequest.setFastestInterval(5000);
        //the smallest displacement in meters the user must move between location updates.
        LocationRequest.setSmallestDisplacement();
    

    【讨论】:

      最近更新 更多