【问题标题】:Google Places Autocomplete Swift 3谷歌地方自动完成 Swift 3
【发布时间】:2017-07-09 12:49:33
【问题描述】:

我想在我的项目中集成 Google Places Autocomplete API,但我无法通过文档实现它,也无法获得 Swift-3 的可靠参考。有人可以帮忙吗?谢谢。

【问题讨论】:

  • 您是说您按照此处的说明进行操作?它们是用 Swift 3 编写的:developers.google.com/places/ios-api/autocomplete
  • 是的,我按照说明进行操作,但这对我没有帮助。所以正在寻找一些例子。并且可用的库在 Swift 2 中。
  • 嘿,你是如何实现它的?可以发链接或者demo吗?我也需要实现它,不知道该怎么做谢谢

标签: ios swift3 google-places-api google-maps-sdk-ios


【解决方案1】:

详情

Swift 4,xCode 9

解决方案

通常的 http 请求。更多详情:https://developers.google.com/places/ios-api/autocomplete

完整样本

Pod 文件

target 'stackoverflow-44996568' do
    use_frameworks!
    pod 'Alamofire'
    pod 'ObjectMapper'      
end

GoogleAutocompleteJSONModel

import ObjectMapper

class GoogleAutocompleteJSONModel: Mappable, CustomStringConvertible {

    public fileprivate(set) var placeId: String?
    public fileprivate(set) var reference: String?
    public fileprivate(set) var title: String?

    required convenience init?(map: Map) {
        self.init()
    }

    func mapping(map: Map) {

        title                       <- map["description"]
        placeId                     <- map["place_id"]
        reference                   <- map["reference"]
    }

    var description: String {
        return "\(toJSON())"
    }
}

网络

import Alamofire
import ObjectMapper

class Network {

    class GoogleAPI {
        class Map {

            class var googleApiKey: String {
                return "YOUR_KEY"
            }

            class func autocomplete(request: String) {
                let url = "https://maps.googleapis.com/maps/api/place/autocomplete/json?input=\(request)&components=country:us&key=\(googleApiKey)"
                Alamofire.request(url)
                    .responseJSON { response in
                        if let json = response.result.value as? [String: Any] {
                            //print("JSON: \(json)")
                            let places = Array<GoogleAutocompleteJSONModel>(json: json["predictions"])
                            let autocomplete = places.flatMap{ $0.title}
                            print("!!!! \(autocomplete)")
                        }
                }
            }
        }
    }
}

扩展

import Foundation

extension Array where Element: Mappable {

    init(json: Any?) {
        self.init()

        var result = [Element]()
        if let array = json as? [[String: Any]] {
            for item in array {
                if let profile = Element(JSON: item) {
                    result.append(profile)
                }
            }
        }
        self = result
    }
}

视图控制器

import UIKit

class ViewController: UIViewController {

    weak var searchBar: UISearchBar!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let searchBar = UISearchBar(frame: CGRect(origin: CGPoint(x: 0, y: 20), size: CGSize(width: UIScreen.main.bounds.width, height: 40)))
        searchBar.delegate = self
        view.addSubview(searchBar)
        self.searchBar = searchBar
    }
}

extension ViewController: UISearchBarDelegate {
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        Network.GoogleAPI.Map.autocomplete(request: searchText)
    }
}

结果

【讨论】:

  • 在扩展中给我两个错误 - 使用未声明的类型 'Mappable' - 非标称类型 'Element' 不支持显式初始化请帮助我
  • 你做了“pod install”吗?
  • 您好,您能看看这个吗? stackoverflow.com/q/68394011/8894627
【解决方案2】:

您需要先通过 pod 在您的项目中安装 google place SDK。 pod 'GooglePlacePicker' 您可以使用此代码通过 pod 安装 googlePlaces。

【讨论】:

  • 感谢您的回复。它已经集成了它。
【解决方案3】:

我还创建了一个简单的库,只是为了避免各种第三方库和谷歌框架的简单请求。

要发出类似AutocompleteReverseGeoPlace 信息或Draw 路径的 Google api 请求,您只需使用以下步骤:-

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

【讨论】:

    猜你喜欢
    • 2017-07-09
    • 1970-01-01
    • 1970-01-01
    • 2014-01-02
    • 2021-12-28
    • 1970-01-01
    • 2017-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多