首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Annotation.coordinate与coordinate.latitude和coordinate.longitude不匹配

Annotation.coordinate与coordinate.latitude和coordinate.longitude不匹配
EN

Stack Overflow用户
提问于 2017-09-19 01:21:22
回答 1查看 264关注 0票数 0

在我的项目中,我使用LocationSearchTable / MKLocalSearch。当用户单击某项时,将在MapViewController中调用我的add注释方法。

代码语言:javascript
复制
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)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-19 04:25:42

尝试下面的代码,以防止回放号码。

代码语言:javascript
复制
print(coordinate)
print(coordinate.latitude.debugDescription)
print(coordinate.longitude.debugDescription)

结果:

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

注:

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46290361

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档