【问题标题】:CLGeocoder Geocoding Requests Going Over LimitCLGeocoder 地理编码请求超出限制
【发布时间】:2016-08-19 06:15:57
【问题描述】:

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

查看用户是否更改地图区域的功能:

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)

     }
  })
}

【问题讨论】:

    标签: ios swift clgeocoder


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-21
      • 2012-06-24
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多