【发布时间】:2015-07-25 22:41:03
【问题描述】:
当我在地图上添加注释时,我想突出显示它。
我知道我必须使用
mapView1.selectAnnotation(annotation: MKAnnotation!, animated: Bool)
但我无法获取我的 MKAnnotation 对象,我只有注释的位置 (CLLocationCoordinate2D)。
我的问题是如何在知道他的位置的情况下获得特定的注释?
编辑: 理论上找到了解决方案,但有一个错误:
for annotation in self.mapView1.annotations as! [MKAnnotation] {
println(" ")
let annotLatitude = annotation.coordinate.latitude
let annotLongitude = annotation.coordinate.longitude
let eventLatitude = self.appDel!.lastEventCoords.latitude
let eventLongitude = self.appDel!.lastEventCoords.longitude
println("annotLatitude : (annotLatitude) , annotLongitude : (annotLongitude)")
println("eventLatitude : (eventLatitude) , eventLongitude : (eventLongitude)")
if (annotLatitude == eventLatitude && annotLongitude == eventLongitude) {
self.lastAnnotationAdded = annotation
println("Location equals")
self.mapView1.selectAnnotation(self.lastAnnotationAdded!, animated: true)
break
}
输出:
annotLatitude : 47.4799380142289 , annotLongitude : -0.44762351737708
eventLatitude : 47.4799380142289 , eventLongitude : -0.44762351737708
经纬度完全一样,但“if”循环没有执行!有时它有效,但有时无效。怎么可能???
EDIT2:问题已解决
为了比较坐标,我必须先用 func tronc() 截断它们
【问题讨论】:
-
因为
==并不代表你认为的那样。这些是 Double 值;您无法使用==可靠地比较它们。 -
对于任何搜索:
trunc(annotLatitude)是他在谈论的内容