У меня проблемы с получением нескольких мест из Google Places API. Проблема в том, что если я получаю только один тип вывода, например Барs, все в порядке, нет проблем. Но если я пытаюсь получить Рестораны, Бары, Казино... кратные типы, это дает мое единственное первое место, в нашем случае Рестораны.
Я попытался сделать тот же запрос с Почтальон со ссылкой ниже... но, например
Я использую этот код, чтобы получить места, которые я хочу:
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 — Restricts the results to places matching the specified type. Only one type may be specified (if more than one type is provided, all types following the first entry are ignored). See the list of supported types.
Сделайте несколько запросов, чтобы получить все типы, которые вам нужны
Будет ли правильно получить все учреждения и отфильтровать их после или сделать несколько запросов, чтобы получить все типы, которые мне нужны?