2017-04-19 53 views
2

我想要使用Google地图API获取两个地点之间的驾驶/步行距离。我更新我的应用程序斯威夫特3,Objective-C和我现在就卡在这个代码块转换成斯威夫特3.使用谷歌地图API在Swift 3中寻找驾车/步行距离2

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.") 
    } 

我的错误,现在有以下几种:

的数据(contentsOf:URL)它告诉我“呼叫可以抛出,但没有标明“试“和错误没有被处理

而且它不会像我如何使用[任何]我arrDistance变量。

如果有人知道如何实现斯威夫特3此API调用,这将是非常有益的谢谢

+1

与问题无关,但您不应使用Data(contentsOf:url!)来下载数据。你应该使用URLSession'dataTask(with:URL)' –

回答

3

什么编译器说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 
    } 
} 
+0

但是然后我遇到了问题,无法存储该距离变量的值。我试图把这个值放在我的一个视图控制器的标签上 – trludt

+0

@trludt你不是简单地在'getDistance'的完成中设置标签文本而不是'print'语句,就像'self.getDistance {(distance)in yourLabel.text =距离 }' –

+0

需要一段时间让标签显示值。它在技术上是有效的,但我需要这些信息在视图加载后立即显示出来。 – trludt