要比較兩個CLLocationCoordinate2Ds您可以檢查它們的緯度和長期互相反對。
func payTime() {
if driverLocation?.latitude == userLocation?.latitude && driverLocation?.longitude == userLocation?.longitude {
// Overlapping
}
}
但是,只有它們是完全相同的位置才能使用。或者您可以使用這樣的事情:
func payTime() {
if let driverLocation = driverLocation, let userLocation = userLocation{
let driverLoc = CLLocation(latitude: driverLocation.latitude, longitude: driverLocation.longitude)
let userLoc = CLLocation(latitude: userLocation.latitude, longitude: userLocation.longitude)
if driverLoc.distance(from: userLoc) < 10{
// Overlapping
}
}
}
這兩個點轉換成CLLocation,然後檢查相隔多遠他們是在米。你可以在門檻周圍玩耍以獲得理想的結果。
編輯1:
這裏是一個擴展,使其更容易更容易比較的位置。
extension CLLocationCoordinate2D{
func isWithin(meters: Double, of: CLLocationCoordinate2D) -> Bool{
let currentLoc = CLLocation(latitude: self.latitude, longitude: self.longitude)
let comparingLoc = CLLocation(latitude: of.latitude, longitude: of.longitude)
return currentLoc.distance(from: comparingLoc) < meters
}
}
func payTime() {
if let driverLocation = driverLocation, let userLocation = userLocation{
if driverLocation.isWithin(meters: 10, of: userLocation){
// Overlapping
}
}
}
組合的設計:有什麼錯誤? – NRitH