统计有多少自定义注释被添加到 mapView swift 4

Getting a count on how many custom annotations are added to a mapView swift 4

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

   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

的计数

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

count = map.annotations
    .filter { [=10=] is CatchAnnotation }
    .count