如何使用 Google Places API 文本搜索结合商家名称和地址来查找兴趣点?

问题描述 投票:0回答:1

我正在构建一个使用 Apple 的 MapKit 框架以及 Google 的 Places API 的应用程序。原因主要是节省成本。

我目前的情况是,我有一个MKMapItem,想找到对应的GMSPlace。我想使用 GMSPlace 获取有关兴趣点的更多详细信息,例如开放时间。我的想法是使用 Google 的 TextSearch,并基于以下支持网站构建我的解决方案。

不幸的是,我无法通过这种方式找到相应的地点,因为我的查询在获取地点名称及其地址时返回空集。

我的 API 代码如下所示,并且适用于通用查询。向它提供诸如“披萨”之类的问题,会在正确的区域返回可行的结果。但是,当我提供以下查询和位置偏差时,我得到一个空结果集。当我实际提供确切的地址详细信息时,也会发生同样的情况。

    “P&M 卢塞恩摄影媒体”
  • CL位置坐标2D(纬度:47.050291,经度:8.306158)
  • class PlacesViewModel: ObservableObject { @Published var placeResults: [GMSPlace] = [] @Published var placeResult: GMSPlace? = nil @Published var errorMessage: String? = nil func searchPlaces(query: String, location: CLLocationCoordinate2D) { // Define the properties we want to retrieve let myProperties = [GMSPlaceProperty.name, GMSPlaceProperty.placeID, GMSPlaceProperty.openingHours, GMSPlaceProperty.website].map { $0.rawValue } // Create the GMSPlaceSearchByTextRequest object let request = GMSPlaceSearchByTextRequest(textQuery: query, placeProperties: myProperties) request.maxResultCount = 5 request.rankPreference = .distance request.locationBias = GMSPlaceCircularLocationOption(location, 500.0) // Callback to handle the response let callback: GMSPlaceSearchByTextResultCallback = { [weak self] results, error in guard let self = self else { return } if let error = error { DispatchQueue.main.async { self.errorMessage = error.localizedDescription } return } guard let results = results else { return } DispatchQueue.main.async { self.placeResults = results print(self.placeResults) //self.placeResult = results[0] } } // Perform the search GMSPlacesClient.shared().searchByText(with: request, callback: callback) } }
有人知道如何构造查询吗?或者我可以使用哪个函数/API?

ios swift google-places-api
1个回答
0
投票

class PlacesViewModel: ObservableObject { @Published var errorMessage: String? = nil var placeSuggestions: [GMSAutocompleteSuggestion] = [] func fetchSuggestions(query: String, location: CLLocationCoordinate2D) { let token = GMSAutocompleteSessionToken() let filter = GMSAutocompleteFilter() filter.locationBias = GMSPlaceCircularLocationOption(location, 200.0) let request = GMSAutocompleteRequest(query: query) request.filter = filter request.sessionToken = token let callback: GMSAutocompleteSuggestionsCallback = { [weak self] results, error in guard let self = self else { return } if let error = error { DispatchQueue.main.async { self.errorMessage = error.localizedDescription } return } if let results = results { DispatchQueue.main.async { self.placeSuggestions = results print(results) } } else { return } } GMSPlacesClient.shared().fetchAutocompleteSuggestions(from: request, callback: callback) } }

	
© www.soinside.com 2019 - 2024. All rights reserved.