Advertise Books Events Forum News Social Networking Support Us

sdkIQ for iPhone
($4.99)

Shape Up
($0.99)

Your First iPhone App
($1.99)

iVidCam Free
(free)

Kid Art
($0.99)

iPUBQUIZ
(£1.19)

ArtStudio
($3.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-06-2008, 11:39 AM   #1 (permalink)
New Member
 
Join Date: Oct 2008
Posts: 34
Default 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?
nbajjuri is offline   Reply With Quote
Old 11-06-2008, 11:47 AM   #2 (permalink)
New Member
 
RickMaddy's Avatar
 
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,121
Default

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.
RickMaddy is offline   Reply With Quote
Old 11-06-2008, 12:47 PM   #3 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 211
Default

Just use the getDistanceFrom message in CLLocation.
lapse is offline   Reply With Quote
Old 11-11-2008, 12:47 PM   #4 (permalink)
New Member
 
Join Date: Oct 2008
Posts: 34
Default

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?
nbajjuri is offline   Reply With Quote
Old 11-11-2008, 12:51 PM   #5 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 211
Default

A partial calculation of the great circle distance will give you the angle.
lapse is offline   Reply With Quote
Old 11-11-2008, 12:56 PM   #6 (permalink)
New Member
 
Join Date: Oct 2008
Posts: 34
Default

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.
nbajjuri is offline   Reply With Quote
Old 11-11-2008, 01:05 PM   #7 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 211
Default

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.
lapse is offline   Reply With Quote
Old 06-13-2009, 02:09 PM   #8 (permalink)
New Member
 
Join Date: Jun 2009
Posts: 1
Default 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.
man2manno is offline   Reply With Quote
Old 06-13-2009, 03:21 PM   #9 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 10
Default Distance between two points.

Quote:
Originally Posted by nbajjuri View Post
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)
SamRhoads is offline   Reply With Quote
Old 06-13-2009, 03:32 PM   #10 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 175
Default

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.
Chilibird is offline   Reply With Quote
Old 06-18-2009, 09:46 PM   #11 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 12
Default

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.
bteasley is offline   Reply With Quote
Old 06-19-2009, 01:01 AM   #12 (permalink)
New Member
 
Join Date: Jun 2009
Posts: 5
Default

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.
Launch is offline   Reply With Quote
Old 08-02-2009, 06:22 PM   #13 (permalink)
New Member
 
Join Date: Aug 2009
Posts: 1
Default

Quote:
Originally Posted by man2manno View Post
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.
RobS is offline   Reply With Quote
Old 08-02-2009, 07:29 PM   #14 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
Default

nvm

joe
FlyingDiver is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Enter the iPhone App Challenge!  Win $500!
» Advertisements
» Stats
Members: 24,293
Threads: 39,083
Posts: 171,370
Top Poster: smasher (2,575)
Welcome to our newest member, michael@2label
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 10:59 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0