【问题标题】:Find Annotation on map with location在带有位置的地图上查找注释
【发布时间】: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) 是他在谈论的内容

标签: ios swift xcode6


【解决方案1】:

MKMapView 有一个注释列表,它的annotations 属性。因此,请浏览这些内容,直到找到您想要的那个(正确的是 coordinate 的那个)。

【讨论】:

  • 添加了奇怪的错误,尽管比较的值完全相等,但有时循环不会执行
猜你喜欢
  • 2012-02-27
  • 2017-11-16
  • 1970-01-01
  • 2013-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-21
  • 1970-01-01
相关资源
最近更新 更多