,所以我一直在学习的iOS搜索回调处理程序添加,和我的实践MapKit,我已经提出,将放大我的区域,并把针在我的应用程序位置(地理坐标提供给模拟器),一切都似乎是工作,我甚至可以看见针...的iOS迅速MapKit,别针从没有出现在地图上
不过下次我在做什么与“店铺”关键字附近的商店搜索并打印出名字回调处理程序中的搜索项似乎正在显示它们。但是的针脚并未出现在地图上。
我google了一些,mapView(mapView:viewFor:) - > MKAnnotation似乎不是一个懒惰的函数,它必须出队并创建新的MKPinAnnotationView对象并返回它们....我之所以检查懒惰的原因是如果我打印每个引脚的名称是从内部返回它不显示任何打印,但如果我把一个断点在那里,它会显示它然后...
除此之外,最初几次引脚显示,所以我知道添加它们的代码工作,但后来我添加标题到每个引脚,现在我不知道它是否需要很长时间来搜索它们,应用程序完成其工作,直到那时或某事或不...
import来自代码的蚂蚁位:
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;
}
我想你的代码。那么引脚出现。 https://i.stack.imgur.com/4mQ8o.png –
那么,我的搜索结果比当前位置引脚的标题打印晚了1-2秒......是否可以因为那个或什么? –