点击两个按钮,我根据自己的结构(来自json对象)填充了两个不同的MKAnnotation数组。
我需要根据值(data_speed)更改这些数组之一的注释的颜色或图像。
有可能吗?我只有在使用mkuserlokation时才发现差异
var myLocationsToUse = [MKAnnotation]()
var towersArray = [TowersDataModel]()
var towersLocations = [MKAnnotation]()
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "customanotation")
// annotationView.backgroundColor = Constants.skyBlue3Color
// annotationView.contentScaleFactor = 0.5
annotationView.image = UIImage(named: "ante3")
annotationView.layer.cornerRadius = annotationView.frame.width / 2
annotationView.layer.contentsScale = 0.3
annotationView.layer.rasterizationScale = 0.3
annotationView.canShowCallout = true //shows buble
self.tappedAnnotation = annotation.title!!
let infoAnnotationButton = UIButton(type: UIButton.ButtonType.custom) as UIButton
infoAnnotationButton.frame.size.width = 44
infoAnnotationButton.frame.size.height = 44
infoAnnotationButton.backgroundColor = UIColor.blue
infoAnnotationButton.setTitle("Info", for: .normal)
infoAnnotationButton.addTarget(self, action:#selector(self.annotationButtonCallout), for: .touchUpInside)
annotationView.leftCalloutAccessoryView = infoAnnotationButton
return annotationView
}
填写人:
//grabs data from the sessions
func getJsonReadings(URLToUse: String, session: String, completion: @escaping ([PostBlueprint]) -> ()) {
//remember you need session_id
//stringa del sito
let jsonUrlString = URLToUse + session
//verifichiamo che la stringa funzioni
guard let url = URL(string: jsonUrlString) else {
print("WARNING: url related error")
return}
//iniziamo la sessione //non è sul main thread
URLSession.shared.dataTask(with: url) { (data, response, err) in
//vediamo che ci siano dei dati in "data"
guard let data = data else {return}
//check data structure before going ahead:
let dataAsString = String(data: data, encoding: .utf8)
//commented due to high resurces usage on mac itself, use only for debug:
// print("we found READINGS:\n" + dataAsString!)
do {
let dataStructs = try JSONDecoder().decode([PostBlueprint].self, from: data)
completion(dataStructs)
for (index, singleStruct) in dataStructs.enumerated() {
if let myLat = singleStruct.lat, let myLong = singleStruct.long {
let locationToShow = CLLocationCoordinate2D(latitude: myLat.SCConvertStringToDouble(),
longitude: myLong.SCConvertStringToDouble()
)
let pin = MyCustomAnnotation(
pinTitle: "Data: " + (singleStruct.data_speed ?? "unknow") + " kBs",
pinSubtitle: "log type: " + (singleStruct.log_type ?? "unknow"),
pinLocation: locationToShow
)
self.myLocationsToUse.append(pin)
}
}
} catch let jsonErr {
print("error serializing json:\n", jsonErr)
}
}.resume()
}
结构:
struct PostBlueprint: Codable {
let rsrp : String?
let lat : String?
let long : String?
let data_speed: String?
let log_type : String?
}
class MyCustomAnnotation: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var title: String?
var subtitle: String?
init(pinTitle : String, pinSubtitle: String, pinLocation: CLLocationCoordinate2D) {
coordinate = pinLocation
title = pinTitle
subtitle = pinSubtitle
}
}