计算手机的实际行驶距离 [英] calculate actual distance travelled by mobile

查看:252
本文介绍了计算手机的实际行驶距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to calculate actual distance traveled by mobile (iOS and Android). I know through google map API, we can find optimum route distance between 2 coordinates. But I want to calculate distance, actual path mobile(in vehicle) has covered.

我认识到的一种算法是在x秒后保存坐标,可以说在5或10秒后保存坐标,然后计算连续坐标之间的距离,总和将得出总距离.

One Algo I came to know is saving coordinates after x seconds, lets say after 5 or 10 seconds, and then calculate distance between consecutive coordinates, and there sum will give total distance.

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

I want to discuss better approach of its solution , Is there any better solution?

像Nike这样运行的应用程序和Uber之类的应用程序如何工作?

Edit : How Apps like Nike running app and Uber works?

推荐答案

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

------------------UPDATE----------------------

您的问题有两个要点.

There is two major point in your question.

1)获取电话坐标(此响应的第一部分已对其进行处理)

1) get the phone coordinates (which has been treated in the first part of this response)

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

2) Calculate the real distance between this two coordinates

恕我直言,演算可以通过网络服务来完成:仅基于计算 两个坐标之间的距离可能会导致错误的结果.

IMHO, calculus could be done by a web service: calculation based only on the distance between two coordinates can lead to really wrong result.

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

Here is an exemple of such a web service https://graphhopper.com/#directions-api

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

它基于流量(与许多此类工具一样) 因此,您必须注意坐标的顺序 因为它会带来错误的结果.

It's based on traffic flow (as many of this tools) So you have to be careful with the order of the coordinates because it can bring wrong result.

对于具有正确顺序的两个点的示例:

For exemple with two point in the right order:

这给出了很好的结果

但是,如果您输入的命令错误(坐标相同)

But if you give wrong order (with the same coordinates)

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

因此对于坐标ABCD(按时间顺序) 您需要这样做:

So for coordinates ABCD (in chrnological order) you need to do:

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

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

Graphhopper似乎可以进行离线距离计算

Graphhopper seems able to do offline distance calculus

这是iOS和Android上的库

Here are the lib on iOS and Android

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

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

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

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

You have to define how your app work. Foreground or background?

如其他回复所述,您必须每X秒获得一次用户位置.然后计算距离.

As said in other responses, you'll have to get the user position every X seconds. Then calculate the distance.

对于iOS:

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

它讨论了在后台运行应用程序时跟踪iOS上的用户位置.

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

然后您必须将点转换为

CLLocationDistance distance = [aCLLocationA distanceFromLocation:aCLLocationB];

(您也可以从Apple文档中检查)

You can also check this (from apple doc) https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html:

确保位置管理器的"pausesLocationUpdatesAutomatically"属性设置为是".当此属性设置为YES时,Core Location会在适当的时候暂停位置更新(并关闭位置硬件的电源),例如当用户不太可能移动时. (核心位置"在无法获取位置信息时也会暂停更新.)

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.)

为位置管理器的activityType属性分配一个适当的值.此属性中的值可帮助位置管理器确定何时可以安全地暂停位置更新.对于提供路口汽车导航的应用程序,将属性设置为CLActivityTypeAutomotiveNavigation会导致位置管理器仅在用户在一段时间内没有移动较大距离时才暂停事件.

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 确保您获得在道路上的位置.

CLActivityTypeAutomotiveNavigation insure you to get a position which is on a road.

对于Android:

  1. 您可以使用此项目: https://github.com/quentin7b/android-location-tracker
  1. You can use this project: https://github.com/quentin7b/android-location-tracker

这可以轻松地帮助您通过时间获得用户的位置 多亏了TrackerSettings对象

That can easily helps you to get the user's position thru time Thanks to the TrackerSettings object

TrackerSettings settings =
        new TrackerSettings()
            .setUseGPS(true)
            .setUseNetwork(true)
            .setUsePassive(true)
            .setTimeBetweenUpdates(30 * 60 * 1000)
            .setMetersBetweenUpdates(100);

  1. 要在Android上查找两点之间的距离,可以检查以下内容: 获取两个地理位置之间的距离
  1. To find the distance between two point on Android, you can check this: Get the distance between two geo points

两个操作系统

基于每X秒拾取一个位置,您必须减少拾取位置数据之间的时间以提高准确性.

Based on a position picked up every X second you have to reduce time between picking location data to improve accuracy.

要在道路上下文中计算距离,请在导航模式中设置位置管理器,此模式为您提供道路上的坐标.

As you want to calculate distance on a road context, setup the Location manager in navigation mode, this mode gives you coordinates that are on road.

最后

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

If you want to improve the accuracy of your distance calculus, you can use a google API: https://developers.google.com/maps/documentation/distance-matrix/intro

通过设置正确的 mode 参数:

By setting the right mode parameter:

可选参数

mode(默认为驾驶)—指定在计算距离时要使用的运输方式.有效值和其他请求详细信息在本文档的出行方式"部分中指定.

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.

这篇关于计算手机的实际行驶距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆