How to find out distance between coordinates?
86,111
Solution 1
CLLocation has a distanceFromLocation method so given two CLLocations:
CLLocationDistance distanceInMeters = [location1 distanceFromLocation:location2];
or in Swift 4:
//: Playground - noun: a place where people can play
import CoreLocation
let coordinate₀ = CLLocation(latitude: 5.0, longitude: 5.0)
let coordinate₁ = CLLocation(latitude: 5.0, longitude: 3.0)
let distanceInMeters = coordinate₀.distance(from: coordinate₁) // result is in meters
you get here distance in meter so 1 miles = 1609 meter
if(distanceInMeters <= 1609)
{
// under 1 mile
}
else
{
// out of 1 mile
}
Solution 2
Swift 4.1
import CoreLocation
//My location
let myLocation = CLLocation(latitude: 59.244696, longitude: 17.813868)
//My buddy's location
let myBuddysLocation = CLLocation(latitude: 59.326354, longitude: 18.072310)
//Measuring my distance to my buddy's (in km)
let distance = myLocation.distance(from: myBuddysLocation) / 1000
//Display the result in km
print(String(format: "The distance to my buddy is %.01fkm", distance))
Solution 3
Try this out:
distanceInMeters = fromLocation.distanceFromLocation(toLocation)
distanceInMiles = distanceInMeters/1609.344
From Apple Documentation:
Return Value: The distance (in meters) between the two locations.
Solution 4
import CoreLocation
//My location
let myLocation = CLLocation(latitude: 31.5101892, longitude: 74.3440842)
//My Next Destination
let myNextDestination = CLLocation(latitude: 33.7181584, longitude: 73.071358)
//Finding my distance to my next destination (in km)
let distance = myLocation.distance(from: myNextDestination) / 1000
Solution 5
func calculateDistanceInMiles(){
let coordinate₀ = CLLocation(latitude:34.54545, longitude:56.64646)
let coordinate₁ = CLLocation(latitude: 28.4646, longitude:76.65464)
let distanceInMeters = coordinate₀.distance(from: coordinate₁)
if(distanceInMeters <= 1609)
{
let s = String(format: "%.2f", distanceInMeters)
self.fantasyDistanceLabel.text = s + " Miles"
}
else
{
let s = String(format: "%.2f", distanceInMeters)
self.fantasyDistanceLabel.text = s + " Miles"
}
}
Author by
John Doe
Updated on April 28, 2020Comments
-
John Doe about 4 years
I want to make it so that it will show the amount of distance between two CLLocation coordinates. Is there someway to do this without a complex math formula? If there isn't how would you do it with a formula?
-
John Doe over 8 yearsHow would I convert this into miles?
-
John Doe over 8 yearsIs there a way to turn it into miles?
-
John Doe over 8 yearsOh wait I got it! Thanks. (:
-
Glenn Howes over 8 yearsAccording to Google, there are 0.000621371 miles in a meter. So, I'd suggest multiplying it by 0.000621371.
-
John Doe over 8 yearsOk great, thanks! I'll accept your answer in a few minutes when it will let me.
-
vijay about 7 yearssome times its showing wrong distance i checked with google map with same locations which i have given the same locations in the code. please any other way to calculate distance let me know please.
-
Glenn Howes about 7 yearsWell, how far off is it?
-
Dory Daniel about 7 yearsthe question here is captioned with 'swift'. So maybe you need to update your answer, or add "For objective-c :" @Vijay
-
AnBisw almost 7 yearsAlso from apple docs - "This method measures the distance between the two locations by tracing a line between them that follows the curvature of the Earth. The resulting arc is a smooth curve and does not take into account specific altitude changes between the two locations."developer.apple.com/documentation/corelocation/cllocation/…
-
AnBisw almost 7 yearsThis is not the true distance "By road" According to apple Docs - "This method measures the distance between the two locations by tracing a line between them that follows the curvature of the Earth. The resulting arc is a smooth curve and does not take into account specific altitude changes between the two locations." developer.apple.com/documentation/corelocation/cllocation/… .. so if you're looking for driving distance, the reliable way to find the correct distance so far has been Google Maps Distance Matrix API or MKRoute.
-
Glenn Howes almost 7 years@Annjawn That really has nothing to do with the question as asked.
-
AnBisw almost 7 years@GlennHowes yes it does, chances are that the OP is trying to find the driving (or other mode of transport) distance between two points in which case just a Geospatial distance as is derived by
distanceFromLocation
will be completely incorrect. For Driving distance, the correct implementation would be to useMKRoute
which will give you the "Route Distance"distance
in meters along with other stuff like expected travel time, transport type etc - developer.apple.com/documentation/mapkit/mkroute -
AnBisw almost 7 yearsAnd as reported by @vijay (in comments above) this is the reason why his distances are incorrect when compared to Google Map. Google map and MKRoute will consider the route information to calculate the actual transport distance whereas the simple
distanceFromLocation
won't, so I suggest you update your answer to clarify this in order to avoid confusing people who reach this page through google. -
Muhammed over 6 yearsHow can i do that with a place with 2 Coordinates ?
-
rollstuhlfahrer about 6 yearsWhile this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
-
Glenn Howes about 6 yearsNo, it's just a math calculation. It isn't a turn by turn database lookup.
-
Gurjinder Singh almost 6 yearslet distanceInMiles = coordinate.distance(from:currentDeviceCoordinate) / 1609.344 // result is in miles
-
koen over 4 yearsWhile this code may answer the question, it would be better to explain how it solves the problem without introducing others and why to use it. Code-only answers are not useful in the long run.
-
thetrutz about 4 yearsGood, that you use the correct factor
1609.344
. en.wikipedia.org/wiki/Mile -
gyratory circus over 3 yearsFor iOS 10+, it would be useful to perform the conversion to miles via the
Measurement
class:let miles = Measurement(value: meters, unit: UnitLength.meters).converted(to: UnitLength.miles).value