ios 检测用户是否在汽车中移动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17869659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detecting if a user is moving in a car
提问by Sam Spencer
NOTICE: This question was originally posted before Apple introduced motion-detection hardware and associated APIs in the iOS SDK. Answers to this question, however, remain relevant.
注意:这个问题最初是在 Apple 在 iOS SDK 中引入运动检测硬件和相关 API 之前发布的。然而,这个问题的答案仍然相关。
I'm creating an iPhone iOS app which involves tracking a user's running and / or walking. It is very important that the recorded results of the users runs and walks remain honest. I need a way to catch a user who may be cheating (or accidentally have left the tracker on) when using a car.
我正在创建一个 iPhone iOS 应用程序,它涉及跟踪用户的跑步和/或步行。用户跑步和步行的记录结果保持真实是非常重要的。我需要一种方法来捕捉可能在使用汽车时作弊(或不小心打开跟踪器)的用户。
To check if the user is driving or riding in a car I first thought of these two checks, however neither can reallydetermine if the user is in a car or not (to a point).
要检查用户是否在开车或乘车,我首先想到了这两个检查,但是两者都不能真正确定用户是否在汽车中(在一定程度上)。
Check the user's current speed in the following method. If the user is traveling faster than 20-ish MPH, then I could assume that the user is in a car:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { CLLocation *recentLocation = [locations lastObject]; recentLocation.speed; //If speed is over 20 MPH, assume the user is not on their feet.
However, I'm not sure if this is really a good check because people have been recorded to go faster than that before. Is this a good check or should I use something else?
Determine how fast the user is accelerating using the Accelerometer and Motion APIs provided with the Core Motion Framework. If the user accelerates over a certain rate, then I could assume the user is traveling in a vehicle.
通过以下方法查看用户当前的速度。如果用户的行驶速度超过 20 英里/小时,那么我可以假设用户在汽车中:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { CLLocation *recentLocation = [locations lastObject]; recentLocation.speed; //If speed is over 20 MPH, assume the user is not on their feet.
然而,我不确定这是否真的是一个很好的检查,因为人们被记录为比以前更快。这是一个很好的检查还是我应该使用其他东西?
使用Core Motion Framework提供的 Accelerometer 和 Motion API 确定用户的加速速度。如果用户加速超过一定的速度,那么我可以假设用户正在乘车旅行。
Are these assumptions correct? I guess my real question is this: Is there any better way to detect if the user is moving in a vehicle - if so how?. And if not, then are these checks suitable for this case or would this just be annoying to some users who are actually thatfast? Is CoreMotion the proper API to do this with?
这些假设正确吗?我想我真正的问题是:有没有更好的方法来检测用户是否在车辆中移动 - 如果是,如何检测?. 如果不是,那么这些检查是否适用于这种情况,或者这是否会让一些实际上那么快的用户感到烦恼?CoreMotion 是正确的 API 吗?
EDIT: The new iPhones 5S M7 Coprocessor provides more accurate movement detection. Could anyone explain how to use the new M7 APIs?
编辑:新的 iPhones 5S M7 协处理器提供更准确的运动检测。谁能解释一下如何使用新的 M7 API?
采纳答案by rickster
All the advice about the wisdom (or lack thereof) in guessing about motion states from location data still applies. But regarding your update about Core Motion and M7...
关于从位置数据猜测运动状态的智慧(或缺乏智慧)的所有建议仍然适用。但是关于你关于 Core Motion 和 M7 的更新......
Yes, you can use Core Motion on devices with an M7, M8, M9, etc motion coprocessor(*) to get an indication of whether the user might be driving.
是的,您可以在配备 M7、M8、M9 等运动协处理器 (*) 的设备上使用 Core Motion 来指示用户是否可能正在驾驶。
- Create a
CMMotionActivityManager
object (after using its class methodisActivityAvailable
to determine whether you you have M7(+) features), and either start activity updates or query it for recent activities. - Check the returned
CMMotionActivity
objects'automotive
property to see if iOS thinks the user is/was in a car. - There's no step three.
- 创建一个
CMMotionActivityManager
对象(在使用其类方法isActivityAvailable
确定您是否具有 M7(+) 功能之后),然后启动活动更新或查询最近的活动。 - 检查返回
CMMotionActivity
对象的automotive
属性以查看 iOS 是否认为用户在/曾经在汽车中。 - 没有第三步。
Like the GPS inferences, though, you should still take this information with a grain of salt. CoreMotion APIs give you iOS' best guess as to the user's activity, but there's no guarantee it's 100% accurate. (For example, I'm not sure if riding a train might count as automotive
. Also note that the different activity types are not mutually exclusive.) It's better for your app to check for the activity types you're interested in than to try to exclude the ones you don't want.
但是,就像 GPS 推断一样,您仍然应该保留这些信息。CoreMotion API 为您提供 iOS 对用户活动的最佳猜测,但不能保证它是 100% 准确的。(例如,我不确定乘坐火车是否可以算作automotive
。另请注意,不同的活动类型并不是相互排斥的。)让您的应用检查您感兴趣的活动类型比尝试检查要好排除那些你不想要的。
(*) M7 devices are those with the A7 SoC: iPhone 5s, iPad Air, iPad mini 2. M8 is A8, M9 is A9, etc. In short, every iOS device introduced since Fall 2013, excluding iPhone 5c.
(*) M7 设备是那些配备 A7 SoC 的设备:iPhone 5s、iPad Air、iPad mini 2。M8 是 A8,M9 是 A9,等等。简而言之,自 2013 年秋季以来推出的所有 iOS 设备,不包括 iPhone 5c。
回答by arturdev
You can use this simple library to detect if user is walking, running, on vehicle or not moving.
您可以使用这个简单的库来检测用户是在走路、跑步、在车上还是没有移动。
Works on all iOS devices and no need M7 chip.
适用于所有 iOS 设备,无需 M7 芯片。
https://github.com/SocialObjects-Software/SOMotionDetector
https://github.com/SocialObjects-Software/SOMotionDetector
In repo you can find demo project
在 repo 中,您可以找到演示项目
回答by Kay
The CLLocation
based check is the only reliable information you can get. As specified by Ali in Need to find Distance using Gyro+Accelerometerit is useless to find velocity and position for a longer period of time. The integrated acceleration values start drifting after 0.5 - 2 seconds and there is no chance to get them calibrated again.
在CLLocation
基于检查是唯一可靠的信息,你可以得到。正如 Ali 在Need to find Distance using Gyro+Accelerometer 中所指定的那样,在更长的时间内找到速度和位置是没有用的。积分加速度值在 0.5 - 2 秒后开始漂移,并且没有机会再次校准它们。
Depending on your use case I see some more problems than Usain Bolt's 44.72 km/h (27.79 mph):
根据您的用例,我发现比 Usain Bolt 的 44.72 公里/小时(27.79 英里/小时)更多的问题:
- Regions with no GPS signal like tunnels, underground parking lot, ...
- Errors in GPS. I remember a 6 hours tracking tour in the German Alps and this is what the app thought about it :-)
- As you mentioned already, you never know if the user is in car, train, underground, bus, ... And you never know if he is driving himself or just the co-driver - if this matters.
- 隧道、地下停车场等没有 GPS 信号的地区...
- GPS 中的错误。我记得在德国阿尔卑斯山进行了 6 小时的跟踪之旅,这就是该应用程序的想法:-)
- 正如您已经提到的,您永远不知道用户是在汽车、火车、地铁、公共汽车上……而且您永远不知道他是自己开车还是只是副驾驶——如果这很重要。
回答by serge-k
Xcode 7.3, iOS 9.3
Xcode 7.3,iOS 9.3
Thanks to all the original contributors.
感谢所有原始贡献者。
I just spent a few hours trying to understand why this fairly straight forward class was not working for me. I used all the suggestion above and from other posts to see what activity iOS thought the user was performing using Core Motion framework and using the CMMotionActivityManager
class.
我只是花了几个小时试图理解为什么这个相当直接的课程不适合我。我使用了上面和其他帖子中的所有建议来查看 iOS 认为用户使用 Core Motion 框架和使用CMMotionActivityManager
类执行的活动。
Turns out that for whatever reason I had my "Fitness Tracking" in "Settings"->"Privacy"->"Motion & Fitness" turned off. And for whatever reason after adding the framework and calling a CM class, the app. did not request to enable this. I must have turned this off manually.
事实证明,无论出于何种原因,我都关闭了“设置”->“隐私”->“运动与健身”中的“健身追踪”。在添加框架并调用 CM 类之后,无论出于何种原因,应用程序。没有要求启用此功能。我必须手动关闭它。
Eventually I stumbled onto this post...
最终我偶然发现了这个帖子......
iOS - is Motion Activity Enabled in Settings > Privacy > Motion Activity
iOS - 是否在“设置”>“隐私”>“动作活动”中启用“动作活动”
Which lead me to check the setting and resolve the issue. After I flipped "Fitness Tracking" to on and ran the app. again, everything worked like a charm.
这导致我检查设置并解决问题。在我打开“健身追踪”并运行应用程序之后。再一次,一切都像魅力一样。
Hopefully this saves someone some time! Cheers.
希望这可以节省一些时间!干杯。