如何使用swift添加谷歌地方自动完成到xcode(教程)

Fre*_*sen 13 xcode google-maps autocomplete ios google-places-api

我想用swift将谷歌地方自动完成功能添加到xcode,这样用户就可以搜索城市并按回车键,因此应用程序应该在地图上显示该城市.

我正在使用谷歌地图,所以它必须连接到该地图和我想在导航栏中的搜索栏(在地图上方)

有没有人知道这样做的好教程?

Rom*_*ain 7

不完全是教程,但Github上有一些资源,人们提供库和代码示例来实现您的要求.

即使没有适当的教程,您仍然可以查看这些库并了解他们的代码如何工作以获得一些灵感,如果您想编写自己的组件.

作为支持者,谷歌也有这个页面,但它不是iOS应用程序的专用教程,只是对其API工作方式的一些有用的解释:https://developers.google.com/places/documentation/autocomplete#examples


小智 7

对于Swift 3:

1-选择您的podfile并输入:pod'GooglePlaces'

2-在appDelegate中,添加您的API密钥:( GMSPlacesClient.provideAPIKey("YOUR KEY")导入GooglePlaces)

3-在包含googleMap的viewController中使用此代码:

    // This code snippet demonstrates adding a
// full-screen Autocomplete UI control

import UIKit
import GooglePlaces

class ViewController: UIViewController {

  // TODO: Add a button to Main.storyboard to invoke onLaunchClicked.

  // Present the Autocomplete view controller when the button is pressed.
  @IBAction func onLaunchClicked(sender: UIButton) {
    let acController = GMSAutocompleteViewController()
    acController.delegate = self
    present(acController, animated: true, completion: nil)
  }
}

extension ViewController: GMSAutocompleteViewControllerDelegate {

  // Handle the user's selection.
  func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
    print("Place name: \(place.name)")
    print("Place address: \(place.formattedAddress)")
    print("Place attributions: \(place.attributions)")
    dismiss(animated: true, completion: nil)
  }

  func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
    // TODO: handle the error.
    print("Error: \(error)")
    dismiss(animated: true, completion: nil)
  }

  // User cancelled the operation.
  func wasCancelled(_ viewController: GMSAutocompleteViewController) {
    print("Autocomplete was cancelled.")
    dismiss(animated: true, completion: nil)
  }
}
Run Code Online (Sandbox Code Playgroud)


Mru*_*rug 5

您可以尝试使用一个包装器来实现Place API与Google的自动完成功能:

https://github.com/mrugrajsinh/MVAutocompletePlaceSearchTextField

它非常简单的嵌入式控件和UITextField的子类,因此通过使用简单的UITextField绑定Class,您可以像Uber和许多流行的应用程序一样实现自动完成下拉列表.