mike vorisis mike vorisis - 1 year ago 70
Swift Question

How can I find some distance from my location with Swift

I have an app that takes some results(lat,long) from my server and put annotations in my map depends the lat and long that it took.

Now i put a slider that filters the results by kilometers (1-10)

How can i filter my query to add the annotation if the lat an long is in its radius location.

Answer Source


Initially convert your lat and long to CLLocation.


var one, two: CLLocation
// assign one and two
let distance = two.distanceFromLocation(one)

CLLocationDistance is just double and distance calculated in meters


convert distance to meters

let distanceKM = distance / 1000


finally compare to distance with in range

if  distanceKM > 1000 &&  distanceKM < 10000
 // drop your pins 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download