Вот как это сделать в Swift, используя CoreLocation. Вы просто сравниваете 2 разных местоположения, используя метод .distance(from: )
для местоположения типа CLLocation
. Убедитесь, что оба местоположения имеют тип CLLocation
import CoreLocation
let someOtherLocation: CLLocation = CLLocation(latitude: someOtherLat,
longitude: someOtherLon)
guard let usersCurrentLocation: CLLocation = locationManager.location else { return }
// **the distance(from: ) is right here **
let distanceInMeters: CLLocationDistance = usersCurrentLocation.distance(from: someOtherLocation)
if distanceInMeters < 100 {
// this user is pretty much in the same area as the otherLocation
} else {
// this user is at least over 100 meters outside the otherLocation
}
в этом нет необходимости, но, возможно, вам нужно сохранить латы и лоны для дальнейшего сравнения. Я знаю, что они мне нужны
let usersCurrentLat: CLLocationDegrees = currentLocation.coordinate.latitude // not neccessary but this is how you get the lat
let usersCurrentLon: CLLocationDegrees = currentLocation.coordinate.longitude // not neccessary but this is how you get the lon
let someOtherLocationLat: CLLocationDegrees = someOtherLocationLocation.coordinate.latitude // not neccessary but this is how you get the lat
let someOtherLocationLon: CLLocationDegrees = someOtherLocationtLocation.coordinate.longitude // not neccessary but this is how you get the lon
let usersLocation: CLLocation = CLLocation(latitude: usersCurrentLat,
longitude: usersCurrentLon)
let otherLocation: CLLocation = CLLocation(latitude: someOtherLocationLat,
longitude: someOtherLocationLon)