的iOS迅速MapKit,别针从没有出现在地图上
,所以我一直在学习的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;
}
也许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)
}
}
}
})
我检查,但它没有做任何改变......阿肖克库马尔雷迪破虏的答复有点帮助,但不是完全,它把对城市的销到最南在我的国家(野生的猜测是多数民众赞成在我的IP痕迹通过,必须有一些东西)... ...我应该强调的另一件事情,locationManager(经理:didUpdateLocations :)多次被调用,所以我不得不使locationManager =零,可能有什么关系呢? –
调用此行代码添加在地图上的所有注释后,
self.mapView.showAnnotations(self.mapView.annotations, animated: true)
它显示引脚,但主要是它显示在我的国家南部大部分城市,离我的用户位置引脚很远(我正在使用地理坐标设置检查仿真器)。有时它可以正常工作,但并不总是如此。我疯狂的猜测是,我在离开该国之前通过那个城市的ip记录,可能与此有关,因为我不能用我所知道的任何其他解释。 另外,我把你给的代码行放到了完成处理程序的末尾,它最后执行的是因为搜索需要更多的时间。我应该移动它吗? –
放置那个更好的方法是什么? –
我想你的代码。那么引脚出现。 https://i.stack.imgur.com/4mQ8o.png –
那么,我的搜索结果比当前位置引脚的标题打印晚了1-2秒......是否可以因为那个或什么? –