 |
 |
|
 |
11-06-2008, 11:39 AM
|
#1 (permalink)
|
|
New Member
Join Date: Oct 2008
Posts: 34
|
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)
|
|
New Member
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,121
|
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)
|
|
New Member
Join Date: Aug 2008
Posts: 211
|
Just use the getDistanceFrom message in CLLocation.
|
|
|
11-11-2008, 12:47 PM
|
#4 (permalink)
|
|
New Member
Join Date: Oct 2008
Posts: 34
|
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)
|
|
New Member
Join Date: Aug 2008
Posts: 211
|
A partial calculation of the great circle distance will give you the angle.
|
|
|
11-11-2008, 12:56 PM
|
#6 (permalink)
|
|
New Member
Join Date: Oct 2008
Posts: 34
|
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)
|
|
New Member
Join Date: Aug 2008
Posts: 211
|
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
|
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)
|
|
Registered Member
Join Date: Jun 2009
Posts: 10
|
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)
|
|
Registered Member
Join Date: Dec 2008
Posts: 175
|
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)
|
|
Registered Member
Join Date: Jun 2009
Posts: 12
|
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
|
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.
|
|
|
08-02-2009, 06:22 PM
|
#13 (permalink)
|
|
New Member
Join Date: Aug 2009
Posts: 1
|
Quote:
Originally Posted by man2manno
Hey guys, I have a question about the getDistanceFrom!
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
Anyone got an idea of why it is doing that?
Thanks a lot.
|
My understanding is that
Code:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
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.
Last edited by RobS; 08-02-2009 at 06:43 PM.
|
|
|
08-02-2009, 07:29 PM
|
#14 (permalink)
|
|
Former NeXTStep Developer
Join Date: Mar 2009
Posts: 997
|
nvm
joe
|
|
|
 |
| 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: 373 |
| 29 members and 344 guests |
| ashish_pandita, bensj, bono_aem, BrianSlick, CHV, dkern, Erle, firearasi, giampo, graph, hawksz, henmue, iGeorG, iTrackiGiveaway, Kalimba, KennyChong, kiancheong, Kilby, marklusted, masc2279, mikeyb, mr.pagu, pengcognito, pierresinne, quky, raviram, rawjam, svveet, yeohchan |
| Most users ever online was 779, 05-11-2009 at 09:55 AM. |
» Stats |
Members: 24,293
Threads: 39,083
Posts: 171,370
Top Poster: smasher (2,575)
|
| Welcome to our newest member, michael@2label |
|