0
每当用户使用我的应用程序进行搜索时,它将返回最多100个结果的数组。我需要对每个索引进行地理编码(每个索引都有lat和lon),并在地图上放置一个标记。来自太多请求的NSURLSession和地理编码器错误,解决方案是什么?
我也需要使用NSURLSession显示一个https。然后我在指定的标记位置显示每个图像。一切工作为较低的数字 “精”,但是我得到两个显著错误:
1)对于图像的东西:
NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)
2)对于地理编码:
geoCoder error probably too many requests Optional(Error Domain=kCLErrorDomain Code=2 "(null)")
我的猜测我的要求太多了,苹果有限制。那我该如何解决这个问题?当然有办法做到这一点,或者可能是一种不同的方法?这里是显示图像的代码:
extension UIImageView {
public func imageFromUrl(urlString: String) {
if let url = NSURL(string: urlString) {
let request = NSURLRequest(URL: url)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {
(response: NSURLResponse?, data: NSData?, error: NSError?) -> Void in
if let imageData = data as NSData? {
self.image = UIImage(data: imageData)
}
}
}
}
}
这里是地理编码器的代码:
for i in (0 ..< tbc.categorizedArray.count) {
//make sure that the specified index actually has a geoLon and geoLat
if var longitude :Double = tbc.categorizedArray[i]["geoLon"] as? Double {
longitude = tbc.categorizedArray[i]["geoLon"] as! Double
let latitude :Double = tbc.categorizedArray[i]["geoLat"] as! Double
//if it does, save it's location
let location = CLLocation(latitude: latitude, longitude: longitude)
//reverseGeoCode it's location
CLGeocoder().reverseGeocodeLocation(location, completionHandler: {
placemarks, error in
if error != nil {
print("geoCoder error probably too many requests \(error)")
return
}
//place the markers
if let placemarks = placemarks {
let placemark = placemarks[0]
// Add annotation
let annotation = MKPointAnnotation()
annotation.title = tbc.categorizedArray[i]["screenname"] as? String
annotation.subtitle = tbc.categorizedArray[i]["userPost"] as? String
if let location = placemark.location {
annotation.coordinate = location.coordinate
// Display the annotation
self.mapView.showAnnotations([annotation], animated: true)
self.mapView.selectAnnotation(annotation, animated: true)
}
}
})
}