ios - Annotation.coordinate 不匹配 coordinate.latitude 和 coordinate.longitude

标签 ios swift annotations mapkit coordinates

在我的项目中,我使用 LocationSearchTable/MKLocalSearch。当用户点击一个项目时,我的添加注释方法在 MapViewController 中被调用:

func dropPinZoomIn(placemark: MKPlacemark) {

    // cache the pin
    selectedPin = placemark

    // create the pin
    let annotation = MKPointAnnotation()
    annotation.coordinate = placemark.coordinate
    annotation.title = placemark.name
    if  let streetNumber = placemark.subThoroughfare,
        let city = placemark.locality,
        let state = placemark.administrativeArea {
        annotation.subtitle = "\(streetNumber) \(city) \(state)"
    }

    // add the pin the the mapView
    mapView.addAnnotation(annotation)
    let span = MKCoordinateSpanMake(0.01, 0.01)
    let region = MKCoordinateRegionMake(annotation.coordinate, span)
    mapView.setRegion(region, animated: true)

    print(placemark.coordinate)
    print(placemark.coordinate.latitude)
    print(placemark.coordinate.longitude)

最后,当我打印坐标时,我得到一个更长的版本,而当我分别打印纬度和经度时,它会在接近末尾的数字四舍五入。当我需要比较坐标时,这会给我带来麻烦。我怎样才能防止这种舍入?例如,这是我的结果:

CLLocationCoordinate2D(纬度:37.331413259110334,经度:-122.03048408031462) CLLocationCoordinate2D(latitude: 37.3314132591103, . . longitude: -122.030484080315)

最佳答案

尝试使用以下代码来防止数字四舍五入。

print(coordinate)
print(coordinate.latitude.debugDescription)
print(coordinate.longitude.debugDescription)

结果:

CLLocationCoordinate2D(latitude: -33.499989999999997, longitude: 150.255) -33.499989999999997 150.255

注意:

coordinate.latitude/coordinate.longitude 的类型是CLLocationDegrees。此类型是 Double 的别名。在大多数编程语言中,用 == 比较 Double 或 Float 值不会给出预期的结果,这意味着您认为应该相等的数字实际上略有不同。相反,如果差异低于某个阈值,则计算绝对差异并将数字视为相等。参见 Compare double to zero using epsilon以获得更多解释。

关于ios - Annotation.coordinate 不匹配 coordinate.latitude 和 coordinate.longitude,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46290361/

相关文章:

java - iOS 和 Android 的移动设备唯一性

swift - SpriteKit 中的 didBeginContact() 不起作用

ios - 错误域=NSCocoaErrorDomain 代码=-1 "(null)"移动.mov 到相机胶卷时

java - 记录线程安全(Java + 注释)

java - 使用注释来计算方法的运行时间

java - 为什么 null 被禁止作为注释中的默认值?

ios - 从 plutil 获取信息

ios - 如何在ios应用程序中通过admob广告获得收入

iphone - 如何在没有 XIB 文件的情况下以编程方式更新 Xcode 中的 UILabel?

ios - ECC Curve25519 钥匙串(keychain)