gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 06:23:50 26 4
gpt4 key购买 nike

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

viewForAnnotation confusion and customizing the pinColor iteratively

Swift different images for Annotation

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

这是在显示 map 的函数内部执行的 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 委托(delegate)方法(与上述线程中使用的方法完全相同):

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 并将特定图像分配给每个图钉。

我很乐意收到任何帮助,提前致谢!

最佳答案

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

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
}
}

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

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)
}

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

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
}

关于ios - Swift - 将不同的图像从数组设置为注释图钉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28033159/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com