存储 Google 个地点 ID 并传递给 UIView
Storing Google Places ID and passing to UIView
我在存储来自 Google Places 的地点详细信息时遇到了一些问题。我可以很好地构建应用程序,但是当我 select 来自 GMSAutocompleteViewController
的一个地方时崩溃......
"Could not cast value of type 'GMSPlace' (0x10075bec8) to 'NSString' (0x1afec1398)."
我想保存 selected 地点数据,然后发送该数据以显示在 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()
}
您的代码 (GMSPlace.self
) 正在创建对 GMSPlace
类型本身的引用,而不是特定实例。委托方法将返回视图控制器能够自动完成的 GMSPlace
的实例。使用此实例,您可以从名为 placeID
.
的 属性 获取地点 ID
试试。
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()
}
我在存储来自 Google Places 的地点详细信息时遇到了一些问题。我可以很好地构建应用程序,但是当我 select 来自 GMSAutocompleteViewController
的一个地方时崩溃......
"Could not cast value of type 'GMSPlace' (0x10075bec8) to 'NSString' (0x1afec1398)."
我想保存 selected 地点数据,然后发送该数据以显示在 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()
}
您的代码 (GMSPlace.self
) 正在创建对 GMSPlace
类型本身的引用,而不是特定实例。委托方法将返回视图控制器能够自动完成的 GMSPlace
的实例。使用此实例,您可以从名为 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()
}