【问题标题】:Force locale for reverseGeocodeLocation in iOS在 iOS 中强制使用 reverseGeocodeLocation 的语言环境
【发布时间】:2015-08-07 11:18:46
【问题描述】:

我正在寻找类似于这两个问题的解决方案

但我希望能够获得地址和位置的非本地化版本。 在我的补充中,在对非拉丁本地化设备地理编码服务进行反向地理编码时,会返回 CLPlacemark,它已经包含地址的本地化版本。例如在日内瓦,当我用俄语使用 iPhone 时,我得到了

“Площадь Пленпале”而不是“Place de Plainpalais”

这可能很酷,但仍然令人困惑,因为街道名称不是西里尔字母。此外,我无法将地址的本地化版本提交给另一个 API 以查找行程。那么有没有办法强制 reverseGeocodeLocation 中的语言环境或短暂欺骗操作系统认为语言环境设置为其他语言?

此外,在请求似乎不起作用之前执行类似的操作,因为它需要重新启动应用程序

NSUserDefaults.standardUserDefaults().setObject("fr", forKey: "AppleLanguages"   
NSUserDefaults.standardUserDefaults().synchronize()

【问题讨论】:

    标签: ios nsuserdefaults reverse-geocoding nslocale clplacemark


    【解决方案1】:

    事实证明,修改 UserDefaults 是不可能的(至少我没有找到可行的解决方案),所以我不得不使用外部反向地理编码器来实现我想要的(强制区域设置)。这是一个使用 Google API 和 Alamofire 和 SwiftyJSON 的,用一个不错的 completionHandler 包裹起来,以便在地理编码完成时获取地址

    func getReversePositionFromGoogle(latitude:Double, longitude:Double, completionHandler: (JSON?, NSError?) -> ()) {
        var parameters = ["latlng":"\(latitude), \(longitude)", "sensor": true, "language": "fr"]
        Alamofire.request(.GET, "http://maps.google.com/maps/api/geocode/json", parameters: parameters as? [String : AnyObject])
            .responseJSON(options: NSJSONReadingOptions.AllowFragments) { (request, response, responseObject, error) -> Void in
                let json = JSON(responseObject!)
                completionHandler(json, error)
        }
    }
    

    【讨论】:

      【解决方案2】:

      我知道这是旧线程,可能有人像我一样正在寻找更新的解决方案。

      使用 Swift 4、iOS 11

      测试

      您可以通过设置额外参数:preferredLocale: Locale.init(identifier: "en_US") 来强制获取所选语言环境中的地理编码结果。

      CLGeocoder().reverseGeocodeLocation(location, preferredLocale: Locale.init(identifier: "en_US"), completionHandler: {(placemarks, error) -> Void in
          print(location)
      
          if error != nil {
              print("Reverse geocoder failed with error" + error!.localizedDescription)
              return
          }
      
          if placemarks!.count > 0 {
              let pm = placemarks![0]
              print(pm.administrativeArea!, pm.locality!)
          }
          else {
              print("Problem with the data received from geocoder")
          }
      })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-31
        • 2021-03-03
        • 2020-05-22
        • 1970-01-01
        • 2013-12-21
        • 2020-01-24
        相关资源
        最近更新 更多