在 Mapbox iOS SDK 中创建 switch 语句
Creating switch statements in Mapbox iOS SDK
我正在尝试为 Mapbox iOS
中的 leftCalloutAccessoryViewForAnnotation
创建一个 switch 语句。我尝试了各种方法,包括创建 CustomPointAnnotation
class 和重用标识符,但都无法正常工作。
最后,我创建了您在下面看到的内容。这不是我想使用的那种代码。任何输入将不胜感激。
func mapView(mapView: MGLMapView, leftCalloutAccessoryViewForAnnotation annotation: MGLAnnotation) -> UIView? {
if (annotation.subtitle! == "Name") {
let imageView = UIImageView(image: UIImage(named: "imageName")!)
self.view.addSubview(imageView)
return imageView
}
if (annotation.subtitle! == "Name2"){
let imageView = UIImageView(image: UIImage(named: "imageName2")!)
self.view.addSubview(imageView)
return imageView
}
if (annotation.subtitle! == "Name3"){
let imageView = UIImageView(image: UIImage(named: "imageName3")!)
self.view.addSubview(imageView)
return imageView
}
return nil
}
注释
for location in locations {let annotation = MGLPointAnnotation()
let coordinate = CLLocationCoordinate2DMake(location.latitude, location.longitude);
annotation.coordinate = coordinate
annotation.title = location.title
annotation.subtitle = location.subtitle
annotations.append(annotation)
mapView.delegate = self
mapView.addAnnotations(annotations)
应该这样做:
func mapView(mapView: MGLMapView, leftCalloutAccessoryViewForAnnotation annotation: MGLAnnotation) -> UIView? {
var imageView = UIImageView()
imageView.frame = ... // Set the frame for the imageView.
// Optional binding to either create non-optional value subtitle or escape the statement.
if let subtitle = annotation.subtitle {
// subtitle is a non-optional value, cases must also be non-optional.
switch subtitle {
case "Name": imageView.image = UIImage(named: "imageName")
case "Name2": imageView.image = UIImage(named: "imageName2")
case "Name3": imageView.image = UIImage(named: "imageName3")
default: return nil
}
self.view.addSubview(imageView)
return imageView
}
return nil
}
如果不想使用可选绑定,可以采用另一种方法:
func mapView(mapView: MGLMapView, leftCalloutAccessoryViewForAnnotation annotation: MGLAnnotation) -> UIView? {
var imageView = UIImageView()
imageView.frame = ... // Set the frame for the imageView.
// annotation.subtitle is an optional value, cases must also be optional.
switch annotation.subtitle {
case nil: return nil
case "Name"?: imageView.image = UIImage(named: "imageName")
case "Name2"?: imageView.image = UIImage(named: "imageName2")
case "Name3"?: imageView.image = UIImage(named: "imageName3")
default: return nil
}
self.view.addSubview(imageView)
return imageView
}
根据字幕切换不是一个好方法。您的逻辑基于 UI 层,这是向后的。除了令人困惑的体系结构之外,当您本地化您的应用程序时,它会崩溃。
您应该子类化 MGLPointAnnotation
并添加一个 属性 以表明它是什么类型的点。我会做类似的事情:
enum PointType {
case FirstPointType
case SecondPointType
case ThirdPointType
var imageName: String {
get {
/*
You can implement whatever logic you'd like here, but if
you follow the pattern of "FirstPointType.png" for your
images, defaulting to the case name is probably easiest.
*/
return String(self)
}
}
var image: UIImage? {
get {
return UIImage(named: imageName())
}
}
}
class CustomAnnotation: MGLAnnotation {
var pointType: PointType
}
func mapView(mapView: MGLMapView, leftCalloutAccessoryViewForAnnotation annotation: MGLAnnotation) -> UIView? {
guard let annotation = annotation as? CustomAnnotation else { return nil }
let imageView = UIImageView(image: annotation.pointType.image)
self.view.addSubview(imageView)
return imageView
}
我正在尝试为 Mapbox iOS
中的 leftCalloutAccessoryViewForAnnotation
创建一个 switch 语句。我尝试了各种方法,包括创建 CustomPointAnnotation
class 和重用标识符,但都无法正常工作。
最后,我创建了您在下面看到的内容。这不是我想使用的那种代码。任何输入将不胜感激。
func mapView(mapView: MGLMapView, leftCalloutAccessoryViewForAnnotation annotation: MGLAnnotation) -> UIView? {
if (annotation.subtitle! == "Name") {
let imageView = UIImageView(image: UIImage(named: "imageName")!)
self.view.addSubview(imageView)
return imageView
}
if (annotation.subtitle! == "Name2"){
let imageView = UIImageView(image: UIImage(named: "imageName2")!)
self.view.addSubview(imageView)
return imageView
}
if (annotation.subtitle! == "Name3"){
let imageView = UIImageView(image: UIImage(named: "imageName3")!)
self.view.addSubview(imageView)
return imageView
}
return nil
}
注释
for location in locations {let annotation = MGLPointAnnotation()
let coordinate = CLLocationCoordinate2DMake(location.latitude, location.longitude);
annotation.coordinate = coordinate
annotation.title = location.title
annotation.subtitle = location.subtitle
annotations.append(annotation)
mapView.delegate = self
mapView.addAnnotations(annotations)
应该这样做:
func mapView(mapView: MGLMapView, leftCalloutAccessoryViewForAnnotation annotation: MGLAnnotation) -> UIView? {
var imageView = UIImageView()
imageView.frame = ... // Set the frame for the imageView.
// Optional binding to either create non-optional value subtitle or escape the statement.
if let subtitle = annotation.subtitle {
// subtitle is a non-optional value, cases must also be non-optional.
switch subtitle {
case "Name": imageView.image = UIImage(named: "imageName")
case "Name2": imageView.image = UIImage(named: "imageName2")
case "Name3": imageView.image = UIImage(named: "imageName3")
default: return nil
}
self.view.addSubview(imageView)
return imageView
}
return nil
}
如果不想使用可选绑定,可以采用另一种方法:
func mapView(mapView: MGLMapView, leftCalloutAccessoryViewForAnnotation annotation: MGLAnnotation) -> UIView? {
var imageView = UIImageView()
imageView.frame = ... // Set the frame for the imageView.
// annotation.subtitle is an optional value, cases must also be optional.
switch annotation.subtitle {
case nil: return nil
case "Name"?: imageView.image = UIImage(named: "imageName")
case "Name2"?: imageView.image = UIImage(named: "imageName2")
case "Name3"?: imageView.image = UIImage(named: "imageName3")
default: return nil
}
self.view.addSubview(imageView)
return imageView
}
根据字幕切换不是一个好方法。您的逻辑基于 UI 层,这是向后的。除了令人困惑的体系结构之外,当您本地化您的应用程序时,它会崩溃。
您应该子类化 MGLPointAnnotation
并添加一个 属性 以表明它是什么类型的点。我会做类似的事情:
enum PointType {
case FirstPointType
case SecondPointType
case ThirdPointType
var imageName: String {
get {
/*
You can implement whatever logic you'd like here, but if
you follow the pattern of "FirstPointType.png" for your
images, defaulting to the case name is probably easiest.
*/
return String(self)
}
}
var image: UIImage? {
get {
return UIImage(named: imageName())
}
}
}
class CustomAnnotation: MGLAnnotation {
var pointType: PointType
}
func mapView(mapView: MGLMapView, leftCalloutAccessoryViewForAnnotation annotation: MGLAnnotation) -> UIView? {
guard let annotation = annotation as? CustomAnnotation else { return nil }
let imageView = UIImageView(image: annotation.pointType.image)
self.view.addSubview(imageView)
return imageView
}