mike vorisis mike vorisis - 4 months ago 18
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

Step-1

Initially convert your lat and long to CLLocation.

Step-2

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

CLLocationDistance is just double and distance calculated in meters

Step-3

convert distance to meters

let distanceKM = distance / 1000

step-4

finally compare to distance with in range

if  distanceKM > 1000 &&  distanceKM < 10000
{
 // drop your pins 
}