views:

541

answers:

1

Hi, I'm trying to develop an application that use the GPS and Compass of the iPhone in order to point some sort of pointer to a specific location (like the compass always point to the North).
The location is fixed and I always need the pointer to point to that specific location no matter where the user is located.
I have the Lat/Long coordinates of this location but not sure how can I point to that location using the Compass and the GPS... any help will be appreciated.

Netanel

A: 

Hey Netanel,

Let's say you have the latitude and longitude of your current point and the destination point. Let's call the destination "Q" and the current point "P".

First, compute the lat and lon distances (deltas) between P and Q:

dx = q.lat - p.lat
dy = q.lon - p.lon

Then compute the angle that the user needs to travel in order to reach Q from P. This is just basic triangle geometry:

float angleRadians = atan2(dy,dx);

Next, we want to compare that angle to the current angle provided by the iPhone's compass. You want the arrow's heading to be relative to the user's current heading, so that the app says "Turn 10º to the left of your current heading" rather than saying "turn 56º from North." That can be accomplished like this:

float relativeRadians = angleRadians - currentHeadingRadians

Something like that should do it! That's some heavy psuedo-code, but I think that's the basic idea. Hope that helps!

Ben Gotow