在我的项目中,我使用LocationSearchTable / MKLocalSearch。当用户单击某项时,将在MapViewController中调用我的add注释方法。
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)
发布于 2017-09-19 04:25:42
尝试下面的代码,以防止回放号码。
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。
https://stackoverflow.com/questions/46290361
复制相似问题