gpt4 book ai didi

iOS swift MapKit,从搜索回调处理程序添加的图钉未出现在 map 上

转载 作者:行者123 更新时间:2023-11-30 12:10:57 26 4
gpt4 key购买 nike

所以我一直在学习iOS,为了练习MapKit,我制作了一个应用程序,可以放大我的区域并在我的位置上放置一个图钉(给模拟器的地理坐标),这一切似乎都是工作正常,我什至可以看到引脚...

但接下来我正在使用“Shop”关键字搜索附近的商店,并在回调处理程序中打印出搜索项目的名称似乎正在显示它们。但该图钉没有出现在 map 上

我用谷歌搜索了一些,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;
}

最佳答案

也许 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)
}
}
}
})

关于iOS swift MapKit,从搜索回调处理程序添加的图钉未出现在 map 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46067008/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com