PlacesClient给出了一个保持零值的错误,在阅读完Google Places文档后,我阅读了以下声明:
应通过[gmsplacesClient sharedclient]方法访问此类。
我想知道如何实现sharedclient方法,以便基于placesid对给定位置的信息进行搜索?

var placesClient: GMSPlacesClient!

func lookUpPlaceID() {
    let placeID = "ChIJPXw5JDZM6IAR4rTXaFQDGys"

    //What value should placesClient be holding if not GMSPlacesClient in order to run the search based on the placeID?

    self.placesClient.lookUpPlaceID(placeID, callback: { (place, error) -> Void in

    if let error = error {
            print("lookup place id query error: \(error.localizedDescription)")
            return
        }

        if let place = place {
            print("Place name \(place.name)")
            print("Place address \(place.formattedAddress)")
            print("Place placeID \(place.placeID)")
            print("Place attributions \(place.attributions)")
        } else {
            print("No place details for \(placeID)")
        }
    })


}

最佳答案

sharedClient是目标C中的类方法。在Swift中,您将其视为Type Method。因此,不要将placesClient指定给GMSPlacesClient类型,而是将其指定给sharedClient类型方法的结果:

let placesClient = GMSPlacesClient.sharedClient()

placesClient现在将保存GMSPlacesClient的实例(共享实例,如果没有垃圾收集,稍后调用sharedClient将返回相同的实例),而不是像原始代码那样保存该类型。

09-25 17:18