我使用Google Places API,我为这个地方拍照,但我遇到了这样一个问题:对于美国加利福尼亚州圣莫尼卡圣莫尼卡码头非常受欢迎的位置,没有照片。我开始研究这个问题,并通过Google Places Finder为此网站返回了什么ID,我又返回了另一个ID,我尝试了我返回网页版本的ID,用于上传照片并成功下载了照片。
我在iOS版本中获得了EjJTYW50YSBNb25pY2EgUGllciwgU2FudGEgTW9uaWNhLCBDQSwgVW5pdGVkIFN0YXRlcw
地方ID,ChIJm6deTdekwoARTY_RzhoRms0
地点ID为Google Places Finder。
import GooglePlaces
class GooglePlacesManager {
private let gmsPlacesClient = GMSPlacesClient()
func loadPhotosForPlace(_ placeID: String, success: ((_ photos: [UIImage]) -> Void)?, fail: ((_ error: Error) -> Void)?) {
gmsPlacesClient.lookUpPhotos(forPlaceID: placeID) { [weak self] (photos, error) -> Void in
guard error == nil else {
debugPrint("loadPhotosForPlace", error!.localizedDescription)
fail?(error!)
return
}
guard let results = photos?.results else {
success?([])
return
}
var resultCount = results.count
debugPrint("first result count", resultCount)
if resultCount == 0 {
success?([])
return
}
var photos = [UIImage]()
guard self != nil else { return }
for result in results {
self!.loadImageForMetadata(photoMetadata: result, success: { (photo) in
photos.append(photo)
if resultCount == photos.count {
success?(photos)
}
}, notExist: {
resultCount -= 1
if resultCount == photos.count {
success?(photos)
}
}, fail: { (error) in
resultCount -= 1
if resultCount == photos.count {
success?(photos)
}
})
}
}
}
private func loadImageForMetadata(photoMetadata: GMSPlacePhotoMetadata, success: ((_ photo: UIImage) -> Void)?, notExist: (() -> Void)?, fail: ((_ error: Error) -> Void)?) {
gmsPlacesClient.loadPlacePhoto(photoMetadata, callback: {
(photo, error) -> Void in
guard error == nil else {
debugPrint("loadPhotosForPlace", error!.localizedDescription)
fail?(error!)
return
}
guard photo != nil else {
notExist?()
return
}
success?(photo!)
})
}
func getLocationInfo(_ placeID: String, success: ((_ place: GMSPlace) -> Void)?, fail: ((_ error: Error) -> Void)?) {
gmsPlacesClient.lookUpPlaceID(placeID) { (place, error) in
guard error == nil else {
debugPrint(error!.localizedDescription)
fail?(error!)
return
}
guard place != nil else { return }
success?(place!)
}
}
}
请告诉我如何修复它?
更新:
我在这个loadPhotosForPlace
函数
gmsPlacesClient.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)")
})
我收到了这些日志,但是我没有收到任何照片。
Place name Santa Monica Pier
Place address Optional("Santa Monica Pier, Santa Monica, CA 90401, USA")
Place placeID EjJTYW50YSBNb25pY2EgUGllciwgU2FudGEgTW9uaWNhLCBDQSwgVW5pdGVkIFN0YXRlcw
Place attributions nil