Surya
Surya

Reputation: 107

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.

Upvotes: 6

Views: 9450

Answers (4)

Swift 3

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

Upvotes: 0

EmptyStack
EmptyStack

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

Claus Broch
Claus Broch

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

Dip Dhingani
Dip Dhingani

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

Related Questions