我正在尝试构建我的第一个应用。我有UITextField
我想要访问Google Places API来搜索附近的餐馆。我不需要在地图上查看这些位置,我只需要返回用户可以点击的搜索结果,以查看有关该位置的更多详细信息。在我安装pod GooglePlaces
之后集成Google Places API的步骤是什么?
答案 0 :(得分:1)
实际上,如果您只想使用一个文本字段来返回您周围的机构作为列表,您可以使用谷歌地方自动完成这样做。
按照这些步骤,
1)安装谷歌的地方 https://developers.google.com/places/ios-api/start2)在你的main.storyboard中添加一个视图控制器然后创建一个ViewController.swift文件并将其链接到storyboard视图控制器
3)点击视图控制器 - >转到菜单中的编辑器 - >嵌入 - >导航控制器。现在你应该得到一个链接到视图控制器的导航控制器。
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)在你的app delegate
中import GooglePlaces
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
GMSPlacesClient.provideAPIKey(GoogleKey) //add your google key here
return true
}
希望这对你有所帮助。您可以自定义颜色和所有这些。除了使用地点ID选择地点后的主要细节,您还可以获得所有细节。
答案 1 :(得分:0)