Ios 如何获取用户当前位置和其他坐标是否相同?
我想匹配用户当前位置和其他位置,无论它们是否相同 我的逻辑:Ios 如何获取用户当前位置和其他坐标是否相同?,ios,google-maps,maps,mapkit,mapbox,Ios,Google Maps,Maps,Mapkit,Mapbox,我想匹配用户当前位置和其他位置,无论它们是否相同 我的逻辑: point.coordinate = coordinate let userLat = mapView.userLocation?.coordinate.latitude let userLong = mapView.userLocation?.coordinate.longitude if point.coordinate.latitud
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
。但是,我在模拟器和其他cooridinate中为userLocation设置了相同的值
我为用户位置设置的值为:
拉丁美洲:28.6
朗:77.35
我为其他坐标设置的值为:
拉丁美洲:28.6
朗:77.35
我得到的用户位置值是:
Lat:可选
-部分:3.4028234663852886e+38
长:▿ 可选的
-部分:3.4028234663852886e+38
我得到的其他坐标值是:
拉丁美洲:28.6
朗:77.35
如果有更好的逻辑来发现这件事,那么请告诉我。你的点对象是CLLocation子类的对象吗?如果没有,请从您的point.coordinate创建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)"
}
let distance阈值:CLLocationDistance=2.0//以米为单位
如果point.distanceFromLocation(mapView.userLocation!)
或者您可以将阈值设置为0您的点对象是CLLocation子类的对象吗?如果没有,请从您的point.coordinate创建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)"
}
let distance阈值:CLLocationDistance=2.0//以米为单位
如果point.distanceFromLocation(mapView.userLocation!)
或者您可以将阈值设置为0