2017-08-30 133 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) 
} 

(我有一个函数,将数据添加到阵列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..'函数将'mapView.delegate'设置为'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

谢谢你的帮助,解决了这个问题。 –