为所有不同类型的 POI 显示相同图像的自定义注释
Posted
技术标签:
【中文标题】为所有不同类型的 POI 显示相同图像的自定义注释【英文标题】:Custom annotation showing same image for all different types of POI's 【发布时间】:2016-09-04 11:58:20 【问题描述】:我需要在地图上为不同的 cat_ID 显示不同的注释。在这个问题中,我只包括两个类别。
let item = json["data"].arrayValue
var count = 0
initMK()
dispatch_async(dispatch_get_main_queue())
while count < item.count
let lat = item[count]["comp_lat"].stringValue
let lng = item[count]["comp_lng"].stringValue
if item[count]["ct_id"].stringValue == "783"
self.state = "Jachthavens"
let poi = MKPointAnnotation()
let coordinates = CLLocationCoordinate2D(latitude: Double(lat)!, longitude: Double(lng)!)
poi.coordinate = coordinates
poi.title = item[count]["comp_name_pub"].stringValue
poi.subtitle = "Jachthavens"
//poi.id
self.mapView.addAnnotation(poi)
self.mapView.reloadInputViews()
else if item[count]["ct_id"].stringValue == "788"
self.state = "Watersportwinkels"
let poi = MKPointAnnotation()
let coordinates = CLLocationCoordinate2D(latitude: Double(lat)!, longitude: Double(lng)!)
poi.coordinate = coordinates
poi.title = item[count]["comp_name_pub"].stringValue
poi.subtitle = "Watersportwinkels"
self.mapView.addAnnotation(poi)
self.mapView.reloadInputViews()
count = count + 1
///////// 这是我设置不同图像的 viewForAnnotation 方法,只有在 (state == "Watersportwinkels") 和 (state == "Jachthavens") 时才会出现问题。结果我在地图上只能看到“Jachthavens”图像,但信息窗口上的详细信息是正确的。
让标识符 = "MyPin"
if annotation.isKindOfClass(MKUserLocation)
return nil
let detailButton: UIButton = UIButton(type: UIButtonType.DetailDisclosure)
// Reuse the annotation if possible
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
if annotationView == nil
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "pin")
annotationView!.canShowCallout = true
if(state == "track")
annotationView!.image = UIImage(named: "startpin")
else if(state == "pause")
annotationView!.image = UIImage(named: "pausepin")
else if(state == "stop")
annotationView!.image = UIImage(named: "endpin")
else if (state == "Jachthavens")
annotationView!.image = UIImage(named: "jachthavenpin")
let deleteButton = UIButton(type: UIButtonType.Custom) as UIButton
deleteButton.frame.size.width = 35
deleteButton.frame.size.height = 35
deleteButton.backgroundColor = UIColor.whiteColor()
deleteButton.setImage(UIImage(named: "jachthaven"), forState: .Normal)
deleteButton.addTarget(self, action: #selector(ViewController.infoClicked(_:)), forControlEvents: .TouchUpInside)
annotationView!.leftCalloutAccessoryView = deleteButton
annotationView?.annotation = annotation
else if (state == "Watersportwinkels")
annotationView!.image = UIImage(named: "watersportwinkelspin")
let deleteButton = UIButton(type: UIButtonType.Custom) as UIButton
deleteButton.frame.size.width = 35
deleteButton.frame.size.height = 35
deleteButton.backgroundColor = UIColor.whiteColor()
deleteButton.setImage(UIImage(named: "watersportwinkels"), forState: .Normal)
deleteButton.addTarget(self, action: #selector(ViewController.infoClicked(_:)), forControlEvents: .TouchUpInside)
annotationView!.leftCalloutAccessoryView = deleteButton
annotationView?.annotation = annotation
else
annotationView!.annotation = annotation
return annotationView
【问题讨论】:
【参考方案1】:你可以尝试改变吗:
else if
进入:
else if(state == "Jachthavens")
更新:
检查字幕而不是状态:
if annotationView == nil
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "pin")
annotationView!.canShowCallout = true
if(annotationView.subtitle == "track")
annotationView!.image = UIImage(named: "startpin")
else if(annotationView.subtitle == "pause")
annotationView!.image = UIImage(named: "pausepin")
else if(annotationView.subtitle == "stop")
annotationView!.image = UIImage(named: "endpin")
else if (annotationView.subtitle == "Jachthavens")
annotationView!.image = UIImage(named: "jachthavenpin")
let deleteButton = UIButton(type: UIButtonType.Custom) as UIButton
deleteButton.frame.size.width = 35
deleteButton.frame.size.height = 35
deleteButton.backgroundColor = UIColor.whiteColor()
deleteButton.setImage(UIImage(named: "jachthaven"), forState: .Normal)
deleteButton.addTarget(self, action: #selector(ViewController.infoClicked(_:)), forControlEvents: .TouchUpInside)
annotationView!.leftCalloutAccessoryView = deleteButton
annotationView?.annotation = annotation
else if (annotationView.subtitle == "Watersportwinkels")
annotationView!.image = UIImage(named: "watersportwinkelspin")
let deleteButton = UIButton(type: UIButtonType.Custom) as UIButton
deleteButton.frame.size.width = 35
deleteButton.frame.size.height = 35
deleteButton.backgroundColor = UIColor.whiteColor()
deleteButton.setImage(UIImage(named: "watersportwinkels"), forState: .Normal)
deleteButton.addTarget(self, action: #selector(ViewController.infoClicked(_:)), forControlEvents: .TouchUpInside)
annotationView!.leftCalloutAccessoryView = deleteButton
annotationView?.annotation = annotation
else
annotationView!.annotation = annotation
【讨论】:
这是我写的实际代码中的错误(state == "Jachthavens") @KrishnaThakur 我不确定我是否理解?代码现在有效还是?当你改变这个时会发生什么? 同样的问题自定义注释没有根据过滤器改变。显示 UIImage(named: "jachthavenpin") 图像。 if (state == "Watersportwinkels") annotationView!.canShowCallout = true put: print(state) and run to see is state always "Jachthavens",对我来说,看起来你正在设置一些全局变量状态,它总是有最后一个状态你从 Json 中读到 是的,你是对的,它总是品脱“Jachthavens”所以什么应该是可选的方式以上是关于为所有不同类型的 POI 显示相同图像的自定义注释的主要内容,如果未能解决你的问题,请参考以下文章
在 Swift 中为 mapView 上的每个注释显示不同的图像