迅速,我正在使用地图视图。当我尝试更改地图视图时,当我调用api并传递数组时,图钉未更改。但是当我在视图上添加大头针确实加载了它时,地图视图大头针图像已更改。
查看加载代码是否正常工作。
override func viewDidLoad() {
super.viewDidLoad()
let london = MapPin(title: "London", subtitle: "Home to the 2012 Summer Olympics.", coordinate: CLLocationCoordinate2D(latitude: 51.507222, longitude: -0.1275))
let oslo = MapPin(title: "oslo", subtitle: "Home to the 2012 Summer Olympics.", coordinate: CLLocationCoordinate2D(latitude: 51.506222, longitude: -0.1275))
let mylocation = MyLocation(title: "myLocation", subtitle: "", coordinate: CLLocationCoordinate2D(latitude: 51.506522, longitude: -0.1225))
mapView.addAnnotations([london,oslo,mylocation])
mapView.addAnnotations([london])
mapView.showAnnotations(mapView.annotations, animated: true)
mapView.showsUserLocation = true
mapView.centerCoordinate = CLLocationCoordinate2D(latitude: 51.506522, longitude: -0.1225)
}
但是当我尝试在调用api之后更新mapview时,它不起作用
self.aryData = (response as! NSDictionary).value(forKey: "category") as? NSArray ?? NSArray()
// mapView
DispatchQueue.main.async {
let allAnnotations = self.mapView.annotations
self.mapView.removeAnnotations(allAnnotations)
// var aryAnotation:[MapPin] = [MapPin]()
let aryAnotation:NSMutableArray = NSMutableArray()
for i in 0..<self.aryData.count {
let getobj:NSDictionary = self.aryData.object(at: i) as? NSDictionary ?? NSDictionary()
let getTitle:String = getobj.value(forKey: "offer_title") as? String ?? ""
let getlat:String = getobj.value(forKey: "lat") as? String ?? ""
let getlong:String = getobj.value(forKey: "lng") as? String ?? ""
let lat:Double = Double(getlat) ?? 0.0
let long:Double = Double(getlong) ?? 0.0
let theAnotation = MapPin(title: getTitle, subtitle: "", coordinate: CLLocationCoordinate2D(latitude: lat, longitude: long), tag: i)
// aryAnotation.append(theAnotation)
//add(theAnotation)
aryAnotation.add(theAnotation)
}
let latuser:Double = Double(ApplicationManager.instance.user_lat) ?? 0.0
let longUser:Double = Double(ApplicationManager.instance.user_long) ?? 0.0
let mylocation = MyLocation(title: "myLocation", subtitle: "", coordinate: CLLocationCoordinate2D(latitude: latuser, longitude: longUser))
// aryAnotation.append(mylocation)
aryAnotation.add(mylocation)
self.mapView.addAnnotations(aryAnotation as! [MKAnnotation])
self.mapView.showAnnotations(self.mapView.annotations, animated: true)
HERE是我的地图查看代码
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let identifier = "MapPin"
if annotation is MapPin {
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
if annotationView == nil {
//annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView?.canShowCallout = false
} else {
annotationView?.annotation = annotation
}
annotationView?.image = #imageLiteral(resourceName: "mapSet")
return annotationView
}else
{
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView?.canShowCallout = true
} else {
annotationView?.annotation = annotation
}
annotationView?.image = UIImage(named:"mylocation")
return annotationView
}
}
请帮助我。调用api后,固定图片未更改,即使我在api调用后使用的相同代码(viewdidload)代码也未更新固定图片。 我在做什么错。