placesClient - 正确使用sharedClient方法?

时间:2016-01-09 22:20:21

标签: swift callback google-places-api google-maps-sdk-ios

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)")
        }
    })


}

1 个答案:

答案 0 :(得分:3)

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

let placesClient = GMSPlacesClient.sharedClient()

placesClient现在将拥有GMSPlacesClient实例(一个共享实例,稍后调用sharedClient将返回相同的实例,如果它还没有垃圾收集),而不是像原始代码一样持有类型