Reputation: 107
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.
Upvotes: 6
Views: 9450
Reputation: 51
func calculateDistanceFromUser(location:CLLocation)->CLLocation!{
if let userLocation = self.userUpdatedLocation{ //CLLocation
let meters = location.distance(from: userLocation)
return meters
}
return nil
}
Upvotes: 0
Reputation: 51374
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
Upvotes: 39
Reputation: 9212
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:
Upvotes: 3
Reputation: 2499
You can use an API provided by Google for the same. The Google Distance Matrix API
Its perfect for finding distance. Hope it helps.
Upvotes: 2