iOS 中 annotationView 中的自定义图钉图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32839970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Custom pin image in annotationView in iOS
提问by Jordi Gámez
I'm trying to change from Swift 1.2 to Swift 2.0 and I'm at the end of the changes. Currently I'm making changes in the MapViewController, and there isn't any error or warning, but the custom image for my pin (annotationView) it's not assigned to the pin and it's showing the default one (red dot).
我正在尝试从 Swift 1.2 更改为 Swift 2.0,但更改已结束。目前我正在 MapViewController 中进行更改,并且没有任何错误或警告,但是我的 pin (annotationView) 的自定义图像没有分配给 pin,它显示的是默认图像(红点)。
Here is my code, I hope you can help me with some tip because I think everything is fine but it's still not working:
这是我的代码,我希望你能帮我一些提示,因为我认为一切都很好,但它仍然无法正常工作:
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
}
Thanks in advance,
提前致谢,
Regards.
问候。
回答by Andrey Gordeev
The accepted answer doesn't work, as it has annotationView
uninitialized in else
block.
接受的答案不起作用,因为它annotationView
在else
块中未初始化。
Here's a better solution. It dequeues annotation view if possible or creates a new one if not:
这是一个更好的解决方案。如果可能,它将使注释视图出列,否则创建一个新视图:
Swift 3, 4:
斯威夫特 3、4:
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
}
Swift 2.2:
斯威夫特 2.2:
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
// Don't want to show a custom image if the annotation is the user's location.
guard !annotation.isKindOfClass(MKUserLocation) else {
return nil
}
let annotationIdentifier = "AnnotationIdentifier"
var annotationView: MKAnnotationView?
if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(annotationIdentifier) {
annotationView = dequeuedAnnotationView
annotationView?.annotation = annotation
}
else {
let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
av.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure)
annotationView = av
}
if let annotationView = annotationView {
// Configure your annotation view here
annotationView.canShowCallout = true
annotationView.image = UIImage(named: "yourImage")
}
return annotationView
}
回答by Jordi Gámez
Here is the answer:
答案如下:
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
let identifier = "MyPin"
if annotation.isKindOfClass(MKUserLocation) {
return nil
}
let detailButton: UIButton = UIButton(type: UIButtonType.DetailDisclosure)
if let annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "pin")
annotationView.canShowCallout = true
annotationView.image = UIImage(named: "custom_pin.png")
annotationView.rightCalloutAccessoryView = detailButton
}
else {
annotationView.annotation = annotation
}
return annotationView
}
Regards
问候
回答by esbenr
SWIFT 3,4
斯威夫特 3,4
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation.isKind(of: MKUserLocation.self) {
return nil
}
let annotationIdentifier = "AnnotationIdentifier"
var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier)
if (pinView == nil) {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
}
pinView?.canShowCallout = false
return pinView
}