Home News Forum Social Networking Support Us Advertise

Spanish Lesson 1 ($1.99)

aWake!Gently ($1.99)

The Bird & The Snail - Knock Knock - Deluxe ($4.99)

Match-It Trains ($0.99)

Tangled ($0.99)

iFlatter ($0.99)

The 15 puzzle ($0.99)

Tap Forms Database ($8.99)

Higher or Lower Card Game (Hi Lo) ($0.99)

Red Pixel ($0.99)

Time-Shift Radio ($0.99)

Want your application advertised here? Only $10/week!

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)
Member
 
Join Date: Oct 2008
Posts: 34
Rep Power: 0
nbajjuri is on a distinguished road
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)
Senior Member
 
RickMaddy's Avatar
 
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,121
Rep Power: 3
RickMaddy is on a distinguished road
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)
Senior Member
 
Join Date: Aug 2008
Posts: 211
Rep Power: 1
lapse is on a distinguished road
Default

Just use the getDistanceFrom message in CLLocation.
lapse is offline   Reply With Quote
Old 11-11-2008, 12:47 PM   #4 (permalink)
Member
 
Join Date: Oct 2008
Posts: 34
Rep Power: 0
nbajjuri is on a distinguished road
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)
Senior Member
 
Join Date: Aug 2008
Posts: 211
Rep Power: 1
lapse is on a distinguished road
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)
Member
 
Join Date: Oct 2008
Posts: 34
Rep Power: 0
nbajjuri is on a distinguished road
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)
Senior Member
 
Join Date: Aug 2008
Posts: 211
Rep Power: 1
lapse is on a distinguished road
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
Rep Power: 0
man2manno is on a distinguished road
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)
New Member
 
Join Date: Jun 2009
Posts: 3
Rep Power: 0
SamRhoads is on a distinguished road
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)
Senior Member
 
Join Date: Dec 2008
Posts: 103
Rep Power: 1
Chilibird is on a distinguished road
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)
New Member
 
Join Date: Jun 2009
Posts: 8
Rep Power: 0
bteasley is on a distinguished road
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
Rep Power: 0
Launch is on a distinguished road
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
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


» Advertisements


» Stats
Members: 8,231
Threads: 20,199
Posts: 90,216
Top Poster: RickMaddy (2,121)
Welcome to our newest member, OneGlobe
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:35 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0