计算将多少自定义注释添加到mapView swift 4

时间:2018-11-08 23:15:08

标签: swift annotations mapkit

我觉得这很简单,我只是想看看。我有一张地图,用户按下按钮即可添加注释。

   func addPinToPath() {

    let catchPathPin = CatchAnnotation()

    let catchLat = map.userLocation.coordinate.latitude
    let catchLon = map.userLocation.coordinate.longitude

    catchPathPin.coordinate = CLLocationCoordinate2D(latitude: catchLat, longitude: catchLon)
    catchPathPin.title = "Fish On"
    catchPathPin.subtitle = "Path"
    catchPathPin.annoID = annoIDStart
    catchPathPin.pinImageName = "catch"

    let idToPass = catchPathPin.annoID

    annoIDCatch = idToPass!

    print(annoIDCatch)

    map.addAnnotation(catchPathPin)

    bitesInRoute = catchPathPin


}

这时,我希望有一个计数器在显示屏上显示添加了多少注释。

annotations.count将为我提供所有注释的计数,但我只希望CatchPathPin的计数

1 个答案:

答案 0 :(得分:1)

有两个选项。您可能有一个计数变量,您每次都会增加 addPinToPath被调用。另一种选择是过滤当前注释并从那里获取计数。

count = map.annotations
    .filter { $0 is CatchAnnotation }
    .count
相关问题