gpt4 book ai didi

ios - 在不使用 Google Maps API 的情况下集成 Google Places API?

转载 作者:行者123 更新时间:2023-11-28 15:20:18 25 4
gpt4 key购买 nike

我正在尝试构建我的第一个应用程序。我有一个 UITextField,我想访问 Google Places API 以搜索附近的餐馆。我不需要在 map 上查看这些位置,我只需要返回搜索结果,用户可以点击该结果以查看有关该位置的更多详细信息。安装 pod GooglePlaces 后集成 Google Places API 的步骤是什么?

最佳答案

实际上,如果您只想使用一个文本字段将您周围的场所作为列表返回,您可以使用 google places auto complete 这样做。

按照这些步骤,

1) 安装 google places https://developers.google.com/places/ios-api/start

2) 在你的 main.storyboard 中添加一个 View Controller ,然后创建一个 ViewController.swift 文件并将其链接到 Storyboard View Controller

3) 单击 View Controller -> 转到菜单中的编辑器 -> 嵌入 -> 导航 Controller 。现在你应该得到一个链接到你的 View Controller 的导航 Controller 。

4) 将下面的代码复制粘贴到 ViewController.swift 中

import UIKit
import GooglePlaces

class ViewController: UIViewController,UISearchBarDelegate{

var resultsViewController: GMSAutocompleteResultsViewController?
var searchController: UISearchController?


override func viewDidLoad() {
super.viewDidLoad()

resultsViewController = GMSAutocompleteResultsViewController()
resultsViewController?.delegate = self

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

// Put the search bar in the navigation bar.
searchController!.searchBar.sizeToFit()
self.navigationItem.titleView = searchController?.searchBar

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

// Prevent the navigation bar from being hidden when searching.
searchController!.hidesNavigationBarDuringPresentation = false
searchController!.searchBar.keyboardAppearance = UIKeyboardAppearance.Dark
searchController!.searchBar.barStyle = .Black

let filter = GMSAutocompleteFilter()
filter.country = "LK" //put your country here
filter.type = .Establishment
resultsViewController?.autocompleteFilter = filter


searchController?.searchBar.showsCancelButton = true
searchController?.searchBar.delegate = self

}

override func viewDidAppear(animated: Bool) {

super.viewDidAppear(animated)
//self.searchController?.active = true
self.searchController!.searchBar.becomeFirstResponder()

}


func didPresentSearchController(searchController: UISearchController) {
self.searchController!.searchBar.becomeFirstResponder()
}


}
// Handle the user's selection.
extension ViewController: GMSAutocompleteResultsViewControllerDelegate {
func resultsController(resultsController: GMSAutocompleteResultsViewController,didAutocompleteWithPlace place: GMSPlace) {

// Do something with the selected place.
print("Pickup Place name: ", place.name)
print("Pickup Place address: ", place.formattedAddress)
print("Pickup Place attributions: ", place.placeID)
}


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

// Turn the network activity indicator on and off again.
func didRequestAutocompletePredictionsForResultsController(resultsController: GMSAutocompleteResultsViewController) {
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
}

func didUpdateAutocompletePredictionsForResultsController(resultsController: GMSAutocompleteResultsViewController) {
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
}


}

5) 在你的应用委托(delegate)中

import GooglePlaces

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
GMSPlacesClient.provideAPIKey(GoogleKey) //add your google key here
return true
}

希望对您有所帮助。您可以自定义颜色和所有这些。并且您可以通过使用它的地点 ID 来获取在此处选择地点后获得的主要详细信息之外的所有详细信息。

你会得到这样的东西,它是完全可定制的, enter image description here

关于ios - 在不使用 Google Maps API 的情况下集成 Google Places API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46127871/

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