将注释更改为图钉

Changing an annotation to a pin

我试着做一个图钉,你点击它就会出现标题和副标题。

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

    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)

问题是我的出现了一个圆形气泡,这不是我想要的,我想要应用程序中使用的那些传统的红色图钉,当您单击更多信息时会显示这些图钉。

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

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
  //Implement Your custom view for annoation
}

扩展 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

}