swift - GMSPlace 返回无效坐标(-180、-180),但名称和地点 ID 正确
问题描述
我正在尝试在 Google 地图上实现自动完成搜索,该搜索将显示用户在地图上选择的位置并带有标记。
搜索工作正常。问题如下。当我从搜索结果中选择一个位置时,我得到一个 GMSPlace 对象,该对象具有正确的名称作为所选值、正确的地点 ID(使用此链接确认),但坐标不正确(-180.0,-180.0
或kCLLocationCoordinate2DInvalid
常数)。我在多个位置进行了测试。
大部分代码是从Places API的文档中借来的。
import UIKit
import GoogleMaps
import GooglePlaces
class ViewController: UIViewController, UISearchBarDelegate {
@IBOutlet weak var mapContainer: UIView!
var mapView: GMSMapView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.mapView = GMSMapView(frame: self.mapContainer.frame)
self.view.addSubview(self.mapView)
}
// Code from https://developers.google.com/places/ios-sdk/autocomplete#add_an_autocomplete_ui_control
@IBAction func searchByAddress(_ sender: Any) {
// Present the Autocomplete view controller when the button is pressed.
let autocompleteController = GMSAutocompleteViewController()
autocompleteController.delegate = self
// Specify the place data types to return.
let fields: GMSPlaceField = GMSPlaceField(rawValue: UInt(GMSPlaceField.name.rawValue) |
UInt(GMSPlaceField.placeID.rawValue))!
autocompleteController.placeFields = fields
// Display the autocomplete view controller.
present(autocompleteController, animated: true, completion: nil)
}
}
extension ViewController: GMSAutocompleteViewControllerDelegate {
// Handle the user's selection.
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
let position: CLLocationCoordinate2D = place.coordinate
let camera = GMSCameraPosition.camera(withLatitude: position.latitude, longitude: position.longitude, zoom: 10)
let newMapView = GMSMapView.map(withFrame: self.mapContainer.frame, camera: camera)
self.mapView = newMapView
self.view.addSubview(newMapView)
let marker = GMSMarker()
marker.position = position
marker.title = place.name
marker.map = self.mapView
viewController.dismiss(animated: true, completion: nil)
}
func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
// TODO: handle the error.
print("Error: ", error.localizedDescription)
}
// User canceled the operation.
func wasCancelled(_ viewController: GMSAutocompleteViewController) {
viewController.dismiss(animated: true, completion: nil)
}
// Turn the network activity indicator on and off again.
func didRequestAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
UIApplication.shared.isNetworkActivityIndicatorVisible = true
}
func didUpdateAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
}
}
任何帮助,将不胜感激!
解决方案
我遇到了同样的问题,并浏览了 Place SDK 文档,该文档清楚地说明我们应该事先定义 GMSPlaceField 我们究竟需要哪些细节,如果你完全遵循了文档,它只会导致名称和placeId 正在填充。因此,在实例化您的 GMSAutoCompleteViewController 时,请按以下方式定义。
**let fields: GMSPlaceField = GMSPlaceField(rawValue:UInt(GMSPlaceField.name.rawValue) |
UInt(GMSPlaceField.placeID.rawValue) |
UInt(GMSPlaceField.coordinate.rawValue) |
GMSPlaceField.addressComponents.rawValue |
GMSPlaceField.formattedAddress.rawValue)!
autocompleteController.placeFields = fields**
推荐阅读
- javascript - For循环使用两个变量?
- proxy - 代理链无法连接
- android - firebase crashlytics 集成不起作用
- java - java8层次数据json解析并以平面格式存储在mongodb中
- python - python中的内部类和内部内部类有什么区别?
- r - 从 RIDGE、LASSO 和带系数 (R) 的网络弹性回归中提取公式,用于许多变量
- javascript - 仅显示具有特定类型的数组内的对象 [GRAPHQL]
- c# - 无法从 asp.net core mvc 中的数据库更新我的模型
- c++ - 如何在 C++ 中的队列结构中使用 malloc
- kepserverex - 是否可以在 KepServerEx 中创建自定义模拟函数?