gpt4 book ai didi

swift - MapView 用户的当前位置常规 Pin

转载 作者:行者123 更新时间:2023-11-28 14:52:09 25 4
gpt4 key购买 nike

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

}

最佳答案

我的代码使用注释。

  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
}

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

 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 用户的当前位置常规 Pin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49748795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com