ios 如何使用 swift 将 google 位置自动完成添加到 xcode(教程)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28793940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 05:03:35  来源:igfitidea点击:

How to add google places autocomplete to xcode with swift (tutorial)

iosxcodegoogle-mapsautocompletegoogle-places-api

提问by Frederik J?rgensen

I want to add google places autocomplete to xcode with swift, so users can search on a city and press enter, so should the app show that city on the map.

我想用swift将谷歌地点自动完成添加到xcode,这样用户就可以搜索一个城市并按回车键,所以应用程序应该在地图上显示那个城市。

I′m using google maps, so it has to be connected to that map and the search bar would i like to have in the navi-bar (just above the map)

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

Does anyone know a good tutorial to do that??

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

回答by Romain

Not exactly tutorials, but there are a few resources on Github, with people providing libraries and code samples to achieve what you're asking for.

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

Even without proper tutorials, you can still check out these libraries and understand how their code works to get some inspiration if you want to write your own components.

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

As a sidetone, Google also has this page, but it's not a dedicated tutorial for iOS apps, just some useful explanations of how their API works: https://developers.google.com/places/documentation/autocomplete#examples

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

回答by Letaief Achraf

For Swift 3:

对于 Swift 3:

1- Select your podfile and type : pod 'GooglePlaces'

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

2- In the appDelegate, add your API Key : GMSPlacesClient.provideAPIKey("YOUR KEY")(Import the GooglePlaces)

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

3- Use this code in your viewController that contains the googleMap:

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

回答by Mrug

You can try one for wrapper to implement Autocompletion for Place API with Google:

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

https://github.com/mrugrajsinh/MVAutocompletePlaceSearchTextField

https://github.com/mrugrajsinh/MVAutocompletePlaceSearchTextField

Its very simple drop-in control and subclass of UITextFieldso using by binding Class with simple UITextFieldyou can achive Auto completion dropdown similar as Uberand many popular app does.

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

回答by Howard Wilson

Here's a tutorialI came across which is based on my ios_google_places_autocompletecode.

这是我遇到的一个基于我的ios_google_places_autocomplete代码的教程

@Frederik: If you've had a problem with the library, please create an issuedescribing it.

@Frederik:如果您在使用库时遇到问题,请创建一个问题来描述它。

回答by Ronak Kalavadia

Use this latest repo. with swift 2.0

使用这个最新的回购。快速 2.0

Just download zip file of project and open pod file and write pod 'GoogleMaps'and save it open terminal and install pod.

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

Enjoy!!!

享受!!!

RKAutoCompletePlaceSearch

RKAutoCompletePlaceSearch

回答by Sam B

For anyone thinking hey lets implement google maps api in my iOS app. This is so fun and easy ... there is a HUGE drawback.

对于任何认为嘿让我们在我的 iOS 应用程序中实现谷歌地图 API 的人。这非常有趣和容易......有一个巨大的缺点。

The Google Places API for iOS enforces a default limit of 1?000 requests per 24 hour period. If your app exceeds the limit, the app will start failing. Enable billing to get 150?000 requests per 24 hour period.

适用于 iOS 的 Google Places API 强制执行每 24 小时 1?000 个请求的默认限制。如果您的应用程序超出限制,应用程序将开始失败。启用计费以每 24 小时获得 150?000 个请求。

How quickly do you think the limit will be reached in a free or freemium app? Love the part where they say hey your app will start failing.

您认为免费或免费增值应用程序会以多快的速度达到限制?喜欢他们说嘿您的应用程序将开始失败的部分。

Read carefully the usage and terms of service.

仔细阅读使用和服务条款

回答by tryKuldeepTanwar

Simple and lightweight solution for Swift users !

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

I also created a library to make these simple request without including any framework of third party library. You can also maintain cache for Autocomplete with the library.

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

To use to library follow these steps : -

要使用图书馆,请按照下列步骤操作:-

step-1 Import GoogleApiHelperinto your project.

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

step-2 Initialise GoogleApiHelper

step-2 初始化 GoogleApiHelper

GoogleApi.shared.initialiseWithKey("API_KEY")

step-3 Call the methods

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

你可以在这里找到图书馆

enter image description here

在此处输入图片说明

回答by AndrewR

Since the question was asked, Google has added UI elements to the iOS SDK for this sort of workflow. Check out the documentation.

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