我是编码和堆栈溢出的新手,如果我做错了什么,请原谅我。
我正在使用 MKLocalSearch 来显示由字符串指定的位置。我有一个用户和位置,所以一切都设置好了。
我已将 MKLocalSearch 添加到我的应用程序中,它可以正常工作,但现在将 MKPointAnnotation 放在用户的位置上。当然,我希望出现著名的蓝点而不是注释。
我已经尝试过查看代码并查找此问题,但没有找到解决方案。
这是我的 MKLocalSearch 代码:
let request = MKLocalSearch.Request()
request.naturalLanguageQuery = "Dispensaries"
request.region = MapView.region
let search = MKLocalSearch(request: request)
search.start(completionHandler: {(response, error) in
if error != nil {
print("Error occured in search")
} else if response!.mapItems.count == 0 {
print("No matches found")
} else {
print("Matches found")
for item in response!.mapItems {
let annotation = MKPointAnnotation()
annotation.title = item.name
annotation.coordinate = item.placemark.coordinate
DispatchQueue.main.async {
self.MapView.addAnnotation(annotation)
}
print("Name = \(String(describing: item.name))")
print("Phone = \(String(describing: item.phoneNumber))")
print("Website = \(String(describing: item.url))")
}
}
})
这是我对注解的看法
extension MapViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
var view = mapView.dequeueReusableAnnotationView(withIdentifier: "reuseIdentifier") as? MKMarkerAnnotationView
if view == nil {
view = MKMarkerAnnotationView(annotation: nil, reuseIdentifier: "reuseIdentifier")`
let identifier = "hold"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
if annotationView == nil {
annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView?.canShowCallout = true
let btn = UIButton(type: .detailDisclosure)
annotationView?.rightCalloutAccessoryView = btn
} else {
annotationView?.annotation = annotation
}
}
view?.annotation = annotation
view?.displayPriority = .required
return view
}
}