我无法从Google Places API获取多个位置。问题是......如果我只获取一个像Bars这样的引脚类型,那么没问题。但是,如果我试图获得餐馆,酒吧,赌场...倍数类型,它给我唯一的第一名,在我们的情况下餐馆。
我尝试使用Postman与下面的链接提出相同的请求...但是例如
我使用此代码来获取我想要的地方:
func fetchPlacesNearCoordinate(_ coordinate: CLLocationCoordinate2D, radius: Double, types:[String], completion: @escaping PlacesCompletion) -> Void {
var urlString = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=\(coordinate.latitude),\(coordinate.longitude)&radius=\(50000)&rankby=prominence&sensor=true&key=\(googleApiKey)"
let typesString = types.count > 0 ? types.joined(separator: "|") : "food"
urlString += "&types=\(typesString)"
urlString = urlString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) ?? urlString
guard let url = URL(string: urlString) else { completion([]); return }
if let task = placesTask, task.taskIdentifier > 0 && task.state == .running {
task.cancel()
}
DispatchQueue.main.async {
UIApplication.shared.isNetworkActivityIndicatorVisible = true
}
placesTask = session.dataTask(with: url) { data, response, error in
if data != nil{
for el in data!{
//print(el)
}
}
var placesArray: [PlaceContent] = []
defer {
DispatchQueue.main.async {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
completion(placesArray)
}
}
guard let data = data else { return }
do{
let decode = try JSONDecoder().decode(GooglePlacesAnswer.self, from: data)
placesArray = (decode.results?.map{ $0.toPlaceContent() }) ?? []
} catch let value{
print(value.localizedDescription)
}
}
placesTask?.resume()
}
您无法获得官方文档中所述的多种类型的地点。您必须提出多个请求并合并结果。
https://developers.google.com/maps/documentation/javascript/places
type - 将结果限制为与指定类型匹配的位置。只能指定一种类型(如果提供了多种类型,则忽略第一个条目后面的所有类型)。见list of supported types。