MapView用户的当前位置常规引脚

时间:2018-04-10 08:24:31

标签: swift annotations mkannotationview mkmapviewdelegate

我无法在用户的当前位置放置“常规”引脚。我四处搜寻但找不到多少。现在,在用户的当前位置,我只有位置信标。如何将其更改为常规“引脚”?我尝试了nnotationView?.annotation = annotation但这似乎不起作用。

extension ViewController: MKMapViewDelegate {

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

        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "AnnotationView")

        if annotationView == nil {
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "AnnotationView")
        }

        if let title = annotation.title, title == "Park Test" {

            annotationView?.image = UIImage(named: "Park icon test")

        } 

   else if annotation === mapView.userLocation {

            return nil
        }

        annotationView?.canShowCallout = true

        return annotationView

    }

1 个答案:

答案 0 :(得分:0)

我的代码使用注释。

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

    let identifier = "MyCustomAnnotation"

    if annotation is MKUserLocation { return nil }
    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKPinAnnotationView

    if annotationView == nil {
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
        annotationView?.canShowCallout = true
        annotationView?.isEnabled = true
        annotationView?.isDraggable = true
        let btn = UIButton(type: .detailDisclosure)
        annotationView?.rightCalloutAccessoryView = btn
    } else {
        annotationView?.annotation = annotation
    }
    return annotationView
}

通过添加此代码来调用委托。

 var point = MKPointAnnotation()
 self.point.coordinate = location.coordinate
 self.point.title = ("Your title")
 self.point.subtitle = (" your subs")
 self.mapView.addAnnotation(self.point)
 //selects the annotation
 self.mapView.selectAnnotation(self.point, animated: true)