在不使用 Google 地图 API 的情况下集成 Google 个地点 API?

Integrating Google Places API without using Google Maps API?

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

您只需要通过提供所需的键值来获取附近的地方

来实现google地方API

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=(lat),(long)&radius=(radius)&type=(type)&hasNextPage=(true)&nextPage()=(true)&keyword=(keyword)&key=(google_services_key)&pagetoken=(pageToken)

实际上,如果您只想使用 return 您周围机构的文本字段作为列表,您可以像这样使用 google 位置自动完成。

按照这些步骤,

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

2) 在你的 main.storyboard 中添加一个视图控制器,然后创建一个 ViewController.swift 文件并将其 link 到情节提要视图控制器

3) 单击视图控制器 -> 转到菜单中的编辑器 -> 嵌入 -> 导航控制器。现在你应该得到一个导航控制器 linked 到你的视图控制器。

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) 在您的应用委托中

import GooglePlaces

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

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

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