If you have two latitude/longitude pairs then you want what is called the "great circle distance". A Google search will quickly reveal the formula to convert the lat/lon pairs into this distance.
Hey guys, I have a question about the getDistanceFrom!
I am trying to use CLLocationDistance to get a distance from a starting point. But for some reason when I load the app onto my iphone and run it and dont even move the iphone, after about 3 seconds the distance label displays a number like "2333.34 m" which is obviously not right due to the fact I have not moved the iphone.
I am using CLLocation to get the current location. And we can also save the previous location information.
How can we calculate the distance between these 2 locations?
A few years ago when I got my first GPS unit, I wrote a program in Java that does just that. I won't bother to include that code, but if you are not successful in solving the problem, let me know - off line -- and I'll be happy to send you the code. All it requires is the latitude and longitude of the two locations.
distanceTotal = sqrt(distance.x^2 + distance.y^2); //Magnitude of the vector is equal to the rooted squares of its components
I haven't used the CL library, but if you can get a meaningful unit of measurement (I.E- turning latitude and longitude into a useful measure of distance), it should be as simple as that to get the distance.
If you have two locations (i.e., two sets of latitudes and longitudes) then you should be able to calculate the direction the person is headed without a function call to the iphone. Draw a few graphs and look at the change in the latitudes and longitudes. Then it's probably sine((change in lats/change in longs)) or cosine. I knew which in 10th grade. I'd have to look it up now.
HOWEVER, if it is a pedestrian application I'd suspect the margin of error of the readings on the 2 locations will be too large to get a good read on the direction the person is heading. In an automobile, different story since they are traveling further between any two readings. It also depends on how precise you need to be.
I may be wrong but you should be able to use the law of cosines. I drew up a crappy diagram in MS paint... Yfrog - anglebetween.jpg
Use the north pole, find the distance between the north pole and point A, the north pole and point B, and between A and B. You are trying to find the angle marked by the X, or angle A. I'll call the sides opposite of A, a, and B, b, and the north pole is point C, so the side opposite that is c.
a^2 = b^2 + c^2 - 2bc Cos(A)
so A (the angle X in the diagram) is = arccos ((a^2-b^2-c^2)(-2bc))
And A will give you the bearing from the North, and keep in mind arccos doesn't give you negative angles (only 0 degrees to 180) so you will have to determine (easily) if the person is going west or east. So if you get 90 degrees from this, or pi/2 radians, see if point B is to the east of A or west of A, and if its east of A then the person is going directly east, and vice versa.
is event driven and is only called again when the location has changed enough to fire another event. When you first start updating the location, it appears that the event fires once (3 seconds after starting) to give you the current location. So newLocation would be the current location, and oldLocation would be the previous (different) location, not necessarily the last time the method was called, but the previous time the device's internal location was updated by a significant-enough change in location.