【问题标题】:Annotation.coordinate does not match coordinate.latitude and coordinate.longitudeAnnotation.coordinate 与坐标.纬度和坐标.经度不匹配
【发布时间】:2018-02-27 15:27:29
【问题描述】:

在我的项目中,我使用LocationSearchTable / MKLocalSearch。当用户点击一个item时,我的add注解方法在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(纬度:37.3314132591103,...经度:-122.030484080315)

【问题讨论】:

    标签: ios swift annotations mapkit coordinates


    【解决方案1】:

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

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

    结果:

    CLLocationCoordinate2D(纬度:-33.499989999999997,经度:150.255) -33.499989999999997 150.255

    注意:

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

    【讨论】:

    • 成功了!这就是我所需要的只是将 debugDescription 添加到我的纬度和经度中,然后其他所有内容都会到位。谢谢小介:)
    猜你喜欢
    • 2011-03-31
    • 1970-01-01
    • 2016-02-03
    • 2010-09-07
    • 2013-02-14
    • 2018-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多