私は、GoogleマップAPIから迅速にJSONデータを解析しています。現在の位置を表示するには、google maps apiを使用して、現在の位置の緯度と経度をdidUpdateLocationsを使用して表示しています。 serviceLocationupdate() postメソッドを使用して緯度と経度の値をポストしていますが、jsonからの応答が得られません。 jsonからどのように応答(データ)を取得できますか?JSON Postメソッドがすぐに動作しない
私はこのAPIにその作業罰金適切なデータを取得する
VAR要求=のURLRequest(URL:URL(文字列:! "http://maps.googleapis.com/maps/api/geocode/json?latlng=13.026811,77.593773&sensor=true"))を使用することができる場合、これはコード
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()
}
ある
をIは、緯度と経度これは遊び場で動作
おかげで、それがうまく働いてBRO – raj