我很难在用户的当前位置设置“常规”pin。我到处找,但找不到很多。现在,在用户的当前位置,我只有定位信标。我怎样才能把它改成普通的“别针”?我试过不看?.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

    }

最佳答案

我的代码使用注释。

  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)

关于swift - MapView用户的当前位置常规图钉,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49748795/

10-12 03:08