Swift - 将数组中的不同图像设置为注释图钉

Swift - setting different images from array to annotation pins

我想知道如何在地图视图上将不同的图像设置为注释图钉。以下问题的区别

viewForAnnotation confusion and customizing the pinColor iteratively

Swift different images for Annotation

我的图像数组是根据服务器响应动态生成的。数组没有固定大小,所以 switch/case 构造不是一个好主意。此外,我不确定如何应用上面主题中提到的自定义 class 的解决方案。我知道最好 post 对之前提出的一个问题发表评论,但不幸的是,我现在太菜鸟了,无法做到这一点(分数太少)。

这是在显示地图的函数内部执行的 for 循环:

for var r=0;r<arrayOfRestaurants.count;r++
    {

        var summonRestaurant:NSDictionary = arrayOfRestaurants[r] as NSDictionary
        var nearbyRestaurant = Restaurant(nearbyRestaurants:summonRestaurant)
        var latRestaurant=(nearbyRestaurant.latitude as NSString).doubleValue
        var longRestaurant=(nearbyRestaurant.longitude as NSString).doubleValue
        var locationOfRestaurant = CLLocationCoordinate2D(
            latitude: latRestaurant as CLLocationDegrees, longitude: longRestaurant as CLLocationDegrees)
        var lunchArray: NSArray = nearbyRestaurant.lunch as NSArray
        var annotation = MKPointAnnotation()

        annotation.setCoordinate(locationOfRestaurant)
        annotation.title = nearbyRestaurant.name + "  " + nearbyRestaurant.distance + " km"
        map.addAnnotation(annotation)
}

这里是 viewForAnnotation 委托方法(与上述线程中使用的方法完全相同):

func mapView(map: MKMapView!,
    viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {

        if annotation is MKUserLocation {
            //return nil so map view draws "blue dot" for standard user location
            return nil
        }

        let reuseId = "pin"

        var pinView = map.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
        if pinView == nil {
            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            pinView!.canShowCallout = true
            pinView!.animatesDrop = true
            pinView!.pinColor = .Purple

            pinView!.image = globalImageArray[0]

            }
        else {
            pinView!.annotation = annotation
        }

        return pinView
}

如您所见,我将特定图像分配给 pinView,即 globalImageArray[0],但我正在寻找一种解决方案,让我遍历 globalImageArray 并将特定图像分配给每个图钉。

我很乐意得到任何帮助,在此先感谢!

首先,您需要创建自己的 class,为您的注释采用 MKAnnotation 协议 -

class RestaurantAnnotation : NSObject, MKAnnotation {
    var coordinate: CLLocationCoordinate2D
    var title: String
    var subtitle: String
    var image: UIImage?

    init(coordinate: CLLocationCoordinate2D, title: String, subtitle: String) {
        self.coordinate = coordinate
        self.title = title
        self.subtitle = subtitle
    }
}

然后,在添加注释和设置图像时使用此 class 的实例 -

for var r=0;r<arrayOfRestaurants.count;r++
    {

        var summonRestaurant:NSDictionary = arrayOfRestaurants[r] as NSDictionary
        var nearbyRestaurant = Restaurant(nearbyRestaurants:summonRestaurant)
        var latRestaurant=(nearbyRestaurant.latitude as NSString).doubleValue
        var longRestaurant=(nearbyRestaurant.longitude as NSString).doubleValue
        let locationOfRestaurant = CLLocationCoordinate2D(
            latitude: latRestaurant as CLLocationDegrees, longitude: longRestaurant as CLLocationDegrees)
        var lunchArray: NSArray = nearbyRestaurant.lunch as NSArray

        let title = nearbyRestaurant.name + "  " + nearbyRestaurant.distance +" km"
        var annotation = RestaurantAnnotation(coordinate, title:title, subtitle:"")
        annotation.image = globalImageArray[r]
        map.addAnnotation(annotation)
}

现在,在您的注释视图中,您可以访问图像 -

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
    if !(annotation is RestaurantAnnotation) {
        return nil
    }

    let reuseId = "restaurant"
    var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
    if anView == nil {
        anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        anView.canShowCallout = true
    }
    else {
        anView.annotation = annotation
    }

    let restaurantAnnotation = annotation as RestaurantAnnotation

    if (restaurantAnnotation.image != nil) {
            anView.image = restaurantAnnotation.image!
            anView.image.layer.setCornerRadius(8.0)
            anView.image.layer.clipsToBounds=true
        }
        else {
         // Perhaps set some default image
        }

    return anView
}