Detecting if a user is moving in a car

前端 未结 4 947
一整个雨季
一整个雨季 2020-12-04 10:27

NOTICE: This question was originally posted before Apple introduced motion-detection hardware and associated APIs in the iOS SDK. Answers to

相关标签:
4条回答
  • 2020-12-04 10:53

    The CLLocation based check is the only reliable information you can get. As specified by Ali in Need to find Distance using Gyro+Accelerometer it 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.

    Depending on your use case I see some more problems than Usain Bolt's 44.72 km/h (27.79 mph):

    1. Regions with no GPS signal like tunnels, underground parking lot, ...
    2. Errors in GPS. I remember a 6 hours tracking tour in the German Alps and this is what the app thought about it :-)
      Walking on water with 920 km/h
    3. 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.
    0 讨论(0)
  • 2020-12-04 10:58

    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.

    https://github.com/SocialObjects-Software/SOMotionDetector

    In repo you can find demo project

    0 讨论(0)
  • 2020-12-04 10:58

    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.

    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.

    Eventually I stumbled onto this post...

    iOS - is Motion Activity Enabled in Settings > Privacy > Motion Activity

    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.

    0 讨论(0)
  • 2020-12-04 11:03

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

    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.

    1. Create a CMMotionActivityManager object (after using its class method isActivityAvailable to determine whether you you have M7(+) features), and either start activity updates or query it for recent activities.
    2. Check the returned CMMotionActivity objects' automotive property to see if iOS thinks the user is/was in a car.
    3. There's no step three.

    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.


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

    0 讨论(0)
提交回复
热议问题