It is my first question about that is working if I set default lat and long and now I try to get location current location and use that. Also I following this one Get User's Current Location / Coordinates but failed to reach result.
I am not sure what is going on here it is not working try to do it several way
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
//print(locations)
//get the actual location from the device and first one each move
let userClLocation : CLLocation = locations[0]
let latitude = userClLocation.coordinate.latitude
let longitude = userClLocation.coordinate.longitude
durationDestance(origin: "\(latitude),\(longitude)", destination: destination, mode:"auto")
}
func durationDestance(origin: String, destination: String, mode:String) {
var urlString : String = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=\(origin)&destinations=\(destination)&mode=\(mode)&key=AIzaSyAVOt9LLagNGSOI8O0ri1Sbahcl_q5AFYc";
urlString = urlString.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed)!
let session = URLSession.shared
let url = URL(string:urlString)!
session.dataTask(with: url) { (data: Data?, response: URLResponse?, erorr: Error?) -> Void in
print("url: \(urlString)")
if let responseData = data {
do{
let json = try JSONSerialization.jsonObject(with: responseData, options: JSONSerialization.ReadingOptions.allowFragments);
print(json);
}
catch{
print("do not serialization :)");
}
}
}.resume();
}
API every time json response
status = "ZERO_RESULTS";
My Goal : I am not sure why code not working my main goal is: get user current location and destination to calculate duration time and km.