iosswift3google-places-apigoogle-maps-sdk-ios

Google Places Autocomplete Swift 3


I want to integrate the Google Places Autocomplete API in my project but I am unable to achieve it with the documentation and also couldn't get some solid references for Swift-3. Can anyone please help? Thanks.


Solution

  • Details

    Swift 4, xCode 9

    Solution

    Usual http request. More details: https://developers.google.com/places/ios-api/autocomplete

    Full sample

    PodFile

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

    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{ $0.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)
        }
    }
    

    Results

    enter image description here