I have used the Google API to fetch nearby locations. I have made the API call and have got the proper results (JSON response) also.
This is the model class I have made for parsing the JSON response:
class Locations: Codable {
var locationName: String?
var locationAddress: String?
enum CodingKeys: String, CodingKey {
case locationName = "name"
case locationAddress = "vicinity"
}
required init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
locationName = try? values.decode(String.self, forKey: .locationName)
locationAddress = try? values.decode(String.self, forKey: .locationAddress)
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(locationName, forKey: .locationName)
try container.encode(locationAddress, forKey: .locationAddress)
}
}
In the above class, I have mentioned only two variables namely location and address since I want to display just those two values on my tableview.
But after fetching the results, in this part below, I’m not able to go inside the if let
condition:
WebServiceClient.shared.getNearbyLocationsList(withParameters: parameters, lattitude: lattitude, longitude: longitude) { [weak self] (isSuccess, result) in
guard let `self` = self else { return }
if isSuccess, result != nil {
print("Successfully fetched nearby locations!")
//I have data in the result but it is not going into the if-let condition.
if let productService = result?["results"] as? [[String: Any]] ,
let jsonData = try? JSONSerialization.data(withJSONObject: productService as Any, options: []),
let locationsList = try? JSONDecoder().decode([Locations].self, from: jsonData) {
self.locationsList = locationsList
}
This is a small snippet of the API response:
{
"html_attributions" : [],
"next_page_token" : "CsQEQAIAAG….”,
"results" : [
{
"geometry" : {
"location" : {
"lat" : 19.07456,
"lng" : 74.86545
},
"viewport" : {
"northeast" : {
"lat" : 19.2456,
"lng" : 74.98456
},
"southwest" : {
"lat" : 18.8564,
"lng" : 76.7745645
}
}
},
"icon" : “sdf.png",
"id" : "2e66c82c2e8d7149bd7e026089ff5dedb8346bd5",
"name" : "Mumbai",
"photos" : [
{
"height" : 2304,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/111225135501811810933/photos\"\u003eRamkrishna Pudasaini\u003c/a\u003e"
],
"photo_reference" : "CmRZAAAA4WHsIxpFh…”,
"width" : 3456
}
],
"place_id" : "Chbdfbdfb_tpF0",
"reference" : "ChIJbnfgh54_tpF0",
"scope" : "GOOGLE",
"types" : [ "locality", "political" ],
"vicinity" : "Mumbai"
}
],
“status” : “OK”
}
And from this API response, I wanted to get the data associated with name and vicinity.
Please shift JSONDecoder part in the do-catch statement and it will work.
if let productService = result?["results"] as? [[String: Any]] ,
let jsonData = try? JSONSerialization.data(withJSONObject: productService as Any, options: []) {
do {
let locationsList = try JSONDecoder().decode([LocationInfo].self, from: jsonData)
self.locationsList = locationsList
} catch {
print("error \(error)"
}
}