Thanks for the tremendous helps, I was able to get lat and lng by using Google geocoding API with swift. If anyone encounters some problems, hope this helps!
here is the code.
var components = URLComponents(string: "https://maps.googleapis.com/maps/api/geocode/json")!
let key = URLQueryItem(name: "key", value: "YOUR_KEY")
let address = URLQueryItem(name: "address", value: TOKYO)
components.queryItems = [key, address]
let task = URLSession.shared.dataTask(with: components.url!) { data, response, error in
guard let data = data, let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200, error == nil else {
print(String(describing: response))
print(String(describing: error))
return
}
guard let json = try! JSONSerialization.jsonObject(with: data) as? [String: Any] else {
print("not JSON format expected")
print(String(data: data, encoding: .utf8) ?? "Not string?!?")
return
}
guard let results = json["results"] as? [[String: Any]],
let geometry = results[0]["geometry"] as? [String:AnyObject],
let location = geometry["location"] as? [String:Double],
let lat = location["lat"],
let lng = location["lng"],
let status = json["status"] as? String,
status == "OK"
else{return}