Tuesday, July 9, 2019

Google Map get Api for location with Alamofire

//MARK: Api for all Location

    var allLocationdataGMS = [LocationModel]()
    var selectedArray = [String]()



func loctaion(name:String,completion:@escaping (_ result:[LocationModel]?) -> Void) {
        
        let url = "https://maps.googleapis.com/maps/api/place/autocomplete/json?input=\(name))&components=country:us&key=\(GOOGLE_API_KEY)"
        
        Alamofire.request(URL(string: url, relativeTo: nil)!, method: .get, parameters:nil , encoding: URLEncoding.default, headers: headers).response { (resposne) in
            
            self.printRequest(resposne)
            
            do {
                let data = try self.decoder.decode(LocationData.self, from: resposne.data ?? Data())
                print("Success")
             
                    completion(data.predictions)
               
                KVNProgress.dismiss()
                
            } catch let error {
                KVNProgress.dismiss()
                self.handleError(data: resposne, error: error)
            }
        }
        
    }
    

//MARK:- api calling with dropDown 


func networkApiCall(){
        
        Network.shared.loctaion(name: self.serach_Loacation.text ?? "") { (result) in
            
            guard let user = result else{
                
                return
            }
            self.allLocationdataGMS = user
            
            self.selectedArrayself.allLocationdataGMS.map({$0.predictionDescription!})
            let dropDown = DropDown()
            
            dropDown.anchorView = self.serach_Loacation
            dropDown.bottomOffset = CGPoint(x: 0, y:(dropDown.anchorView?.plainView.bounds.height)!)
            
            // The list of items to display. Can be changed dynamically
            dropDown.dataSource = self.selectedArray
            dropDown.textColor = UIColor.black
            dropDown.backgroundColor = UIColor.red
            dropDown.selectionAction = { [unowned self] (index: Int, item: String) in
                print("Selected item: \(item) at index: \(index)")
                
            }
            dropDown.show()
    }
    }

//MARK: Api for all Location lat Long

var name = "Noida"
    func loctaionForLatLong(name:String,completion:@escaping (_ result:[LocationModel]?) -> Void) {
        
        let url = "https://maps.googleapis.com/maps/api/geocode/json?"
        
        let geocodeURLString = "\(url)address=\(name)&key=\(GOOGLE_API_KEY)"
        
        
        Alamofire.request(URL(string: geocodeURLString, relativeTo: nil)!, method: .get, parameters:nil , encoding: URLEncoding.default, headers: headers).response { (resposne) in
            
            self.printRequest(resposne)
            
            do {
                let data = try self.decoder.decode(LocationData.self, from: resposne.data ?? Data())
                print("Success")
                
                completion(data.predictions)
                
                KVNProgress.dismiss()
                
            } catch let error {
                KVNProgress.dismiss()
                self.handleError(data: resposne, error: error)
            }
        }
        

    }

//MARK:- api calling for lat long 

func locationForLatLong(){
        
        Network.shared.loctaionForLatLong(name:"Delhi") { (result) in
            
            guard let user = result else{
                
                return
            }
    }


Model:

// MARK: - Welcome
struct LocationData: Codable {
    let predictions: [LocationModel]?
    let status: String?
}

// MARK: - Prediction
struct LocationModel: Codable {
    let predictionDescription, id: String?
    let matchedSubstrings: [MatchedSubstring]?
    let placeID, reference: String?
    let structuredFormatting: StructuredFormatting?
    let terms: [Term]?
    let types: [String]?
    
    enum CodingKeys: String, CodingKey {
        case predictionDescription = "description"
        case id
        case matchedSubstrings = "matched_substrings"
        case placeID = "place_id"
        case reference
        case structuredFormatting = "structured_formatting"
        case terms, types
    }
}

// MARK: - MatchedSubstring
struct MatchedSubstring: Codable {
    let length, offset: Int?
}

// MARK: - StructuredFormatting
struct StructuredFormatting: Codable {
    let mainText: String?
    let mainTextMatchedSubstrings: [MatchedSubstring]?
    let secondaryText: String?
    
    enum CodingKeys: String, CodingKey {
        case mainText = "main_text"
        case mainTextMatchedSubstrings = "main_text_matched_substrings"
        case secondaryText = "secondary_text"
    }
}

// MARK: - Term
struct Term: Codable {
    let offset: Int?
    let value: String?
}

No comments:

Post a Comment