CLLocation distanceFromLocation (in Swift?)
let distance = fromLocation.distanceFromLocation(toLocation)
New syntax:
let distance = aLocation.distance(from: anotherLocation)
How to find out distance between coordinates?
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
}
distanceFromLocation - Calculate distance between two points
Try this instead:
CLLocationDistance meters = [newLocation distanceFromLocation:oldLocation];
The method you're trying to use is a method on a CLLocation object :)
CLLocation distanceFromLocation returns wrong answer, then correct answers
In my experience the first values you get from the location manager are bad. The very first location reading is often a cached reading from when the GPS was last "lit." You can detect and reject stale readings by checking the timestamp on the location. If it's more than a second old, discard it.
Next, the first few location readings you get are often quite inaccurate. You want to check the horizontal accuracy reading, which is actually a "radius" value (in meters) that specifies the radius a circle in which the location might be located. Smaller values are better.
You should check the horizontal accuracy reading and discard readings where the accuracy is less than some threshold you select. I suggest 20 meters or so. Depending on the conditions you might not get ANY readings more accurate than that.
The simulator is just about worthless for testing this stuff. You need to create a test app that logs the readings you get and then get out and walk around (and/or ride as a passenger in a car/bus) with it and test it out. You might even get somebody to drive you around while you have your phone and Mac and run the app from Xcode while tethered. (I did that when I was first figuring out the subtleties of the location manager.)
Casting CLLocationDistance in Swift
The reason this is occurring boils down to the following line:
let distance = CLLocation.distanceFromLocation(loc)
This is not doing what you think it's doing. distanceFromLocation
is an instance method, but you are calling it as a class method. Unlike ObjC, Swift allows this, but what you're actually getting back is not a CLLocationDistance
but a function with the following type signature: CLLocation -> CLLocationDistance
. You can verify this by adding in the type annotation yourself:
let distance: CLLocation -> CLLocationDistance = CLLocation.distanceFromLocation(loc)
This is called partial application and is outside the scope of my answer, except to note that the first parameter of the function assigned to distance
would be the implicit self
when called as an instance method.
The fix is simple:
let distance = loc.distanceFromLocation(loc)
Then everything will work as you desire, at least from the perspective of the type system, because this properly returns a CLLocationDistance
, which is substitutable for Double
.
CLLocation distanceFromLocation: returns zero on iOS 9. Is it a bug?
Check the latitude value of location1 .. I guess there is some mismatch with range of latitude (-90 , 90).
Finding distance between CLLocationCoordinate2D points
You should create an object of CLLocation using,
- (id)initWithLatitude:(CLLocationDegrees)latitude
longitude:(CLLocationDegrees)longitude;
Then, you should be able to calculate the distance using
[location1 distanceFromLocation:location2];
Convert distanceFromLocation to Int
It's possible I'm missing something, but does let distanceInt = Int(distance)
not do exactly what you want?
Related Topics
Xcode 8 Shell Script Invocation Error
Swift: Reusable Uiview in Storyboard and Sizing Constraints
Nsdateformatter Detect 24-Hour Clock in Os X and iOS
Uirefreshcontrol() in iOS 11 Glitchy Effect
How to Suppress a Specific Warning in Swift
From Any Utf-16 Offset, Find the Corresponding String.Index That Lies on a Character Boundary
Margin Between Images in Uiscrollview
How to Access App Specific Folder and Use It to Backup or Some App Specific Data? in Swift
iOS 11 PDFkit Not Updating Annotation Position
Instagram Explorer Searchbar and Tableview
How to Detect If a Observable Has Not Emitted Any Events for Specific Time in Rxswift
Swiftui, Shadow Only for Container
What Is the Slice Compare Logic in Swift
Symbol Is Considered to Be an Identifier, Not an Operator