如果我设置默认的纬度和经度,这是我的第一个问题,现在我尝试获取当前位置并使用它。我也关注这个 Get User's Current Location / Coordinates但未能达到结果。
我不确定这里发生了什么它不起作用尝试以多种方式进行
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每次json响应
状态=“零结果”;
我的目标: 我不确定为什么代码不起作用我的主要目标是:获取用户当前位置和目的地以计算持续时间和公里。