gpt4 book ai didi

ios - 如何定位 Google 地点自动完成功能?

转载 作者:行者123 更新时间:2023-11-30 12:27:23 25 4
gpt4 key购买 nike

我正在使用这个Tutorial由 Google 使用 Swift 将 Google Places Autocomplete 实现到 iOS 应用程序中,但我试图将结果限制到特定位置,如 link说是可能的,但对于我的生活,我看不到任何地方可以这样做..............

这是 View Controller :

import UIKit
import GooglePlaces

class ViewController: UIViewController {

var resultsViewController: GMSAutocompleteResultsViewController?
var searchController: UISearchController?
var resultView: UITextView?

override func viewDidLoad() {
super.viewDidLoad()

resultsViewController = GMSAutocompleteResultsViewController()
resultsViewController?.delegate = self

searchController = UISearchController(searchResultsController: resultsViewController)
searchController?.searchResultsUpdater = resultsViewController

let subView = UIView(frame: CGRect(x: 0, y: 65.0, width: 350.0, height: 45.0))

subView.addSubview((searchController?.searchBar)!)
view.addSubview(subView)
searchController?.searchBar.sizeToFit()
searchController?.hidesNavigationBarDuringPresentation = false

// When UISearchController presents the results view, present it in
// this view controller, not one further up the chain.
definesPresentationContext = true
}
}

// Handle the user's selection.
extension ViewController: GMSAutocompleteResultsViewControllerDelegate {
func resultsController(_ resultsController: GMSAutocompleteResultsViewController,
didAutocompleteWith place: GMSPlace) {
searchController?.isActive = false
// Do something with the selected place.
print("Place name: \(place.name)")
print("Place address: \(place.formattedAddress)")
print("Place attributions: \(place.attributions)")
}

func resultsController(_ resultsController: GMSAutocompleteResultsViewController,
didFailAutocompleteWithError error: Error){
// TODO: handle the error.
print("Error: ", error.localizedDescription)
}

// Turn the network activity indicator on and off again.
func didRequestAutocompletePredictions(forResultsController resultsController: GMSAutocompleteResultsViewController) {
UIApplication.shared.isNetworkActivityIndicatorVisible = true
}

func didUpdateAutocompletePredictions(forResultsController resultsController: GMSAutocompleteResultsViewController) {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
}
}

最佳答案

在文档中它说明了用于此目的的类。

A GMSCoordinateBounds object biasing the results to a specific area specified by latitude and longitude bounds.

注意:您无法完全删除这些范围之外的所有内容,如果人们搜索埃菲尔铁塔,他们仍然会看到它。但 99% 的结果可能都在这些范围内。这只是一个偏见搜索。

给 GMSColinedBounds 类提供角点经度和纬度,就这样了。

它的用法如下:

let northEastBoundsCorner = CLLocationCoordinate2D(latitude: -33.843366,
longitude: 151.134002)
let southWestBoundsCorner = CLLocationCoordinate2D(latitude: -33.875725,
longitude: 151.200349)
let bounds = GMSCoordinateBounds(coordinate: neBoundsCorner,
coordinate: swBoundsCorner)
func placeAutocomplete() {
let filter = GMSAutocompleteFilter()
filter.type = .establishment
placesClient.autocompleteQuery("Sydney Oper", bounds: bounds, filter: filter, callback: {(results, error) -> Void in
if let error = error {
print("Autocomplete error \(error)")
return
}
if let results = results {
for result in results {
print("Result \(result.attributedFullText) with placeID \(result.placeID)")
}
}
}) }

这是他们使用它的文档中的网址: Set the GMSCoordinateBounds of autocomplete

关于ios - 如何定位 Google 地点自动完成功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44013234/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com