gpt4 book ai didi

ios - iOS 中 annotationView 中的自定义图钉图像

转载 作者:IT王子 更新时间:2023-10-29 05:13:38 25 4
gpt4 key购买 nike

我正在尝试从 Swift 1.2 更改为 Swift 2.0,我正处于更改的末尾。目前我正在 MapViewController 中进行更改,没有任何错误或警告,但我的图钉 (annotationView) 的自定义图像未分配给图钉,它显示默认图像(红点)。

这是我的代码,我希望你能给我一些提示,因为我认为一切都很好,但它仍然无法正常工作:

func parseJsonData(data: NSData) -> [Farmacia] {

let farmacias = [Farmacia]()

do
{
let jsonResult = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary

// Parse JSON data
let jsonProductos = jsonResult?["farmacias"] as! [AnyObject]

for jsonProducto in jsonProductos {

let farmacia = Farmacia()
farmacia.id = jsonProducto["id"] as! String
farmacia.nombre = jsonProducto["nombre"] as! String
farmacia.location = jsonProducto["location"] as! String

let geoCoder = CLGeocoder()
geoCoder.geocodeAddressString(farmacia.location, completionHandler: { placemarks, error in

if error != nil {
print(error)
return
}

if placemarks != nil && placemarks!.count > 0 {

let placemark = placemarks?[0]

// Add Annotation
let annotation = MKPointAnnotation()
annotation.title = farmacia.nombre
annotation.subtitle = farmacia.id
annotation.coordinate = placemark!.location!.coordinate

self.mapView.addAnnotation(annotation)
}

})
}
}
catch let parseError {
print(parseError)
}

return farmacias
}

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

let identifier = "MyPin"

if annotation.isKindOfClass(MKUserLocation) {
return nil
}

// Reuse the annotation if possible
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)

if annotationView == nil
{
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView!.canShowCallout = true
}

annotationView!.image = UIImage(named: "custom_pin.png")

let detailButton: UIButton = UIButton(type: UIButtonType.DetailDisclosure)
annotationView!.rightCalloutAccessoryView = detailButton

print(annotationView!.image)

return annotationView
}

提前致谢

问候。

最佳答案

已接受的答案无效,因为它在 else block 中未初始化 annotationView

这是一个更好的解决方案。如果可能,它会出列注释 View ,否则会创建一个新 View :

// https://stackoverflow.com/a/38159048/1321917
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 is MKUserLocation) else {
return nil
}

// Better to make this class property
let annotationIdentifier = "AnnotationIdentifier"

var annotationView: MKAnnotationView?
if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {
annotationView = dequeuedAnnotationView
annotationView?.annotation = annotation
}
else {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
}

if let annotationView = annotationView {
// Configure your annotation view here
annotationView.canShowCallout = true
annotationView.image = UIImage(named: "yourImage")
}

return annotationView
}

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

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