Getting speed/heading from iPhone's GPS

Discussion in 'Mac Programming' started by imrtt, Jul 19, 2008.

  1. imrtt macrumors newbie

    Jul 19, 2008
    Am I missing something, or is there no way to get speed/heading from CLLocation / CLLocationManager? I can't believe Apple would leave those attributes out.
  2. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    it seems like as long as you have 2 or more points and the times they were gathered you can calculate direction and velocity.

  3. imrtt thread starter macrumors newbie

    Jul 19, 2008
    Yes, but the accuracy would be terrible. GPS positions sometimes fluctuate by hundreds of feet, even if you are standing still. On the other hand, velocity as computed from GPS signals is accurate to within 1/10 mph.
  4. Cromulent macrumors 603


    Oct 2, 2006
    The Land of Hope and Glory
    Then why don't you just calculate the velocity then?

    Velocity = distance (in a certain direction) / time.
  5. imrtt thread starter macrumors newbie

    Jul 19, 2008
    GPS chips do not compute speed by dividing distance by time. As I mentioned earlier, that would be too imprecise, given that positions (and hence distances) fluctuate quite a bit. Instead, they measure Doppler shift in signals received from GPS satellites. Because positions and speeds of satellites are known to a very high precision, the speed of the receiver can also be computed very accurately, down to a fraction of mph.

    All other GPS-enabled phones (e.g. BlackBerries or N-series Nokias) expose both speed and direction through APIs. The fact that Apple left these out from the iPhone is a glaring omission, one that I hope they will correct in a future release.
  6. fishcove Guest

    If the GPS can calculate speed/heading for you with greater accuracy than you can do it using position information, then why can't the GPS use this information to improve the accuracy of that position information? :)
  7. imrtt thread starter macrumors newbie

    Jul 19, 2008
    That's because GPS receivers do not compute speed/direction continuously. A typical receiver operates at 1Hz. Also, even the smaller errors in speed would accumulate over time.
  8. antibact1 macrumors 6502

    Jun 1, 2006
    Those APIs have no mention of velocity which you are obviously aware of. People have offered you solutions which you have criticized. Come up with something yourself - you obviously have more intrinsic knowledge of how GPS works than other people.
  9. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    If velocity/direction are something GPS hardware computes, there are three possibilities:
    1) The GPS hardware in the iPhone 3G does not support this hardware function.
    2) The GPS hardware in the iPhone 3G does support this function, but the Cocoa Touch APIs do not expose this data to the programmer.
    3) The GPS hardware in the iPhone 3G does support this function, and the Cocoa Touch APIs do expose this data to the programmer.

    I have not looked at the CoreLocation documentation, and I do not know if the latest version of the SDK has any APIs for accessing this type of information from the GPS hardware. The OP states that they cannot get this information, so I will assume that they have checked and this information is not available. This leaves two of the three possibilities.

    In one case, the hardware is incapable of doing this, which could mean that the chip chosen doesn't perform this function. Maybe the chip has lower power consumption, is more readily available, is cheaper, etc. than chips that do. I'm sure people have discovered the GPS chip/receiver/etc in the iPhone 3G by means of teardowns featured on a number of sites. If that's the case, it would be easy enough to find out if the hardware supports this function or not. I just did a quick search and found that the GPS hardware in the iPhone 3G is the Infineon PMB 2525 Hammerhead II. I do not know what this velocity/direction feature of GPS hardware is called, so I can't find out much more than that. The product description for the chip is here:

    The other case is that the hardware is capable, but Apple has not published a means of accessing this. This might be because calculating this information in hardware is expensive for power reasons, or they just haven't gotten around to safely exposing this hardware function in software. If this is the case, I expect that they will remedy this with a future SDK update.

    One last possibility is that this is not a hardware function, but a complex software function performed based on data delivered by all GPS hardware. If this is the case, it may simply be that other platforms have implemented these functions for you and expose them through their API. If this is the case, I would suspect that Apple would write these routines and expose them in a later release. However, if this is the case, you would probably be able to write it yourself.

  10. antibact1 macrumors 6502

    Jun 1, 2006
    Checking the actual CoreLocation module shows that there are heading and speed attributes present in CLLocation, they just aren't defined in the header. Give them a try:

    316ae740 t -[CLLocation altitude]
    316ae814 t -[CLLocation clientLocation]
    316ae6fc t -[CLLocation coordinate]
    316ae59c t -[CLLocation copyWithZone:]
    316ae8a8 t -[CLLocation course]
    316ae53c t -[CLLocation dealloc]
    316ae608 t -[CLLocation description]
    316ae36c t -[CLLocation encodeWithCoder:]
    316ae8d4 t -[CLLocation getDistanceFrom:]
    316ae850 t -[CLLocation heading]
    316ae76c t -[CLLocation horizontalAccuracy]
    316ae104 t -[CLLocation initWithClientLocation:]
    316ae1dc t -[CLLocation initWithCoder:]
    316adff8 t -[CLLocation initWithCoordinate:altitude:horizontalAccuracy:verticalAccuracy:timestamp:]
    316adf54 t -[CLLocation initWithLatitude:longitude:]
    316ae87c t -[CLLocation speed]
    316ae7c4 t -[CLLocation timestamp]
    316ae798 t -[CLLocation verticalAccuracy]
    316adef8 t -[CLLocationInternal copyWithZone:]
    316ade80 t -[CLLocationInternal initWithClientLocation:]
    316af234 t -[CLLocationManager bestAccuracy]
    316aeb5c t -[CLLocationManager dealloc]
    316aebe0 t -[CLLocationManager delegate]
    316aee6c t -[CLLocationManager desiredAccuracy]
    316aed24 t -[CLLocationManager distanceFilter]
    316aead4 t -[CLLocationManager init]
    316af2ac t -[CLLocationManager internalClient]
    316af054 t -[CLLocationManager locationServicesApproved]
    316af020 t -[CLLocationManager locationServicesEnabled]
    316aee94 t -[CLLocationManager location]
    316af4f4 t -[CLLocationManager onClientEvent:supportInfo:]
    316af538 t -[CLLocationManager onClientEventLocation:]
    316af9f0 t -[CLLocationManager onClientEventRegistered:]
    316aebbc t -[CLLocationManager setDelegate:]
    316aed4c t -[CLLocationManager setDesiredAccuracy:]
    316aec04 t -[CLLocationManager setDistanceFilter:]
    316af088 t -[CLLocationManager setSupportInfo:]
    316af2d0 t -[CLLocationManager startUpdatingLocation]
    316af3f8 t -[CLLocationManager stopUpdatingLocation]
    316af1fc t -[CLLocationManager supportInfo]
    316aea70 t -[CLLocationManagerInternal dealloc]
    316ae960 t -[CLLocationManagerInternal initWithInfo:]
  11. tdhurst macrumors 601


    Dec 27, 2003
    Phoenix, AZ

    I remember reading something in the iPhone developer DSK that the iPhone can't be used for real time trip navigation, which is why TomTom isn't allowed on the phone.
  12. imrtt thread starter macrumors newbie

    Jul 19, 2008
    Thank you! Will give it a try and report back. Just curious, how do you dump this data from a module?
  13. imrtt thread starter macrumors newbie

    Jul 19, 2008
    What would be the recommended practice of accessing member variables that are not in the .h file? location.speed obviously doesn't work. I could find the .h file in the framework and modify it, but then I wonder if Apple would compile my code in their environment when the app is submitted. Also, is there any way to find out what the types of the hidden attributes are?
  14. imrtt thread starter macrumors newbie

    Jul 19, 2008
    No luck. Speed is always set to 0 and heading to 1. Tried this on the physical device too. Thanks everyone for your help.
  15. NSNick macrumors regular

    Jun 27, 2008
    Washington D.C.
    For Doppler shift to mean anything the object would have to be traveling towards or away from the satelite. Unless it is tracking the speed of a rocket heading right for it, there is no way that a GPS satelite could use Doppler shift for speed. GPS disance is accurate to within meters. Stop being a lazy arse and write a program to calculate it. I will give you a hint - divide the distance in miles by the time difference in hours and you will get mph.
  16. lazydog macrumors 6502a

    Sep 3, 2005
    Cramlington, UK
    I really hope you're not a professional programmer in the aerospace industry :rolleyes:

    b e n
  17. Nonnus3G macrumors member

    Jul 9, 2008
    hi ppl

    i´ve implemented my own gps compass

    today i was trying it while driving the car
    although the heading is not the most accurate in the world and needs some speed to properly work it gives a fairly usable orientation,
    i could even see it turning while i was on a curve !

    but what impressed me was that the speed reading was quite accurate compared to the car display, not atomic precision but precise enough for me to know at what speed some transport i am travelling in is going

    i am not talking about doppler or other crazy stuff here,
    just basic psysics with the universal speed = distance / time

    i have also played a bit with the accelerometers,
    i was hoping to track horizontal rotation to try drive the compass after being set, or always show the right direction to some place as you rotate the phone

    unfortunatelly i could only track all the other rotations but not horizontal (not gravity influented...)
    any ideas ?


  18. akac macrumors 6502


    Aug 17, 2003
    So Nonus, given the discussion here how did you end up getting the speed? Simple math of distanceMoved divided by time?
  19. Nonnus3G macrumors member

    Jul 9, 2008

    i am always using the distance between the newlocation and oldlocation reported on each gps data callback
  20. imrtt thread starter macrumors newbie

    Jul 19, 2008
    Guess what -- GPS satellites do move relative to the ground, as they are not geostationary. The distance between the receiver and satellites constantly changes, which means that there is measurable doppler shift (any relative velocity = doppler shift).

    Computing distance / time can work but is very imprecise. GPS positions are accurate to within a few meters _on average_, but there is significant variation. Occasionally, my iphone 3g will move my position on Google Maps by a hundred feet or so, even though I am stationary. 100 ft / sec = 70 mph. I am looking for a bit more precision.
  21. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
  22. laprej macrumors regular

    Oct 14, 2005
    Troy, NY
    Beggars can't be choosers.
  23. donmei macrumors regular

    Mar 8, 2007
    I'm not a programmer, but I do know how GPS devices work. Its simple. They derive "heading" and speed by taking your position at one point in time, then at another point in time and then deriving the direction you are traveling (NOT HEADING) and your velocity.

    Heading is not direction of travel. Heading describes what direction you are pointing in. GPS can not calculate that. Remember on a boat or in an aircraft your heading almost is never your direction of travel.

    Also in other words, a gps can not compute "heading" if you are not moving. When standing still any heading is just caused by small positional errors. Remember that a standard GPS updates every 1 second.

    Also IMRT - you are wrong about gps position fluctuating by hundreds of feet. Not since the demise of Selective Availability (google it) have errors of that magnitude existed in the gps signal. The error for a normal GPS is generally under 30 ft. The error for position corrected (WAAS - Wide area augmentation system) is generally about 3 feet. WAAS corrected GPS is good enough that aircraft can do precision approaches in the fog with it. Unfortunately, I dont believe any phone devices are WAAS enabled.

    p.s. I've had a BBery for the last 1 1/2 years with GPS and Googlemaps for mobile. Its a great combo.

    p.p.s. Finally, Garmin had just released software for blackberry that makes it mimic one of their dedicated navigators. I cant believe an iPhone version can be far behind.
  24. Nonnus3G macrumors member

    Jul 9, 2008
    after some further driving and testing my gps compass is working pretty reliable
    i would just like to better filter anomalous data to improve accuracy
    speed reported is not too erratic
    but not so much about total distance calculations, lots of strange readings between good ones...
  25. gnasher729 macrumors P6


    Nov 25, 2005
    I checked this article on Wikipedia:

    No mention of using any doppler shift at all. The GPS receiver knows its time with a precision far better than a microsecond. The precision of the position is only about 5 to 15 meters, but the error is almost constant unlike what you were stating. If your GPS is off by 12 meters to the west now, then it will be off by exactly the same 12 meters one second later, and even ten seconds later. The GPS receiver can actually determine a change of position with much less than a meter of precision, and since it knows the time with microsecond precision, it will get your velocity with very high precision.

Share This Page