我正在尝试在 MKPointAnnotations 上添加不同的图像。这是我的代码,当只有一个注释时它工作正常。我面临在不同注释上添加不同图像的问题。
func showDevices(Devs: Array<Devices.Device>){
if let annotations = self.MainMap?.annotations {
self.MainMap.removeAnnotations(annotations)
}
if let overlays = self.MainMap?.overlays {
self.MainMap.removeOverlays(overlays)
}
if Devs.count > 200{
}
else{
for Dev in Devs {
let lat: CLLocationDegrees = Dev.lt!
let lon: CLLocationDegrees = Dev.ln!
let coordinate = CLLocationCoordinate2D(latitude: lat, longitude: lon)
let mapAnnotation = MKPointAnnotation()
mapAnnotation.title = Dev.dn
mapAnnotation.subtitle = Dev.dn
mapAnnotation.coordinate = coordinate
iconImage = Common.getIcon(Dev.icon!, ign: Dev.ign!)
DispatchQueue.main.async {
self.MainMap.addAnnotation(mapAnnotation)
}
}
Common.endLoadingInNavigationCtrl(self)
}
}
这是我的mapView( :viewFor)
功能:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation ) -> MKAnnotationView? {
// Don't want to show a custom image if the annotation is the user's location.
guard !annotation.isKind(of: MKUserLocation.self) else {
return nil
}
let annotationIdentifier = "AnnotationIdentifier"
var annotationView: MKAnnotationView?
if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {
annotationView = dequeuedAnnotationView
annotationView?.annotation = annotation
}
else {
let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
av.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
annotationView = av
}
if var annotationView = annotationView {
//Configure your annotation view here
//annotationView.image = UIImage(named: iconImage!)
if iconImage != nil {
annotationView = addImageToAnnotation(annotationView, rotate: degrees, imageUrl: iconImage!)
}
}
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
annotationView?.canShowCallout = true
// Resize image
let pinImage = UIImage(named: "pin maps.png")
let size = CGSize(width: 60, height: 90)
UIGraphicsBeginImageContext(size)
pinImage!.draw(in: CGRect(x: 0, y: 1, width: size.width, height: size.height))
//(0, 0, size.width, size.height))
let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
annotationView?.image = resizedImage
let rightButton: AnyObject! = UIButton(type: UIButtonType.detailDisclosure)
annotationView?.rightCalloutAccessoryView = rightButton as? UIView
}
else {
annotationView?.annotation = annotation
}
return annotationView
}
将图像添加到引脚的功能:
func addImageToAnnotation( _ annotationView:MKAnnotationView , rotate : CGFloat? , imageUrl : String) -> MKAnnotationView {
var img = UIImage(named: imageUrl)
let size = CGSize(width:50 ,height:50);
UIGraphicsBeginImageContext(size)
img?.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
annotationView.canShowCallout = true
annotationView.image = rotate != nil ? img?.imageRotatedByDegrees(rotate!, flip: false) : img
return annotationView
}
我想为地图上的每个注释添加不同的图像,但它会覆盖所有注释上的单个图像。