Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func calculateRoutes(from f: CLLocationCoordinate2D, to t: CLLocationCoordinate2D) {
- let saddr = "(f.latitude),(f.longitude)"
- let daddr = "(t.latitude),(t.longitude)"
- let url = "https://maps.googleapis.com/maps/api/directions/json?origin=(saddr)&destination=(daddr)&sensor=false&mode=driving&key=(googleApiKey)"
- Alamofire.request(url).responseJSON { response in
- do {
- let json = try JSONSerialization.jsonObject(with: response.data!, options: .mutableContainers) as! NSDictionary
- let routes = (json.object(forKey: "routes") as! NSArray)
- if routes.count > 0 {
- if let route = ((routes.object(at: 0) as? NSDictionary)?.object(forKey: "overview_polyline") as? NSDictionary)?.value(forKey: "points") as? String {
- self.mapview.clear()
- let path = GMSPath(fromEncodedPath:route)!
- let polyline = GMSPolyline(path: path)
- polyline.strokeColor = UIColor.black
- polyline.strokeWidth = 5.0
- polyline.map = self.mapview
- }
- }
- } catch {
- }
- }
- }
Add Comment
Please, Sign In to add comment