Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate distance between user's location and a fixed location

I am receiving multiple restaurants' latitude/longitude from a server. I want to calculate the distance between each restaurant and the user's current latitude/longitude.

like image 801
Surya Avatar asked Oct 05 '11 08:10

Surya


People also ask

How do I find the distance between two Geocodes?

First, subtract y2 - y1 to find the vertical distance. Then, subtract x2 - x1 to find the horizontal distance. Don't worry if the subtraction yields negative numbers. The next step is to square these values, and squaring always results in a positive number.

How do you find the distance between two locations using latitude and longitude?

from math import cos, asin, sqrt, pi def distance(lat1, lon1, lat2, lon2): p = pi/180 a = 0.5 - cos((lat2-lat1)*p)/2 + cos(lat1*p) * cos(lat2*p) * (1-cos((lon2-lon1)*p))/2 return 12742 * asin(sqrt(a)) #2*R*asin...


2 Answers

You can use distanceFromLocation: method of CLLocation which returns the distance in meters between two CLLocation's.

CLLocation *currentLoc = /* Assume you have found it */;
CLLocation *restaurantLoc = [[CLLocation alloc] initWithLatitude:restaurantLat longitude:restaurantLng];
CLLocationDistance meters = [restaurantLoc distanceFromLocation:currentLoc];
// Go ahead with this
like image 137
EmptyStack Avatar answered Dec 19 '22 06:12

EmptyStack


Once you have a list of places (one way or the other) you can the get the distance to (or from) these using CoreLocation to calculate this with distanceFromLocation:

like image 40
Claus Broch Avatar answered Dec 19 '22 05:12

Claus Broch