gpt4 book ai didi

swift - 在 for 循环中的 Mapbox 中添加自定义标记

转载 作者:行者123 更新时间:2023-11-30 13:32:28 25 4
gpt4 key购买 nike

我有点卡在这里......

我正在尝试为 map 框 View 中的每个标记添加不同的图像。为了填充 map ,我使用了一个 for 循环来查看数组:

    for arrayInterest in dicoInterest {

let point = MGLPointAnnotation()
var latitudePoint : Double
var longitudePoint : Double
var typePoint : String
latitudePoint = (arrayInterest["latitude"] as! Double)
longitudePoint = (arrayInterest["longitude"] as! Double)
typePoint = (arrayInterest["type"] as! String)
point.coordinate = CLLocationCoordinate2D(latitude: latitudePoint, longitude: longitudePoint)

mapView.addAnnotation(point)

print("latitude : \(latitudePoint)")
print("longitude : \(longitudePoint)")
print("point : \(point)")
print("type : \(typePoint)")


}

到目前为止一切顺利。问题是,添加我在网上找到的特定图像的唯一方法是:

func mapView(mapView: MGLMapView, imageForAnnotation annotation: MGLAnnotation) -> MGLAnnotationImage? {

var annotationImage = mapView.dequeueReusableAnnotationImageWithIdentifier(typePoint)
var image = UIImage(named: typePoint)
image = image?.imageWithAlignmentRectInsets(UIEdgeInsetsMake(0, 0, image!.size.height/2, image!.size.width/2))
annotationImage = MGLAnnotationImage(image: image!, reuseIdentifier:"\(point)")
return annotationImage
}

我添加将其放在循环之外,因此它不适用于每个标记。

还有其他方法吗?

最佳答案

您仍然可以添加不同的图像。每次添加标记时都会调用此函数,因此可以添加不同的图像。

import UIKit
import Mapbox

class yourClassController: UIViewController{

//define an image var in your class
var markerImage = UIImage(name: "defaultMarker.png")


override func viewDidLoad() {
super.viewDidLoad()

//Set new image in yout loop
for arrayInterest in dicoInterest {

//new marker Image
markerImage = UIImage(name:newImageName)
}
}

}


// MARK: - MKMapViewDelegate// Use Extension
extension yourClassController: MGLMapViewDelegate {

func mapView(mapView: MGLMapView, imageForAnnotation annotation: MGLAnnotation) -> MGLAnnotationImage? {

let annotationImage = MGLAnnotationImage(image: markerImage, reuseIdentifier: "NewIdentiferName")

return annotationImage

}
}

始终为图像设置新的reuseIdentifier很重要,否则它将始终使用相同的图像。

代码未经测试,但希望原理清楚

关于swift - 在 for 循环中的 Mapbox 中添加自定义标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36411430/

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