【问题标题】:How to geocode address by google maps iOS API?如何通过谷歌地图 iOS API 对地址进行地理编码?
【发布时间】:2017-04-19 16:35:36
【问题描述】:

我找到了一种发送请求的方法:

Google Maps Geocoding API 请求采用以下形式:

https://maps.googleapis.com/maps/api/geocode/outputFormat?parameters 其中 outputFormat 可以是以下任一值:

json(推荐)以 JavaScript Object Notation 表示输出 (JSON);或 xml 表示以 XML 格式输出 访问 Google 地图 基于 HTTP 的地理编码 API,使用:

但是真的很不方便,swift中有没有原生的方式?

我查看了 GMSGeocoder 接口,它的 API 只能完成反向地理编码。

【问题讨论】:

    标签: ios swift google-maps geocoding google-geocoding-api


    【解决方案1】:

    正如其他人指出的那样,没有预定义的方法来进行搜索,但您可以使用网络请求自己访问Google Geocoding API:

    func performGoogleSearch(for string: String) {
        strings = nil
        tableView.reloadData()
    
        var components = URLComponents(string: "https://maps.googleapis.com/maps/api/geocode/json")!
        let key = URLQueryItem(name: "key", value: "...") // use your key
        let address = URLQueryItem(name: "address", value: string)
        components.queryItems = [key, address]
    
        let task = URLSession.shared.dataTask(with: components.url!) { data, response, error in
            guard let data = data, let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200, error == nil else {
                print(String(describing: response))
                print(String(describing: error))
                return
            }
    
            guard let json = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any] else {
                print("not JSON format expected")
                print(String(data: data, encoding: .utf8) ?? "Not string?!?")
                return
            }
    
            guard let results = json["results"] as? [[String: Any]],
                let status = json["status"] as? String,
                status == "OK" else {
                    print("no results")
                    print(String(describing: json))
                    return
            }
    
            DispatchQueue.main.async {
                // now do something with the results, e.g. grab `formatted_address`:
                let strings = results.compactMap { $0["formatted_address"] as? String }
                ...
            }
        }
    
        task.resume()
    }
    

    【讨论】:

    • 奇怪的是我收到错误 此 IP、站点或移动应用程序无权使用此 API 密钥。从 IP 地址 xxx.xx.xx.xx 收到的请求,引用者为空,即使我已启用 API 并使用我的应用程序包标识符对其进行限制。
    • 更新:找到了修复。显然,我必须在此请求中将包标识符作为标头值 X-Ios-Bundle-Identifier 传递。就在那时它起作用了。
    【解决方案2】:

    不,Google Maps SDK for iOS 中没有原生方式。

    这是一个非常受欢迎的功能请求,请参阅: Issue 5170: Feature request: Forward geocoding (from address to coordinates)

    【讨论】:

      【解决方案3】:

      不幸的是,没有办法以原生方式做到这一点。我希望该功能会有所帮助。

          func getAddress(address:String){
      
          let key : String = "YOUR_GOOGLE_API_KEY"
          let postParameters:[String: Any] = [ "address": address,"key":key]
          let url : String = "https://maps.googleapis.com/maps/api/geocode/json"
      
          Alamofire.request(url, method: .get, parameters: postParameters, encoding: URLEncoding.default, headers: nil).responseJSON {  response in
      
              if let receivedResults = response.result.value
              {
                  let resultParams = JSON(receivedResults)
                  print(resultParams) // RESULT JSON
                  print(resultParams["status"]) // OK, ERROR
                  print(resultParams["results"][0]["geometry"]["location"]["lat"].doubleValue) // approximately latitude
                  print(resultParams["results"][0]["geometry"]["location"]["lng"].doubleValue) // approximately longitude
              }
          }
      }
      

      【讨论】:

        【解决方案4】:

        如果您只是在寻找地理编码解决方案,您可以查看我构建的一个小型开源项目。它非常轻量级,并使用 OpenStreetMap 的名为 Nominatim 的地理编码 API。在这里查看:https://github.com/caloon/NominatimSwift

        您甚至可以搜索地标。

        地理编码地址和地标:

        Nominatim.getLocation(fromAddress: "The Royal Palace of Stockholm", completion: {(error, location) -> Void in
          print("Geolocation of the Royal Palace of Stockholm:")
          print("lat = " + (location?.latitude)! + "   lon = " + (location?.longitude)!)
        })
        

        【讨论】:

          【解决方案5】:

          Alamofire 和Google's Geodecode API

          斯威夫特 4

          func getAddressFromLatLong(latitude: Double, longitude : Double) {
              let url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=\(latitude),\(longitude)&key=YOUR_API_KEY_HERE"
          
              Alamofire.request(url).validate().responseJSON { response in
                  switch response.result {
                  case .success:
          
                      let responseJson = response.result.value! as! NSDictionary
          
                      if let results = responseJson.object(forKey: "results")! as? [NSDictionary] {
                          if results.count > 0 {
                              if let addressComponents = results[0]["address_components"]! as? [NSDictionary] {
                                  self.address = results[0]["formatted_address"] as? String
                                  for component in addressComponents {
                                      if let temp = component.object(forKey: "types") as? [String] {
                                          if (temp[0] == "postal_code") {
                                              self.pincode = component["long_name"] as? String
                                          }
                                          if (temp[0] == "locality") {
                                              self.city = component["long_name"] as? String
                                          }
                                          if (temp[0] == "administrative_area_level_1") {
                                              self.state = component["long_name"] as? String
                                          }
                                          if (temp[0] == "country") {
                                              self.country = component["long_name"] as? String
                                          }
                                      }
                                  }
                              }
                          }
                      }
                  case .failure(let error):
                      print(error)
                  }
              }
          }
          

          【讨论】:

            【解决方案6】:

            您可以使用 Google Places API 的Place Search 通过地址的 URL 会话发送请求,然后解析 json 结果。它可能并不完美,但您可以获得除坐标以外的更多信息。

            【讨论】:

              【解决方案7】:

              Google Maps API iOS SDK 中没有原生方式。正如其他答案中提到的那样,它是requested feature for years。

              要记住的一点是,Google Maps API 主要专注于创建地图:这是一个主要目标。

              您必须使用基于 URL 的 API 调用或其他一些服务。例如,名为 SmartyStreets 的不同服务有一个 iOS SDK,它具有对正向地理编码的本机支持。这是来自their iOS SDK documentation page 的 Swift 示例代码:

              // Swift: Sending a Single Lookup to the US ZIP Code API
              
              package examples;
              
              import Foundation
              import SmartystreetsSDK
              
              class ZipCodeSingleLookupExample {
              
                  func run() -> String {
                      let mobile = SSSharedCredentials(id: "SMARTY WEBSITE KEY HERE", hostname: "HOST HERE")
                      let client = SSZipCodeClientBuilder(signer: mobile).build()
              //        Uncomment the following line to use Static Credentials
              //        let client = SSZipCodeClientBuilder(authId: "YOUR AUTH-ID HERE", authToken: "YOUR AUTH-TOKEN HERE").build()
              
                      let lookup = SSZipCodeLookup()
                      lookup.city = "Mountain View"
                      lookup.state = "California"
              
                      do {
                          try client?.send(lookup)
                      } catch let error as NSError {
                          print(String(format: "Domain: %@", error.domain))
                          print(String(format: "Error Code: %i", error.code))
                          print(String(format: "Description: %@", error.localizedDescription))
                          return "Error sending request"
                      }
              
                      let result: SSResult = lookup.result
                      let zipCodes = result.zipCodes
                      let cities = result.cities
              
                      var output: String = String()
              
                      if (cities == nil && zipCodes == nil) {
                          output += "Error getting cities and zip codes."
                          return output
                      }
              
                      for city in cities! {
                          output += "\nCity: " + (city as! SSCity).city
                          output += "\nState: " + (city as! SSCity).state
                          output += "\nMailable City: " + ((city as! SSCity).mailableCity ? "YES" : "NO") + "\n"
                      }
              
                      for zip in zipCodes! {
                          output += "\nZIP Code: " + (zip as! SSZipCode).zipCode
                          output += "\nLatitude: " + String(format:"%f", (zip as! SSZipCode).latitude)
                          output += "\nLongitude: " + String(format:"%f", (zip as! SSZipCode).longitude) + "\n"
                      }
              
                      return output
                  }
              }
              

              完全披露:我曾在 SmartyStreets 工作过。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2011-08-28
                • 1970-01-01
                • 2016-03-13
                • 2019-06-10
                相关资源
                最近更新 更多