Cllocation Distancefromlocation (In Swift)

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



Leave a reply



Submit