I am Parsing json data from google maps api in swift, am trying to show current location using google maps api, getting current location latitude and longitude using didUpdateLocations. inside the serviceLocationupdate() posting lat and long values using post method but am not getting the response from the json. how can I get the response(data) from the json.
this is the code
var lat = ""
var long = ""
var latlng:String = ""
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location:CLLocationCoordinate2D = manager.location!.coordinate
self.lat = String(location.latitude)
self.long = String(location.longitude)
latlng = self.lat+"," + self.long
}
func serviceLocationupdate()
{
var request = URLRequest(url: URL(string: "http://maps.googleapis.com/maps/api/geocode/json")!)
let session = URLSession.shared
request.httpMethod = "POST"
----->> let bodyData = "latlng=\(latlng)&sensor=\("true")"
print("bodydata",bodyData)
request.httpBody = bodyData.data(using: String.Encoding.utf8);
let task = session.dataTask(with:request,completionHandler:{(d,response,error)in
do{
if let data = d{
do{
let jsonResult = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary
if let results = jsonResult["results"] as? [[String : AnyObject]] {
for result in results{
if let addressComponents = result["address_components"] as? [[String : AnyObject]] {
print(addressComponents)
}
}
}
} catch
{
}
}
}
})
task.resume()
}
if i can use this api its working fine getting proper data
var request = URLRequest(url: URL(string:"http://maps.googleapis.com/maps/api/geocode/json?latlng=13.026811,77.593773&sensor=true")!)
I want display the current location using latitude and longitude