简体   繁体   中英

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.

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 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM