当前工作方式是在地图上移动时,它将收集给定mapview区域中的所有条形。但是,有时其中一些搜索结果/注释会消失。
是否可以在搜索后将先前的注释添加到数组,然后在地图上移动后添加所有在该先前数组中不存在的注释,以便所有结果都显示在地图上?
任何建议都将有助于实现此目标。
class BarsAroundMeVC: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate, UIGestureRecognizerDelegate{
@IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
override func viewDidLoad()
{
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
let mapDragRecognizer = UIPanGestureRecognizer(target: self, action: #selector(didDragMap(gestureRecognizer:)))
mapDragRecognizer.delegate = self
self.mapView.addGestureRecognizer(mapDragRecognizer)
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool
{
return true
}
@objc func didDragMap(gestureRecognizer: UIGestureRecognizer)
{
if (gestureRecognizer.state == UIGestureRecognizerState.began)
{
print("Map drag began")
self.locationManager.stopUpdatingLocation()
}
if (gestureRecognizer.state == UIGestureRecognizerState.ended)
{
print("Map drag ended")
self.locationManager.stopUpdatingLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
let location = locations[0]
let center = location.coordinate
let span = MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)
let region = MKCoordinateRegion(center: center, span: span)
mapView.setRegion(region, animated: true)
mapView.showsUserLocation = true
showBarsAroundUser()
}
func showBarsAroundUser()
{
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = "bar"
request.region = mapView.region
let search = MKLocalSearch(request: request)
search.start(completionHandler: {(response, error) in
if(error != nil)
{
print("Error occured in search: \(error!.localizedDescription)")
}
else if (response!.mapItems.count == 0)
{
print("No matches found")
}
else
{
for item in response!.mapItems
{
//print("Name = \(item.name)")
let annotation = MKPointAnnotation()
annotation.coordinate = item.placemark.coordinate
annotation.title = item.name
self.mapView.addAnnotation(annotation)
}
}
});
}