所以我一直在学习iOS,而对于我在MapKit上的练习,我已经制作了一个应用程序放大我的区域并在我的位置放置一个引脚(给模拟器的地理坐标),似乎一切工作,我甚至可以看到针......
但接下来我正在寻找附近的商店' Shop'关键字和打印出回调处理程序中搜索项的名称似乎正在显示它们。但的引脚没有出现在地图上。
我google了一些,mapView(mapView:viewFor :) - > MKAnnotation似乎不是一个懒惰的函数,它必须出列并生成新的MKPinAnnotationView对象并返回它们....我检查懒惰的原因是如果我打印从内部返回的每个引脚的名称,它不显示任何打印,但如果我在那里放一个断点,它会显示它...
除此之外,最初几次引脚显示,所以我知道添加它们的代码是有效的,但后来我为每个引脚添加了标题,现在我不知道它是否花了很长时间来搜索它们和应用程序完成其工作直到那时或某事或不...
代码中的重要部分:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager.delegate = self
mapView.delegate = self
if CLLocationManager.authorizationStatus() == .notDetermined {
self.locationManager.requestWhenInUseAuthorization()
}
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
manager.stopUpdatingLocation()
locationManager.delegate = nil
centerMapOnLocation(location: manager.location!)
// Searching for shops
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = "Shop"
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 {
print("Matches found")
for item in response!.mapItems {
let shopPin = MKPointAnnotation()
shopPin.coordinate = item.placemark.coordinate
shopPin.title = item.name
self.mapView.addAnnotation(shopPin)
print("Name = \(item.name!)")
//print(shopPin.title)
}
}
})
// Putting a pin on current location
let homePin = MKPointAnnotation()
homePin.coordinate = (manager.location?.coordinate)!
homePin.title = "BitSol Technologies"
mapView.addAnnotation(homePin)
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let pinIdent = "Pin";
var pinView: MKPinAnnotationView;
if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: pinIdent) as? MKPinAnnotationView {
dequeuedView.annotation = annotation;
pinView = dequeuedView;
}else{
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: pinIdent);
}
if annotation.coordinate.latitude == locationManager.location!.coordinate.latitude && annotation.coordinate.longitude == locationManager.location!.coordinate.longitude{
pinView.pinTintColor = MKPinAnnotationView.redPinColor();
}else{
pinView.pinTintColor = MKPinAnnotationView.purplePinColor()
}
pinView.canShowCallout = true
print(annotation.title!)
return pinView;
}
答案 0 :(得分:0)
也许MKLocalSearch完成处理程序不能在主线程上运行。请尝试以下代码:
let search = MKLocalSearch(request: request)
search.start(completionHandler: {(response, error) in
// Go back to the main thread to update the UI
DispatchQueue.main.async {
if error != nil {
print("Error occured in search: \(error!.localizedDescription)")
} else if response!.mapItems.count == 0 {
print("No matches found")
} else {
print("Matches found")
for item in response!.mapItems {
let shopPin = MKPointAnnotation()
shopPin.coordinate = item.placemark.coordinate
shopPin.title = item.name
self.mapView.addAnnotation(shopPin)
print("Name = \(item.name!)")
//print(shopPin.title)
}
}
}
})
答案 1 :(得分:0)
在地图上添加所有注释后调用此行代码
self.mapView.showAnnotations(self.mapView.annotations, animated: true)