用于多个注释的 MapKit 自定义图像
MapKit custom image for multiple annotations
我有新闻对象,想根据新闻类别自定义新闻标注。如果没有switch语句,如果我只写annotationView?.image = UIImage(named: "ic_sports")
,所有注解;图片显示运动图像。如何获取该注释的新闻类别 ID,以便根据该 ID 更改注释图像?
控制台为所有注释打印 Log annotation is news
。
新闻:
class News: NSObject, MKAnnotation {
let coordinate: CLLocationCoordinate2D
var categoryId: Int
var title: String?
// Other variables and init function ...
func getCategoryId() -> Int {
return categoryId
}
}
地图视图控制器:
function parse(json: JSON) {
// ...
let news = News(categoryId: data["category_id"].intValue,
title: data["title"].stringValue,
coordinate: coordinate)
self.mapView.addAnnotation(news)
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is News {
print("Log annotation is news")
} else {
print("Log annotation NOT news")
}
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "newsAnnotationView")
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "newsAnnotationView")
}
switch(annotation.getCategoryId()) {
case 1: // Other
annotationView?.image = UIImage(named: "ic_other")
break;
case 2: // sports
annotationView?.image = UIImage(named: "ic_sports")
break;
case 3: // education
annotationView?.image = UIImage(named: "ic_education")
break;
case 4: // crime
annotationView?.image = UIImage(named: "ic_crime")
break;
case 5: // health
annotationView?.image = UIImage(named: "ic_health")
break;
default:
break;
}
annotationView?.canShowCallout = true
return annotationView
}
我已经解决了这个问题,方法是创建一个新闻对象并将 annotation
的值作为新闻赋予它,然后使用 news.getCategoryId()
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let news = annotation as! News
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "newsAnnotationView")
if annotationView == nil {
annotationView = MKAnnotationView(annotation: news, reuseIdentifier: "newsAnnotationView")
}
switch(news.getCategoryId()) {
case 1: // Other
annotationView?.image = UIImage(named: "ic_other")
break;
case 2: // sports
annotationView?.image = UIImage(named: "ic_sports")
break;
case 3: // education
annotationView?.image = UIImage(named: "ic_education")
break;
case 4: // crime
annotationView?.image = UIImage(named: "ic_crime")
break;
case 5: // health
annotationView?.image = UIImage(named: "ic_health")
break;
default:
break;
}
annotationView?.canShowCallout = true
return annotationView
}
我有新闻对象,想根据新闻类别自定义新闻标注。如果没有switch语句,如果我只写annotationView?.image = UIImage(named: "ic_sports")
,所有注解;图片显示运动图像。如何获取该注释的新闻类别 ID,以便根据该 ID 更改注释图像?
控制台为所有注释打印 Log annotation is news
。
新闻:
class News: NSObject, MKAnnotation {
let coordinate: CLLocationCoordinate2D
var categoryId: Int
var title: String?
// Other variables and init function ...
func getCategoryId() -> Int {
return categoryId
}
}
地图视图控制器:
function parse(json: JSON) {
// ...
let news = News(categoryId: data["category_id"].intValue,
title: data["title"].stringValue,
coordinate: coordinate)
self.mapView.addAnnotation(news)
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is News {
print("Log annotation is news")
} else {
print("Log annotation NOT news")
}
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "newsAnnotationView")
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "newsAnnotationView")
}
switch(annotation.getCategoryId()) {
case 1: // Other
annotationView?.image = UIImage(named: "ic_other")
break;
case 2: // sports
annotationView?.image = UIImage(named: "ic_sports")
break;
case 3: // education
annotationView?.image = UIImage(named: "ic_education")
break;
case 4: // crime
annotationView?.image = UIImage(named: "ic_crime")
break;
case 5: // health
annotationView?.image = UIImage(named: "ic_health")
break;
default:
break;
}
annotationView?.canShowCallout = true
return annotationView
}
我已经解决了这个问题,方法是创建一个新闻对象并将 annotation
的值作为新闻赋予它,然后使用 news.getCategoryId()
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let news = annotation as! News
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "newsAnnotationView")
if annotationView == nil {
annotationView = MKAnnotationView(annotation: news, reuseIdentifier: "newsAnnotationView")
}
switch(news.getCategoryId()) {
case 1: // Other
annotationView?.image = UIImage(named: "ic_other")
break;
case 2: // sports
annotationView?.image = UIImage(named: "ic_sports")
break;
case 3: // education
annotationView?.image = UIImage(named: "ic_education")
break;
case 4: // crime
annotationView?.image = UIImage(named: "ic_crime")
break;
case 5: // health
annotationView?.image = UIImage(named: "ic_health")
break;
default:
break;
}
annotationView?.canShowCallout = true
return annotationView
}