The main problem is that your JSON is using the wrong key for the longitude
. You're using longtide
, which is obviously not what you intended. If you use longitude
, it works fine.
A few unrelated observations:
You are doing do
-catch
, but don't have a try
. You are using try?
which doesn't throw any errors, but rather just returns nil
if there was a problem. Therefore is nothing to catch. Use try
, not try?
if you want to catch the error.
You have a curious model object in which you have private properties, but then have computed properties that get
and set
the private property. If you're not doing anything valuable in your custom getter and setter, you should lose those computed properties entirely because this is just convoluted and inefficient. Plus the underscore convention is used for hidden, private properties, not for properties you expose.
One is left to infer that your GoogleMap
method is returning a [String: Any]
produced by toJSON
. In Swift 4, you can use JSONEncoder
and make your model object Encodable
(or Codable
), and you can avoid that toJSON
method. Just change GoogleMap
to return the simple model object which you can then encode with JSONEncoder
.
So, that leaves us with:
let url = URL(string: "http://test.com/google/getAddress")!
var request = URLRequest(url: url)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
let coordinate = Coordinate(latitude: 34.05222, longitude: -118.242) // set this however you want
request.httpBody = try! JSONEncoder().encode(coordinate)
URLSession.shared.dataTask(with: request) { data, _, error in
guard let data = data, error == nil else {
print(error ?? "Unknown error")
return
}
do {
let responseObject = try JSONSerialization.jsonObject(with: data)
print(responseObject)
} catch let parseError {
print(parseError)
}
}.resume()
And your model object is reduced to merely:
struct Coordinate: Codable {
var latitude: Double
var longitude: Double
}
Or, if you really need it to be a reference type, a class
:
class Coordinate: Codable {
var latitude: Double
var longitude: Double
init(latitude: Double, longitude: Double) {
self.latitude = latitude
self.longitude = longitude
}
}
If you're stuck in Swift 3, then go ahead and make a dictionary, but I'd make it [String: Double]
, e.g.
let url = URL(string: "http://ipinbar.net:8080/google/getAddress")!
var request = URLRequest(url: url)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
let coordinate = Coordinate(latitude: 34.05222, longitude: -118.242) // set this however you want
request.httpBody = try! JSONSerialization.data(withJSONObject: coordinate.dictionary)
URLSession.shared.dataTask(with: request) { data, _, error in
guard let data = data, error == nil else {
print(error ?? "Unknown error")
return
}
do {
let responseObject = try JSONSerialization.jsonObject(with: data)
print(responseObject)
} catch let parseError {
print(parseError)
}
}.resume()
and
struct Coordinate {
var latitude: Double
var longitude: Double
var dictionary: [String: Double] { return ["latitude": latitude, "longitude": longitude] }
}