【发布时间】:2019-12-12 16:55:24
【问题描述】:
我是编码和 Stack Overflow 的新手,如果我做错了什么,请原谅我。
我正在使用 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
}
}
【问题讨论】:
-
请编辑您的问题以显示您的
viewForAnnotation;此方法需要在接收到的注解是用户位置时返回nil,以便地图视图提供默认的注解视图(蓝点) -
感谢您的评论。我编辑了我的问题,希望能包括您正在寻找的内容。虽然,我不确定它是否正确。
标签: ios swift xcode mapkit mklocalsearch