【发布时间】:2015-05-01 20:51:42
【问题描述】:
我想用 swift 将 google 地方自动完成功能添加到 xcode,这样用户就可以搜索一个城市并按 Enter,这样应用程序就应该在地图上显示该城市。
我正在使用谷歌地图,所以它必须连接到该地图,并且我希望在导航栏中(就在地图上方)中有搜索栏
有没有人知道一个很好的教程来做到这一点??
【问题讨论】:
标签: ios xcode google-maps autocomplete google-places-api
我想用 swift 将 google 地方自动完成功能添加到 xcode,这样用户就可以搜索一个城市并按 Enter,这样应用程序就应该在地图上显示该城市。
我正在使用谷歌地图,所以它必须连接到该地图,并且我希望在导航栏中(就在地图上方)中有搜索栏
有没有人知道一个很好的教程来做到这一点??
【问题讨论】:
标签: ios xcode google-maps autocomplete google-places-api
对于 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)
}
}
【讨论】:
acController.delegate = self 给出该错误:无法将类型“MyController”的值分配给类型“GMSAutocompleteViewControllerDelegate?”
extension ViewController: GMSAutocompleteViewControllerDelegate { 重命名为extension MyController: GMSAutocompleteViewControllerDelegate {
不完全是教程,但 Github 上有一些资源,人们提供库和代码示例来实现您的要求。
即使没有适当的教程,如果您想编写自己的组件,您仍然可以查看这些库并了解它们的代码是如何工作的,以获得一些灵感。
作为旁白,Google 也有这个页面,但它不是专门针对 iOS 应用的教程,只是对其 API 工作原理的一些有用说明:https://developers.google.com/places/documentation/autocomplete#examples
【讨论】:
您可以尝试使用一种包装器来使用 Google 为 Place API 实现自动完成功能:
https://github.com/mrugrajsinh/MVAutocompletePlaceSearchTextField
它非常简单的插入式控件和 UITextField 的子类,因此通过将 Class 与简单的 UITextField 绑定使用,您可以实现类似于 的自动完成下拉菜单Uber 和许多流行的应用程序都可以。
【讨论】:
这是我遇到的tutorial,它基于我的ios_google_places_autocomplete 代码。
@Frederik:如果您对库有任何疑问,请create an issue 描述它。
【讨论】:
使用这个最新的 repo。使用 swift 2.0
只需下载项目的 zip 文件并打开 pod 文件并写入 pod 'GoogleMaps' 并保存打开终端并安装 pod。
享受!!!
【讨论】:
自从提出问题以来,Google 已将 UI 元素添加到 iOS SDK 以实现此类工作流程。查看documentation。
【讨论】:
我还创建了一个库来发出这些简单的请求,而不包括任何第三方库的框架。您还可以使用库维护自动完成的缓存。
要使用库,请按照以下步骤操作:-
GoogleApiHelper 导入您的项目。GoogleApiHelper
GoogleApi.shared.initialiseWithKey("API_KEY")
var input = GInput()
input.keyword = "San francisco"
GoogleApi.shared.callApi(input: input) { (response) in
if let results = response.data as? [GApiResponse.Autocomplete], response.isValidFor(.autocomplete) {
//Enjoy the Autocomplete Api
} else { print(response.error ?? "ERROR") }
}
【讨论】: