如何在使用Swift的Xcode中添加谷歌地点自动完成(教程)

13
我想在使用Swift的Xcode中添加Google地点自动完成功能,以便用户可以搜索一个城市并按Enter键,应用程序将在地图上显示该城市。我正在使用Google地图,因此它必须连接到该地图,我希望将搜索栏放置在导航栏中(就在地图上方)。有人知道好的教程吗?
8个回答

8

对于Swift 3:

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

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

3- 在包含谷歌地图的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?'。 - NEOline
以上错误已解决: 忘记将扩展名从以下内容重命名: extension ViewController: GMSAutocompleteViewControllerDelegate {extension MyController: GMSAutocompleteViewControllerDelegate { - NEOline
简单而甜美的回答。 - Dharmbir Singh
如何从这些委托中获取 LAN 和 LOG? - Qazi Ammar

7

虽然没有详细的教程,但是在Github上有一些资源,人们提供了库和代码示例来实现你所要求的功能。

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

顺便说一句,Google也有这个页面,但它不是专门针对iOS应用程序的教程,只是一些有关其API如何工作的有用说明:https://developers.google.com/places/documentation/autocomplete#examples


我都认识,但第一个是用Objective C编写的,我不知道如何转换它。 - Frederik Jørgensen
第二个充满了失败。 - Frederik Jørgensen
目前我正在使用第二个项目,并且没有遇到任何问题。 - Shaun
有一个网站可以用来将Objective-C转换为Swift。它有一些小问题,但是它可以让你对代码的不同之处有一个相当好的理解。快去看看吧 -> objectivec2swift.net/#/converter - Jobs

5
你可以尝试使用一个包装器来实现与Google Place API的自动完成功能:https://github.com/mrugrajsinh/MVAutocompletePlaceSearchTextField 这是一个非常简单的控件,是UITextField的子类,在使用时只需要将其绑定到普通的UITextField类上即可实现像Uber等许多流行应用程序一样的自动完成下拉菜单。

1
这个库不支持Swift。请参考:https://dev59.com/epHea4cB1Zd3GeqPsa6l - Aliens
安装了这个之后才意识到它不适用于Swift。 - Anconia
我之前使用了你的库,它以前可以工作。但是现在,当我输入位置并点击地点后,除非我一直按住手机,否则它不起作用。@Mrug - Dilip Tiwari

3

2

1
请使用最新的repo,支持swift 2.0。
只需下载项目的zip文件,打开pod文件并写入pod 'GoogleMaps',保存后打开终端并安装pod即可。
享受吧!!!

RKAutoCompletePlaceSearch


0

为Swift用户提供简单轻量级的解决方案!

我还创建了一个库,可以在不包括任何第三方框架或库的情况下进行这些简单的请求。您还可以使用该库维护自动完成的缓存。

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

步骤1:将GoogleApiHelper导入您的项目中。

步骤2:初始化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") }
}

您可以在这里找到该库

enter image description here


0

自从这个问题被提出以来,Google已经为iOS SDK添加了UI元素,用于此类工作流程。请查看文档


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接