Swift 在for循环的mapbox中添加自定义标记
我有点困在这里了 我正在尝试为mapbox视图中的每个标记添加不同的图像。为了填充映射,我使用了一个查找数组的for循环:Swift 在for循环的mapbox中添加自定义标记,swift,for-loop,mapbox,mapbox-gl,Swift,For Loop,Mapbox,Mapbox Gl,我有点困在这里了 我正在尝试为mapbox视图中的每个标记添加不同的图像。为了填充映射,我使用了一个查找数组的for循环: for arrayInterest in dicoInterest { let point = MGLPointAnnotation() var latitudePoint : Double var longitudePoint : Double var typePoint : String
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很重要,否则它将始终使用相同的映像
代码未经测试,但希望原则明确