扑来 - 计算使用地理媒介和Google API驱动的里程

发布于 2025-01-31 20:31:03 字数 248 浏览 5 评论 0原文

我使用的是Google Directions API,最初是为了从/到方向获取详细信息, ie 从丹佛到芝加哥。

我需要做的是在开车时获得实际道路里程。我正在成功地通过地理定位器从插件中流坐标,但是,当我获得两个GPS点之间的距离时,这是距离,而不是道路里程。如果我接受足够的这些小读数并加起来距离,这应该有些准确。

这是最好的方法吗?

我也可以继续致电Google API,但是在与大量用户的长途旅行中,这变得非常昂贵。

I am using the Google Directions API, initially to get the detailed from/to directions, i.e. from let's say Denver to Chicago.

What I need to do is get actual road mileage as they are driving. I am successfully streaming the coordinates from the plugin by geolocator, however, when I get distance between two GPS points it's the distance, not road mileage. This should be somewhat accurate if I take enough of these little readings and add up the distance.

Is this the best way?

I could also keep calling Google API, but that gets very expensive on a long trip with lots of users.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹿港巷口少年归 2025-02-07 20:31:03

API在路由的leg s中提供距离。循环通过它们,将总距离加起来为米。

DirectionsService.init('API_KEY');

final directionsService = DirectionsService();

final request = DirectionsRequest(
  origin: 'New York',
  destination: 'San Francisco',
  travelMode: TravelMode.driving,
);

directionsService.route(request, (result, status) {
  if (status != DirectionsStatus.ok) {
    // TODO Handle the error.
    return;
  }
  var totalDistance = 0.0;
  result.routes?.first.legs?.forEach((element) {
    totalDistance += element.distance?.value?.toDouble() ?? 0.0;
  });
  print("This trip is $totalDistance meters.");
});

The API provides the distance in the route's Legs. Loop through them to add up the total distance in meters.

DirectionsService.init('API_KEY');

final directionsService = DirectionsService();

final request = DirectionsRequest(
  origin: 'New York',
  destination: 'San Francisco',
  travelMode: TravelMode.driving,
);

directionsService.route(request, (result, status) {
  if (status != DirectionsStatus.ok) {
    // TODO Handle the error.
    return;
  }
  var totalDistance = 0.0;
  result.routes?.first.legs?.forEach((element) {
    totalDistance += element.distance?.value?.toDouble() ?? 0.0;
  });
  print("This trip is $totalDistance meters.");
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文