gpt4 book ai didi

ios - CLGeocoder 地理编码请求超出限制

转载 作者:行者123 更新时间:2023-11-30 13:28:35 26 4
gpt4 key购买 nike

我正在尝试找出方法来限制我向 CLGeocoder 发出的请求数量,因为我不断超出最大限制。我得到“Error Domain=kCLErrorDomain Code = 2”(null)”。基本上,每次有人在 func RegionDidChangeAnimated 中移动 map 时,我都会得到他们确定的坐标,通过地理编码来获取城市/state,然后点击我的 API,然后使用这些结果对该地区的新 map 图钉进行地理编码。有人有任何最佳实践来处理这种情况吗?非常感谢任何帮助。

查看用户是否更改 map 区域的函数:

private func mapViewRegionDidChangeFromUserInteraction() -> Bool {
let view: UIView = self.mapView.subviews[0] as UIView
// Look through gesture recognizers to determine whether this region change is from user interaction
if let gestureRecognizers = view.gestureRecognizers {
for recognizer in gestureRecognizers {
if( recognizer.state == UIGestureRecognizerState.Began || recognizer.state == UIGestureRecognizerState.Ended ) {
return true
}
}
}
return false
}

func mapView(mapView: MKMapView, regionWillChangeAnimated animated: Bool) {
mapChangedFromUserInteraction = mapViewRegionDidChangeFromUserInteraction()
if (mapChangedFromUserInteraction) {
// user changed map region

}
}

func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
if (mapChangedFromUserInteraction) {
// user changed map region
let center = mapView.centerCoordinate

let mapLatitude = center.latitude
let mapLongitude = center.longitude


let locationmove = CLLocation(latitude: mapLatitude, longitude: mapLongitude)
CLGeocoder().reverseGeocodeLocation(locationmove) { (placemarks, error) in

if (error != nil){

print(error)

}else {

if let p = placemarks?[0]{

let locality = p.locality ?? ""
let administrativeArea = p.administrativeArea ?? ""


self.mappedCity = String(locality)
self.mappedState = String(administrativeArea)
self.parseJSON("\(locality)", state: "\(administrativeArea)")
}

}

}
}
}

从地理编码结果中解析 JSON 的函数:

func parseJSON(city: String, state: String){
let passedCity = city
let passedState = state
let escapedCity = passedCity.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!
let escapedState = passedState.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!

let url = NSURL(string:"http://www.API.com/api.php?city=\(escapedCity)&stateAbv=\(escapedState)&limit=10”)!

let session = NSURLSession.sharedSession()

let task = session.dataTaskWithURL(url) { (items, response, error) -> Void in

if error != nil {

print(error)


}else {


if let items = items {


do {
let jsonResult = try NSJSONSerialization.JSONObjectWithData(items, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary

if jsonResult.count > 0 {

if let datas = jsonResult["data"] as? NSArray{

for data in datas{

if let title = data["title"] as? String {

if let street = data["street"] as? String {

if let city = data["city"] as? String {

if let stateAbv = data["stateAbv"] as? String {

if let zip = data["zip"] as? String {

self.geoAddress("\(title)", street: "\(street)", city: "\(city)", state: "\(stateAbv)", zip: "\(zip)")


}
}
}

}

}

}
}

}
} catch{}


}


}

}

task.resume()

}


func geoAddress(title: String, street: String, city: String, state: String, zip: String){
let storeName = "\(title)"
let location = "\(street) \(city) \(state) \(zip)"
let geocoder = CLGeocoder();
geocoder.geocodeAddressString(location, completionHandler: {(placemarks: [CLPlacemark]?, error: NSError?) -> Void in
if (error != nil) {
print("Error \(error!)")
} else if let placemark = placemarks?[0] {

let coordinates:CLLocationCoordinate2D = placemark.location!.coordinate

let pointAnnotation:MKPointAnnotation = MKPointAnnotation()
pointAnnotation.coordinate = coordinates
pointAnnotation.title = storeName
pointAnnotation.subtitle = location

self.mapView.addAnnotation(pointAnnotation)

}
})
}

最佳答案

只需将更新后的位置与旧位置进行比较,因此您将获得的距离应该足够高以发出另一个 CLGeocoder 请求。苹果的文档说的是

  • 当您想要自动更新用户的当前位置时(例如当用户移动时),仅发出新的地理编码请求当用户移动了相当大的距离并且经过合理的移动后已经过去一段时间了。例如,在典型情况下,您每分钟不应发送超过一个地理编码请求。

    查看CLGeocoder的官方文档了解更多信息。

关于ios - CLGeocoder 地理编码请求超出限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36847637/

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