ios - 谷歌地理编码不断返回不同地方的市中心位置

标签 ios swift google-maps geocoding

我正在使用谷歌自动完成(GMSAutocompletePrediction)来获取attributedFullText(result.attributedFullText.string),所选择的建议在以下代码中使用,这是从attributedFullText获取纬度和经度的地理编码。对于许多不同的地方,返回的原点是我正在查看的城市的市中心。

知道使用的语言是阿拉伯语

if let correctedAddress = self.allAddresses[0][(indexPath as NSIndexPath).row].full.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
                if let url = URL(string: "https://maps.googleapis.com/maps/api/geocode/json?address=\(correctedAddress)&sensor=false") {
                    print(correctedAddress)
                    print(self.allAddresses[0][(indexPath as NSIndexPath).row].full)
                    let task = URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) -> Void in
                        // 3
                        do {
                            if data != nil{
                                if let jsonObject = try JSONSerialization.jsonObject(with: data!, options: []) as? [String:Any],
                                    let results = jsonObject["results"] as? [[String:Any]], !results.isEmpty,
                                    let geometry = results[0]["geometry"] as? [String:Any],
                                    let location = geometry["location"] as? [String:Double],
                                    let lat = location["lat"], let lng = location["lng"] {
                                     print("\(lat) - \(lng)")
                                    let newAddress = Address(name: self.allAddresses[0][(indexPath as NSIndexPath).row].name , detail: self.allAddresses[0][(indexPath as NSIndexPath).row].detail, latitude: String(lat), longitude: String(lng))
                                    // go back to the previous view controller
                                    self.delegate?.userDidChooseLocation(newAddress)
                                    self.dismiss(animated: true, completion: nil)

                                }

                            }

                        }catch {
                            print("Error")
                        }
                    }) 
                    // 5
                    task.resume()
                }
            }


        }

最佳答案

使用地点 ID 会更好:

https://developers.google.com/places/ios-api/reference/interface_g_m_s_autocomplete_prediction.html#a1c5f68fbd615425c3dcc5cb60b8b16c7

您还可以使用自动完成中的地点 ID 查询 Google Maps Geocoding API Web 服务:

https://maps.googleapis.com/maps/api/geocode/json?place_id=(placeId)&key=YOUR_API_KEY

请注意,此类请求需要 API key 。

此外,传感器参数早已退役。

关于ios - 谷歌地理编码不断返回不同地方的市中心位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40164199/

相关文章:

ios - 在 ViewController 中找到 indexPath

ios - iOS 中的应用内购买有问题吗?

javascript - 呈现angular.js ui-map指令后如何触发 map 调整大小事件?

google-maps - Flutter:如何使用新的 Marker API 向 Google Maps 添加标记?

python - 在 Google Colab 平台上的 Jupyter Notebook 中显示/渲染 HTML 文件

ios - 在回调函数上设置 uilabel

iphone - 在我的 iPhone 应用程序中调用 rss

javascript - 是否可以在 Mobile Safari 中允许跨站点脚本 (XSS)?

ios - UITableView 快速向上滚动时不渲染单元格

swift - 我制作了一个 Swift 扩展以像在 iOS 上一样使用框架,但在使用时无法编译