1

我在从Google Places存储地点详细信息时遇到了一些问题。我可以很好地构建应用程序,但是当我从...中选择一个位置时会崩溃GMSAutocompleteViewController......“无法将'GMSPlace'(0x10075bec8)类型的值转换为'NSString'(0x1afec1398)。”

我想保存选定的地点数据,然后将该数据发送到 UIView 和 UITableView 中显示。不知道我做错了什么......

    // MARK: GOOGLE AUTO COMPLETE DELEGATE

    func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {

        // Do something with the selected place.
        // A hotel in Saigon with an attribution.
//        let placeID = "ChIJV4k8_9UodTERU5KXbkYpSYs"
        let placeID = GMSPlace.self

        placesClient.lookUpPlaceID((placeID as AnyObject) as! String, callback: { (place, error) -> Void in
            if let error = error {
                print("lookup place id query error: \(error.localizedDescription)")
                return
            }

            guard let place = place else {
                print("No place details for \(placeID)")
                return
            }

            print("Place name \(place.name)")
            print("Place address \(place.formattedAddress)")
            print("Place placeID \(place.placeID)")
            print("Place attributions \(place.attributions)")
        })

        self.dismiss(animated: true, completion: nil)

        setupConfirmationPopUp()

    }
4

1 回答 1

2

您的代码 ( GMSPlace.self) 正在创建对GMSPlace类型本身的引用,而不是特定实例。委托方法将返回GMSPlace视图控制器能够自动完成的实例。使用此实例,您可以从名为 的属性中获取地点 ID placeID

尝试。

func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {

    // Do something with the selected place.
    // A hotel in Saigon with an attribution.
    // let placeID = "ChIJV4k8_9UodTERU5KXbkYpSYs"
    let placeID = place.placeID

    placesClient.lookUpPlaceID(placeID, callback: { (place, error) -> Void in
        if let error = error {
            print("lookup place id query error: \(error.localizedDescription)")
            return
        }

        guard let place = place else {
            print("No place details for \(placeID)")
            return
        }

        print("Place name \(place.name)")
        print("Place address \(place.formattedAddress)")
        print("Place placeID \(place.placeID)")
        print("Place attributions \(place.attributions)")
    })

    self.dismiss(animated: true, completion: nil)

    setupConfirmationPopUp()

}
于 2017-03-19T17:42:15.590 回答