如何获取用户当前位置和其他一些坐标是否相同?

How to fetch user current location and some other coordinate are same or not?

我想将用户当前位置与其他位置匹配,无论它们是否相同。

我的逻辑:

            point.coordinate = coordinate
            let userLat = mapView.userLocation?.coordinate.latitude
            let userLong = mapView.userLocation?.coordinate.longitude
            if point.coordinate.latitude == userLat && point.coordinate.longitude == userLong
            {
            point.title = "You are here"

            }
            else
            {
            point.title = "\(coordinate.latitude), \(coordinate.longitude)"
            }

但问题是,mapView.userLocation?.coordinate.latitude 不等于 point.coordinate.latitude。但是我在模拟器和其他坐标中为 userLocation 设置了相同的值。

我为用户位置设置的值是: 纬度:28.6 做多:77.35

我为其他坐标设置的值是: 纬度:28.6 做多:77.35

我得到的用户位置值是: 纬度:可选 - 一些:3.4028234663852886e+38

长:▿ 可选 - 一些:3.4028234663852886e+38

我得到的其他坐标值是: 纬度:28.6 做多:77.35

如果有更好的逻辑来找出这个东西,请告诉我。

你的point对象是CLLocation子类的对象吗?如果不是,请从 point.coordinate 创建 CLLocation 并进行如下比较。 试试这个比较 CLLocation 对象,而不是坐标,使用这种方法:

let distanceThreshold: CLLocationDistance = 2.0 // in meters
if point.distanceFromLocation(mapView.userLocation!) < distanceThreshold {
point.title = "You are here"
} else {
point.title = "\(coordinate.latitude), \(coordinate.longitude)"
}

或者您可以将阈值设置为 0