我正在尝试使用 Google Maps API 获取两个位置之间的驾车/步行距离。我正在将我的应用程序从 objective-c 更新到 Swift 3,现在我一直在将这段代码转换为 Swift 3。
NSString *dist;
NSString *strUrl = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=%f,%f&sensor=false&mode=%@", closestLocation.coordinate.latitude, closestLocation.coordinate.longitude, _currentUserLocation.coordinate.latitude, _currentUserLocation.coordinate.longitude, @"DRIVING"];
NSURL *url = [NSURL URLWithString:[strUrl stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLQueryAllowedCharacterSet]];
NSData *jsonData = [NSData dataWithContentsOfURL:url];
if(jsonData != nil)
{
NSError *error = nil;
id result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
NSMutableArray *arrDistance=[result objectForKey:@"routes"];
if ([arrDistance count]==0) {
NSLog(@"N.A.");
}
else{
NSMutableArray *arrLeg=[[arrDistance objectAtIndex:0]objectForKey:@"legs"];
NSMutableDictionary *dictleg=[arrLeg objectAtIndex:0];
dist = [NSString stringWithFormat:@"%@",[[dictleg objectForKey:@"distance"] objectForKey:@"text"]];
}
}
else{
NSLog(@"N.A.");
}
我尝试使用 Swiftify 等工具,但到处都出现错误。我现在拥有的是:
var dist: String = ""
var strUrl: String = "http://maps.googleapis.com/maps/api/directions/json?origin=\(closestLocation!.coordinate.latitude),\(closestLocation!.coordinate.longitude)&destination=\(currentUserLocation!.coordinate.latitude),\(currentUserLocation!.coordinate.longitude)&sensor=false&mode=\("DRIVING")"
var url = URL(string: strUrl)
var jsonData = Data(contentsOf: url!)
if (jsonData != nil) {
var error: Error? = nil
var result: Any? = try? JSONSerialization.jsonObject(with: jsonData, options: JSONSerialization.ReadingOptions.Element.mutableContainers)
var arrDistance: [Any]? = (result?["routes"] as? [Any])
if arrDistance?.count == 0 {
print("N.A.")
}
else {
var arrLeg: [Any]? = ((arrDistance?[0] as? [Any])?["legs"] as? [Any])
var dictleg: [AnyHashable: Any]? = (arrLeg?[0] as? [AnyHashable: Any])
dist = "\(dictleg?["distance"]["text"])"
}
}
else {
print("N.A.")
}
我现在的错误如下:
使用 Data(contentsOf: url!) 它告诉我“调用可以抛出,但它没有标记为‘try’并且错误没有被处理
它不喜欢我将 [Any] 用于我的 arrDistance 变量的方式。
如果有人知道如何在 Swift 3 中实现此 API 调用,那将非常有帮助。谢谢
最佳答案
编译器说的是什么 Data(contentsOf:)
将 throws
异常,因此您需要使用 do catch
block 来处理它。现在按照评论中的建议,您需要使用 dataTask(with:)
而不是 Data(contentsOf:)
来下载数据。您也可以使用您的代码制作完成 block ,因此请像下面这样更改您的代码。
func getDistance(completion: @escaping(String) -> Void) {
var dist = ""
let strUrl = "http://maps.googleapis.com/maps/api/directions/json?origin=\(closestLocation!.coordinate.latitude),\(closestLocation!.coordinate.longitude)&destination=\(currentUserLocation!.coordinate.latitude),\(currentUserLocation!.coordinate.longitude)&sensor=false&mode=\("DRIVING")"
let url = URL(string: strUrl)!
let task = URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) in
guard let data = data, error == nil else {
print(error?.localizedDescription ?? "")
completion(dist)
return
}
if let result = (try? JSONSerialization.jsonObject(with: data, options: [])) as? [String:Any],
let routes = result["routes"] as? [[String:Any]], let route = routes.first,
let legs = route["legs"] as? [[String:Any]], let leg = legs.first,
let distance = leg["distance"] as? [String:Any], let distanceText = distance["text"] as? String {
dist = distanceText
}
completion(dist)
})
task.resume()
}
现在只需像这样调用 getDistance
函数即可。
self.getDistance { (distance) in
//Update UI on main thread
DispatchQueue.main.async {
print(distance)
self.label.text = distance
}
}
关于swift - 使用 Google Maps API 在 Swift 3 中查找 2 个位置之间的驾驶/步行距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43485070/