Google 地点自动完成 Swift 3
Google Places Autocomplete Swift 3
我想在我的项目中集成 Google Places Autocomplete API,但我无法通过文档实现它,也无法获得 Swift 的可靠参考-3。有人可以帮忙吗?谢谢
您需要先通过pods在您的项目中安装googleplace SDK。
pod 'GooglePlacePicker'
您可以使用此代码通过 pods.
安装 googlePlaces
详情
Swift4,xCode9
解决方案
普通的http请求。更多详情:https://developers.google.com/places/ios-api/autocomplete
完整样本
PodFile
target 'Whosebug-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())"
}
}
Network
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{ [=12=].title}
print("!!!! \(autocomplete)")
}
}
}
}
}
}
Extensions
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
}
}
ViewController
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)
}
}
结果
我还创建了一个简单的库,只是为了避免各种第三方库和 google 简单请求的框架。
要发出 Google api 请求,例如 Autocomplete
、ReverseGeo
、Place
信息或 Draw
路径,您可以简单地使用这些步骤:-
第 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") }
}
我想在我的项目中集成 Google Places Autocomplete API,但我无法通过文档实现它,也无法获得 Swift 的可靠参考-3。有人可以帮忙吗?谢谢
您需要先通过pods在您的项目中安装googleplace SDK。
pod 'GooglePlacePicker'
您可以使用此代码通过 pods.
详情
Swift4,xCode9
解决方案
普通的http请求。更多详情:https://developers.google.com/places/ios-api/autocomplete
完整样本
PodFile
target 'Whosebug-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())"
}
}
Network
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{ [=12=].title}
print("!!!! \(autocomplete)")
}
}
}
}
}
}
Extensions
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
}
}
ViewController
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)
}
}
结果
我还创建了一个简单的库,只是为了避免各种第三方库和 google 简单请求的框架。
要发出 Google api 请求,例如 Autocomplete
、ReverseGeo
、Place
信息或 Draw
路径,您可以简单地使用这些步骤:-
第 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") }
}