Calculate distance between user location and fixed location

I get the latitude / longitude of several restaurants from the server. I want to calculate the distance between each restaurant and the current latitude / longitude of the user.

+6
source share
4 answers

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

+3
source

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

CLLocation *currentLoc = /* Assume you have found it */; CLLocation *restaurantLoc = [[CLLocation alloc] initWithLatitude:restaurantLat longitude:restaurantLng]; CLLocationDistance meters = [restaurantLoc distanceFromLocation:currentLoc]; // Go ahead with this 
+39
source

You can use the API provided by Google to do this. Google Distance Matrix API

It is ideal for finding distance. Hope this helps.

+2
source

Swift 3

 func calculateDistanceFromUser(location:CLLocation)->CLLocation!{ if let userLocation = self.userUpdatedLocation{ //CLLocation let meters = location.distance(from: userLocation) return meters } return nil } 
0
source

Source: https://habr.com/ru/post/898650/


All Articles