【问题标题】:MapView User's Current Location Regular PinMapView 用户当前位置常规图钉
【发布时间】:2018-04-10 08:24:31
【问题描述】:

我无法在用户当前位置放置“常规”图钉。我四处寻找,但找不到太多。现在,在用户的当前位置,我只有位置信标。我怎样才能把它改成普通的“别针”?我试过 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

    }

【问题讨论】:

    标签: swift annotations mkannotationview mkmapviewdelegate


    【解决方案1】:

    我的代码使用注释。

      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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多