将注释更改为图钉

时间:2018-07-26 11:02:43

标签: swift annotations pins

我试图做一个别针,然后单击它,标题和副标题就会出现。

到目前为止,这是我的代码:

    let span : MKCoordinateSpan = MKCoordinateSpanMake(0.1, 0.1)
    let location : CLLocationCoordinate2D = CLLocationCoordinate2DMake(40.750517, -73.987271)
    let region : MKCoordinateRegion = MKCoordinateRegionMake(location, span)
    mapView.setRegion(region, animated: true)

    let annotation = MKPointAnnotation()

    annotation.coordinate = location
    annotation.title = "Bus Stop"
    annotation.subtitle = "Street Name at Street Name"
    mapView.addAnnotation(annotation)

问题是我的气泡不是我想要的,而是我想要的,我希望在应用程序中使用的那些传统的红色图钉会在您单击更多信息时显示出来。

2 个答案:

答案 0 :(得分:0)

您必须实现委托方法才能更改注释的视图。

  func mapView(_ mapView:MKMapView,viewFor注释:MKAnnotation)-> MKAnnotationView? {
  //实施您的自定义视图以进行标注
}
 

答案 1 :(得分:0)

扩展名YourViewC:MKMapViewDelegate {

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        if annotation is MKUserLocation
        {
            return nil
        }
        var annotationView = self.mapView.dequeueReusableAnnotationView(withIdentifier: "CustomPin")
        if annotationView == nil{
            annotationView = AnnotationView(annotation: annotation, reuseIdentifier: "CustomPin")
            annotationView?.canShowCallout = false
        }else{
            annotationView?.annotation = annotation
        }
        annotationView?.image = UIImage(named: "bluecircle") // Pass name of your custom image
        return annotationView
    }
 func mapView(_ mapView: MKMapView,
             didSelect view: MKAnnotationView){ 
    let annotation = view.annotation as? CustomAnnotation
   print(annotation?.address) // get info you passed on pin

  // write code here to add custom view on tapped annotion

}