gpt4 book ai didi

ios - 多个 MKPointAnnotations 上的自定义图像

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

我正在尝试在 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
}

我想为 map 上的每个注释添加不同的图像,但它会覆盖所有注释上的单个图像。

最佳答案

创建自定义 MKAnnotation 类。

class ImageAnnotation : NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var title: String?
var subtitle: String?
var imageUrl: String?

override init() {
self.coordinate = CLLocationCoordinate2D()
self.title = nil
self.subtitle = nil
self.imageUrl = nil
}
}

设置数据并将注释添加到您的 map View 。

    let annotation = ImageAnnotation()
annotation.coordinate = coordinate1
annotation.title = "title"
annotation.subtitle = "subtitle"
annotation.imageUrl = "https:// ..."
self.mapView.addAnnotation(annotation)

实现 mapView(_ mapView: MKMapView, viewFor comment: MKAnnotation) 委托(delegate)方法。

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard let annotation = annotation as? ImageAnnotation else {
return nil
}

let reuseId = "Pin"
var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
if pinView == nil {
pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView?.canShowCallout = true
let data = NSData(contentsOf: URL(string: annotation.imageUrl!)!)
pinView?.image = UIImage(data: data! as Data)
}
else {
pinView?.annotation = annotation
}

return pinView
}

关于ios - 多个 MKPointAnnotations 上的自定义图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52924519/

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