 |
 |
|
 |
11-06-2008, 11:39 AM
|
#1 (permalink)
|
|
Member
Join Date: Oct 2008
Posts: 34
Rep Power: 0
|
Calculating distance between 2 locations using GPS
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?
|
|
|
11-06-2008, 11:47 AM
|
#2 (permalink)
|
|
Senior Member
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,121
Rep Power: 3
|
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.
|
|
|
11-06-2008, 12:47 PM
|
#3 (permalink)
|
|
Senior Member
Join Date: Aug 2008
Posts: 211
Rep Power: 1
|
Just use the getDistanceFrom message in CLLocation.
|
|
|
11-11-2008, 12:47 PM
|
#4 (permalink)
|
|
Member
Join Date: Oct 2008
Posts: 34
Rep Power: 0
|
Thanks for the response.
getDistanceFrom just gives the distance. Is there any way we can find the angle between the location.
I am trying to find out the direction in which user is travelling. How can i find using this GPS API?
|
|
|
11-11-2008, 12:51 PM
|
#5 (permalink)
|
|
Senior Member
Join Date: Aug 2008
Posts: 211
Rep Power: 1
|
A partial calculation of the great circle distance will give you the angle.
|
|
|
11-11-2008, 12:56 PM
|
#6 (permalink)
|
|
Member
Join Date: Oct 2008
Posts: 34
Rep Power: 0
|
Thanks for the quick response.
Distance Calculation latitude longitude global database lists
I am trying to look at this formula for calculating distance. Could not find a way to calculate angle.
DO u know of a web site which has that information?
Thank you so much for your help.
|
|
|
11-11-2008, 01:05 PM
|
#7 (permalink)
|
|
Senior Member
Join Date: Aug 2008
Posts: 211
Rep Power: 1
|
There's an easier way, now that I think about it.
Suppose your starting point is A and your destination is B.
Calculate distance from A to (0, 0) and B to (0, 0). You have two lengths of a triangle.
I told you how to calculate the distance from A to B so you have your third length which completes your triangle.
Use some basic trig to find the angle.
|
|
|
06-13-2009, 02:09 PM
|
#8 (permalink)
|
|
New Member
Join Date: Jun 2009
Posts: 1
Rep Power: 0
|
getDistanceFrom not right
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.
Code:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if (startingPoint == nil)
self.startingPoint = newLocation;
CLLocationDistance distance = [newLocation getDistanceFrom:startingPoint];
NSString *distanceString = [[NSString alloc] initWithFormat:@"%g", distance];
distanceLabel.text = distanceString;
}
Anyone got an idea of why it is doing that?
Thanks a lot.
|
|
|
06-13-2009, 03:21 PM
|
#9 (permalink)
|
|
New Member
Join Date: Jun 2009
Posts: 3
Rep Power: 0
|
Distance between two points.
Quote:
Originally Posted by nbajjuri
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.
Sam (sam@hcc.hawaii.edu)
|
|
|
06-13-2009, 03:32 PM
|
#10 (permalink)
|
|
Senior Member
Join Date: Dec 2008
Posts: 103
Rep Power: 1
|
What about vector algebra?
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.
|
|
|
06-18-2009, 09:46 PM
|
#11 (permalink)
|
|
New Member
Join Date: Jun 2009
Posts: 8
Rep Power: 0
|
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.
|
|
|
06-19-2009, 01:01 AM
|
#12 (permalink)
|
|
New Member
Join Date: Jun 2009
Posts: 5
Rep Power: 0
|
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.
Last edited by Launch; 06-19-2009 at 01:04 AM.
|
|
|
 |
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
» Advertisements |
|
» Online Users: 235 |
| 30 members and 205 guests |
| 2ndSegment, bondjpf, crossfire, dapis, davidlansalot, ElysianEagle, enfamus, exosyphen, freshking, g.castaldi, gbh, Groucho, hugo, joshsroka, Jume, kaleman, Lars, lastiko, lildragon, martinws, mattjgalloway, MetaImi, mnemonic_fx, nattylux, OneGlobe, Opticfibre, shabzcohelp, Sicga, StatCoder, williamlegate |
| Most users ever online was 779, 05-11-2009 at 09:55 AM. |
» Stats |
Members: 8,231
Threads: 20,199
Posts: 90,216
Top Poster: RickMaddy (2,121)
|
| Welcome to our newest member, OneGlobe |
|