2016-07-16 11 views
0

leftCalloutAccessoryViewForAnnotationのswitch文をMapbox iOSに作成しようとしています。私はCustomPointAnnotationクラスと再利用識別子を作成するなど、さまざまな方法を試しましたが、それを動作させることはできませんでした。Mapbox iOS SDKでswitch文を作成

最後に、私はあなたが以下に示すものを作成しました。私が使用したいコードのようなものではありません。どんな入力も高く評価されます。

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) 

答えて

1

切り替え字幕はagではないoodアプローチ。あなたは、あなたのロジックを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 
} 
0

これは、実行する必要があります。

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 
} 
+0

上記の方法を実装した後、次のエラーが表示され続けます。「タイプ 'String'の式パターンはタイプ 'String」の値と一致できません。 Mapboxにマップを埋め込むための注釈情報を含めるために、元の質問を編集しました。 –

+0

このエラーが発生している場合は、正しく実装していません。 'switch'文はオプションの値を非オプションの値と比較することができないため、エラーが発生します。上記の答えでは、 'annotation.subtitle'の値は、' if let'ステートメントでのオプションのバインディングを通じて、省略可能な値ではなく、nilにキャストされます。 – xoudini

関連する問題