2017-08-30 23 views
0

MapKitの注釈に吹き出しを追加しようとしていますが、オンラインでかなり検索した後では機能しません。注釈コールアウトが機能しないSwift

これは、現時点で正常に動作する各アノテーションを現在作成しているところです。

struct loc { 
    let title: String 
    let latitude: Double 
    let longitude: Double 
    let phone: Int 
    //let subTitle: String 
} 

var locs = [ 
    loc(title: "New York, NY", latitude: 40.713054, longitude: -74.007228, phone: 2334), //subTitle: "Sub title"), 
    ] 

func placeAnnotations() { 
    let annotations = locs.map { location -> MKAnnotation in 
     let annotation = MKPointAnnotation() 
     annotation.title = location.title 
     annotation.coordinate = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude) 

     //annotation.subtitle = location.subTitle 
     return annotation 
    } 
    mapView.addAnnotations(annotations) 
} 

(Iは、配列LOCSにデータを追加する機能を持っている)

そして、これは動作していない吹き出しを追加しようとすると、私が持っているものです。

あなたが使用
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 
    if annotation is MKUserLocation { return nil } 
    let identifier = "pin" 
    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKPinAnnotationView 
    if annotationView == nil { 
     annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier) 
     annotationView?.canShowCallout = true 
     annotationView?.rightCalloutAccessoryView = UIButton(type: .infoDark) 
    } else { 
     annotationView?.annotation = annotation 
    } 

    return annotationView 
} 
+0

です。 'mapView.delegate'を' mapView..'関数で 'UIViewController'に設定しましたか? – JuicyFruit

+0

いいえ、私は持っていないと思っています。私はそれをやろうとしましたが、クラッシュしました。これを正しく行うにはどうすればいいですか? –

+0

あなたは 'mapView.delegate = self'を行うべきです、どうしてクラッシュしますか? – JuicyFruit

答えて

1

:確かに呼び出されることはありません

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! 

。 Swift 3では、正しい署名は

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? 
+0

この問題を解決していただきありがとうございます。 –

関連する問題