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)
上記の方法を実装した後、次のエラーが表示され続けます。「タイプ 'String'の式パターンはタイプ 'String」の値と一致できません。 Mapboxにマップを埋め込むための注釈情報を含めるために、元の質問を編集しました。 –
このエラーが発生している場合は、正しく実装していません。 'switch'文はオプションの値を非オプションの値と比較することができないため、エラーが発生します。上記の答えでは、 'annotation.subtitle'の値は、' if let'ステートメントでのオプションのバインディングを通じて、省略可能な値ではなく、nilにキャストされます。 – xoudini