Guest User

Untitled

a guest
Feb 19th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. func calculateRoutes(from f: CLLocationCoordinate2D, to t: CLLocationCoordinate2D) {
  2.  
  3. let saddr = "(f.latitude),(f.longitude)"
  4. let daddr = "(t.latitude),(t.longitude)"
  5.  
  6. let url = "https://maps.googleapis.com/maps/api/directions/json?origin=(saddr)&destination=(daddr)&sensor=false&mode=driving&key=(googleApiKey)"
  7.  
  8. Alamofire.request(url).responseJSON { response in
  9.  
  10. do {
  11. let json = try JSONSerialization.jsonObject(with: response.data!, options: .mutableContainers) as! NSDictionary
  12.  
  13. let routes = (json.object(forKey: "routes") as! NSArray)
  14.  
  15. if routes.count > 0 {
  16.  
  17. if let route = ((routes.object(at: 0) as? NSDictionary)?.object(forKey: "overview_polyline") as? NSDictionary)?.value(forKey: "points") as? String {
  18.  
  19. self.mapview.clear()
  20.  
  21. let path = GMSPath(fromEncodedPath:route)!
  22. let polyline = GMSPolyline(path: path)
  23. polyline.strokeColor = UIColor.black
  24. polyline.strokeWidth = 5.0
  25. polyline.map = self.mapview
  26.  
  27. }
  28.  
  29. }
  30.  
  31. } catch {
  32. }
  33.  
  34. }
  35. }
Add Comment
Please, Sign In to add comment