如何使用 swift 将 google 地方自动完成功能添加到 xcode(教程)

Posted

技术标签:

【中文标题】如何使用 swift 将 google 地方自动完成功能添加到 xcode(教程)【英文标题】:How to add google places autocomplete to xcode with swift (tutorial) 【发布时间】:2015-05-01 20:51:42 【问题描述】:

我想用 swift 将 google 地方自动完成功能添加到 xcode,这样用户就可以搜索一个城市并按 Enter,这样应用程序就应该在地图上显示该城市。

我正在使用谷歌地图,所以它必须连接到该地图,并且我希望在导航栏中(就在地图上方)中有搜索栏

有没有人知道一个很好的教程来做到这一点??

【问题讨论】:

【参考方案1】:

对于 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)
  

【讨论】:

line acController.delegate = self 给出该错误:无法将类型“MyController”的值分配给类型“GMSAutocompleteViewControllerDelegate?” 上述错误已解决:忘记将​​扩展名从:extension ViewController: GMSAutocompleteViewControllerDelegate 重命名为extension MyController: GMSAutocompleteViewControllerDelegate 简单而甜蜜的答案。 如何从这些代表那里获取局域网和日志??【参考方案2】:

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

SPGooglePlacesAutocomplete (https://github.com/spoletto/SPGooglePlacesAutocomplete) 非常流行并且有很多功能,但是它是用 Objective-C 编写的 ios Google Places Autocomplete (https://github.com/watsonbox/ios_google_places_autocomplete) 较新,使用 Swift 编写,还包含一些示例。 很多其他...

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

作为旁白,Google 也有这个页面,但它不是专门针对 iOS 应用的教程,只是对其 API 工作原理的一些有用说明:https://developers.google.com/places/documentation/autocomplete#examples

【讨论】:

两个我都认识,但第一个在目标 C 中,我不知道必须转换它。 第二个满是失败 我在一个项目中使用第二个,目前没有问题。 有一个网站可以用来将objective c 转换为swift。它有一些错误,但它可以让您很好地理解代码的不同之处。看看 ->objectivec2swift.net/#/converter【参考方案3】:

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

https://github.com/mrugrajsinh/MVAutocompletePlaceSearchTextField

它非常简单的插入式控件和 UITextField 的子类,因此通过将 Class 与简单的 UITextField 绑定使用,您可以实现类似于 的自动完成下拉菜单Uber 和许多流行的应用程序都可以。

【讨论】:

这个库不适用于 Swift。见:***.com/questions/34060166/… 在意识到它不适用于 swift 之前安装了它 我之前使用的是你的 lib,它现在可以工作,当我在输入位置后单击位置时它不起作用,直到我持有单元格 @Mrug【参考方案4】:

这是我遇到的tutorial,它基于我的ios_google_places_autocomplete 代码。

@Frederik:如果您对库有任何疑问,请create an issue 描述它。

【讨论】:

【参考方案5】:

请检查这一项 110% 的工作。 http://sweettutos.com/2015/09/30/how-to-use-the-google-places-autocomplete-api-with-google-maps-sdk-on-ios/

【讨论】:

【参考方案6】:

使用这个最新的 repo。使用 swift 2.0

只需下载项目的 zip 文件并打开 pod 文件并写入 pod 'GoogleMaps' 并保存打开终端并安装 pod。

享受!!!

RKAutoCompletePlaceSearch

【讨论】:

【参考方案7】:

自从提出问题以来,Google 已将 UI 元素添加到 iOS SDK 以实现此类工作流程。查看documentation。

【讨论】:

【参考方案8】:

适用于 Swift 用户的简单轻量级解决方案!

我还创建了一个库来发出这些简单的请求,而不包括任何第三方库的框架。您还可以使用库维护自动完成的缓存。

要使用库,请按照以下步骤操作:-

step-1 将GoogleApiHelper 导入您的项目。

step-2 初始化GoogleApiHelper

GoogleApi.shared.initialiseWithKey("API_KEY")

step-3 调用方法

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") 

You can find the library here

【讨论】:

以上是关于如何使用 swift 将 google 地方自动完成功能添加到 xcode(教程)的主要内容,如果未能解决你的问题,请参考以下文章

如何使用iOS和Swift从Google SDK获取附近的地点?

制作从 google 下载的圆形照片(google places,swift)

在 Meteor 中使用 Google 地方信息自动填充功能

Swift 桥接文件路径到底如何写 ?

将时间变量添加到过滤器(Swift)

Google地方样式自动完成不起作用