gpt4 book ai didi

ios - 使用计时器更改注释上的图像 - Swift

转载 作者:行者123 更新时间:2023-11-30 14:12:33 25 4
gpt4 key购买 nike

我从服务器获取数组,并且必须在 map 上显示带有坐标的注释(带有坐标的数组)我有数组“状态”。数组的值必须更改注释图像(1 = 绿色图片,2 - 红色图片,3 - 黄色图片)所以这是工作但是,当我更新位置数组和状态数组时,我的图片发生了变化。当我获得另一种状态时,形象必须改变,但任何时候,都会有变化。如何解决这个问题?

class SuperPin: MKPointAnnotation {
var lon: Double!
var lat: Double!
var name: String!
var status: String!
}


func DriverData(string: String) {
if (string != nil)
//there I'm get my array
var t = 0
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> Void in
let ann = self.myMap.annotations.filter { $0 !== self.myMap.userLocation }
self.myMap.removeAnnotations(ann)
dispatch_async(dispatch_get_main_queue(), {
do {
var pin: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: self.longitude[t].doubleValue, longitude: self.latitude[t].doubleValue)

var my_color = SuperPin()

if (self.status[t].toInt() == 1){
my_color.status = "1.png"
} else {
if (self.status[t].toInt() == 2){
my_color.status = "2.png"
} else {
my_color.status = "3.png"
}
}
my_color.lat = self.latitude[t].doubleValue
my_color.lon = self.longitude[t].doubleValue
my_color.coordinate.latitude = my_color.lon
my_color.coordinate.longitude = my_color.lat
my_color.title = self.name[t]
self.myMap.addAnnotation(my_color)
t++
} while (t < namearr.count)
})
})
} else {
timer1.invalidate()
}
}





func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
let reuseID = "test"
var anView = myMap.dequeueReusableAnnotationViewWithIdentifier(reuseID) as? MKPinAnnotationView
if (annotation is MKUserLocation) {
anView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseID)
anView!.image = UIImage(named: "usrloc")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
anView!.canShowCallout = true
}

if anView == nil {
anView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseID)
let cpa = annotation as! SuperPin
anView!.image = UIImage(named: cpa.status)
anView!.canShowCallout = true
} else {
anView!.annotation = annotation
}

return anView
}

最佳答案

--> 首先从 map View 中删除来自旧数组的所有注释,然后您将获取或更新您的位置并在 map 上获取新注释,因此请尝试以下逻辑。它对我很有帮助,也可能对你有帮助。

var arrannotations = NSMutableArray();
map.removeAnnotations(map.annotations);
for var i = 0; i <arrannotations; i++
{
map.addAnnotation(arrannotations.objectAtIndex[i])
}

关于ios - 使用计时器更改注释上的图像 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31606660/

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