使用 Google Maps API 在 Swift 3 中查找 2 个位置之间的驾驶/步行距离

特鲁特

我正在尝试使用 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 调用,那将会非常有帮助。谢谢

尼拉夫D

编译器所说的Data(contentsOf:)throws异常,因此您需要使用do catch来处理它现在正如评论中所建议的,您需要使用dataTask(with:)而不是Data(contentsOf:)下载数据。你也可以用你的代码制作完成块,所以在你的代码中进行如下更改。

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
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Google Maps Api(JSON)获取两个位置之间的距离

使用Google Map API计算两个位置之间的距离

使用Google Maps API在Android中查找设备当前位置的纬度和经度值

使用Google Maps JavaScript API v3和Geocoding API映射多个位置

使用Google Maps API查找最近的标记

使用STDistance在2个位置之间的距离

计算R中2个位置之间的距离

Google Maps iOS SDK,获取2个位置之间的路线

Google Maps API-查找最近的位置

在两个位置Google Maps API v2之间添加折线

查找当前位置和另一个位置之间的距离(Swift 5)

在Google Maps API中插入从文件读取的多个位置

Google Maps API 中的距离单位

使用Google Maps API从用户的邮政编码中查找最近的条

如何使用Google Maps API查找流量是否在给定路径中

在Google Maps API v3中使用多个标记自动对中地图

无法使用Google Maps API v3计算道路距离

仅运行Google Maps Android API v2 3

使用Google Maps Geocode查找(API密钥)的奇怪响应

如何使用Google Maps API查找给定位置附近的地标?

ArcMap导出地图以在Google Maps API v3中使用

如何在Google Maps API v3中使用SVG标记

如何使用Google Maps JS API在3D建筑中显示地图

使用可拖动的Google Maps API V3在输入字段中获取标记地址

在Google Maps SDK中查找要路由的位置的距离

在经纬度范围内按名称在Google Maps API中查找位置

如何在特定半径内使用Google Maps API v3查找和显示所有POI?

Google Maps v3 WebService API直线距离

使用API V2和mapfragment的google maps