Priyanka
Priyanka

Reputation: 513

How To calculate nearest location from user?

In my application i am having 4 places with its longitude and altitude(Given).Now i want to find the nearest place from user.

So anyone can tell me the solution for it or provide me some source code or demo for this.

Thanks to all

Upvotes: 2

Views: 405

Answers (4)

winwaed
winwaed

Reputation: 7801

picknick describes a native class method.

This is probably going to use the Haversine formula (search the web or stackoverflow for discussions) to measure distances. In my experience, writing your own Haversine is usually faster than using a system/app-provided one, although this is on Windows where COM overhead can be "problematic" as it were!

The Haversine (and many similar geometry-on-the-Earth) formulae can be found here:

http://williams.best.vwh.net/avform.htm

(this is one of my few bookmarks I go back to for reference on a regular basis!)

Upvotes: 0

picknick
picknick

Reputation: 4007

Create an CLLocation of the points:

CLLocation* locationx = [[CLLocation alloc] initWithLatitude:lat longitude:lon];

And the compare the points with:

double distance = [userposition distanceFromLocation:locationx];

Keep the nearest location.

Edit: If you really mean "longitude and altitude" then I dont know how.

Upvotes: 2

John Kenedy
John Kenedy

Reputation: 574

for example user is at x=0,y=0 Calculate the distance by doing : SquareRoot(x2^2 & y2^2) You will get a distance. Look through all points and compare which distance is the shortest

Upvotes: -1

oezi
oezi

Reputation: 51797

just took 10 seconds to ask google and find this, wich describes how to calculate the distance beween two points given latitude and logitude:

http://www.movable-type.co.uk/scripts/latlong.html

all you have to do is calculate the distance to all 4 points and choose the lowest one.

ETDI: i'm sure you're also given the latitude, otherwise you won't even have specific coordinates. if this wasn't a type and you really have to take the altitude into account, take a look at this question at google answers

Upvotes: 1

Related Questions