GMSPlace返回无效坐标(-180,-180),但名称和位置ID正确

Sab*_*que 6 google-maps-sdk-ios swift

我正在尝试在Google Maps上实现自动完成搜索,该搜索将使用标记显示用户在地图上选择的位置。

搜索效果很好。问题如下。当我从搜索结果中选择位置时,我得到一个GMSPlace对象,该对象具有正确的名称作为所选值,正确的位置ID(使用此链接确认),但坐标不正确(-180.0,-180.0kCLLocationCoordinate2DInvalid常数)。我在多个位置进行了测试。

这些代码大部分是从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
    }

}
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激!

小智 9

我遇到了同样的问题,并浏览了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**
Run Code Online (Sandbox Code Playgroud)