java - 计算手机实际行驶距离

标签 java android ios objective-c google-maps

我想计算移动设备(iOS 和 Android)的实际行驶距离。我知道通过google map API,我们可以找到2个坐标之间的最佳路线距离。但是我想计算距离,移动(在车辆中)已经覆盖的实际路径。

我知道的一个算法是在 x 秒后保存坐标,比如说 5 或 10 秒后,然后计算连续坐标之间的距离,总和将给出总距离。

我想讨论其解决方案的更好方法,有没有更好的解决方案?

编辑:像耐克运行应用和优步这样的应用是如何工作的?

最佳答案

------------------更新------------------ -

您的问题有两个要点。

1) 获取手机坐标(已在此响应的第一部分处理)

2) 计算这两个坐标之间的真实距离

恕我直言,微积分可以通过网络服务完成:仅基于计算 两个坐标之间的距离会导致非常错误的结果。

以下是此类 Web 服务的示例 https://graphhopper.com/#directions-api

演示应用程序:https://graphhopper.com/api/1/examples/

它基于流量(与许多此类工具一样) 所以你必须小心坐标的顺序 因为它会带来错误的结果。

以正确顺序的两点为例: enter image description here

这给出了一个很好的结果

但是如果你给出错误的顺序(坐标相同) enter image description here

对于相同的坐标,可能会导致截然不同的结果。

所以对于坐标 ABCD(按时间顺序) 你需要做的:

A->B B->C C->D

Graphhopper 似乎能够进行离线距离计算

这是 iOS 和 Android 上的库

https://github.com/graphhopper/graphhopper-ios/

https://github.com/graphhopper/graphhopper/tree/master/android

------------------------------------------ --------

您必须定义应用的工作方式。前景还是背景?

正如其他回复中所说,您必须每 X 秒获取一次用户位置。然后计算距离。

对于 iOS:

  1. 您可以使用本网站上的信息:http://mobileoop.com/

它谈到了当应用处于后台时在 iOS 上跟踪用户位置。

  1. 这里是github:https://github.com/voyage11/Location

那么你必须转换点感谢

CLLocationDistance distance = [aCLLocationA distanceFromLocation:aCLLocationB];

你也可以检查这个(来自苹果文档)https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html :

Make sure the location manager’s pausesLocationUpdatesAutomatically property is set to YES. When this property is set to YES, Core Location pauses location updates (and powers down the location hardware) whenever it makes sense to do so, such as when the user is unlikely to be moving anyway. (Core Location also pauses updates when it can’t obtain a location fix.)

Assign an appropriate value to the location manager’s activityType property. The value in this property helps the location manager determine when it is safe to pause location updates. For an app that provides turn-by-turn automobile navigation, setting the property to CLActivityTypeAutomotiveNavigation causes the location manager to pause events only when the user does not move a significant distance over a period of time.

CLActivityTypeAutomotiveNavigation 确保您获得道路上的位置。

对于 Android:

  1. 您可以使用这个项目: https://github.com/quentin7b/android-location-tracker

这可以很容易地帮助您通过时间获得用户的位置 感谢 TrackerSettings 对象

TrackerSettings settings =
        new TrackerSettings()
            .setUseGPS(true)
            .setUseNetwork(true)
            .setUsePassive(true)
            .setTimeBetweenUpdates(30 * 60 * 1000)
            .setMetersBetweenUpdates(100);
  1. 要在 Android 上查找两点之间的距离,您可以检查以下内容: Get the distance between two geo points

两个操作系统

根据每 X 秒拾取的位置,您必须缩短拾取位置数据之间的时间以提高准确性。

当您想根据道路环境计算距离时,请将位置管理器设置为导航模式,此模式会为您提供道路上的坐标。

终于

如果您想提高距离演算的准确性, 您可以使用谷歌 API: https://developers.google.com/maps/documentation/distance-matrix/intro

通过设置正确的mode参数:

Optional parameters

mode (defaults to driving) — Specifies the mode of transport to use when calculating distance. Valid values and other request details are specified in the Travel Modes section of this document.

关于java - 计算手机实际行驶距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34551318/

相关文章:

java - 单词在句子中的共现

Java接口(interface)实现对象?

Android - 抑制 native In Call Screen 并替换为第 3 方?

Android登录并下载csv文件

java - 如何在 Android 中为 fragment 添加标题

iphone - 如何将对象移回我的 View

ios - 按变量对自定义对象数组进行排序

java - jsp中输入类型date的值设置

java - 安卓/Java : Check if arraylist is empty and then launch another activity

ios - 从服务器获取 JSON 数据后,UICollectionViewCell 不可点击