-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRouteCreater.swift
123 lines (114 loc) · 5.33 KB
/
RouteCreater.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//
// RouteCreater.swift
// TestGoogleMaps
//
// Created by N A Shashank on 12/14/17.
// Copyright © 2017 N A Shashank. All rights reserved.
//
import UIKit
import GoogleMaps
class RouteCreater {
class func navigationPath(dictPathInformation:[String:Any]) -> PathInformationModel?
{
guard let arrRoutes = dictPathInformation["routes"] as? [[String:Any]],arrRoutes.count > 0,let arrWaypointOrder = arrRoutes[0]["waypoint_order"] as? [Int],let arrLegs = arrRoutes[0]["legs"] as? [[String:Any]] else{
return nil
}
var pathInformation = PathInformationModel()
pathInformation.arrWaypointOrder = arrWaypointOrder
let path = GMSMutablePath()
for leg in arrLegs
{
var stepIndex = 0
let intermediatePath = GMSMutablePath()
guard let distance = leg["distance"] as? [String:Any],let duration = leg["duration"] as? [String:Any],let endLocation = leg["end_location"] as? [String:Any],let startLocation = leg["start_location"] as? [String:Any],let startAddress = leg["start_address"] as? String,let endAddress = leg["end_address"] as? String, let arrSteps = leg["steps"] as? [[String:Any]] else{
return nil
}
var leg = LegModel()
leg.distance = distance
leg.duration = duration
leg.endLocation = endLocation
leg.startLocation = startLocation
leg.startAddress = startAddress
leg.endAddress = endAddress
for step in arrSteps
{
guard let dictStartLocation = step["start_location"] as? [String:Any] else{
return nil
}
stepIndex = stepIndex + 1
guard let coordinate = RouteCreater.coordinateFromLocation(dict:dictStartLocation) else{
return nil
}
path.add(coordinate)
intermediatePath.add(coordinate)
guard let dictPolyLine = step["polyline"] as? [String:Any],let polyLinePoints = dictPolyLine["points"] as? String else{
return nil
}
let polyLinePath = GMSPath(fromEncodedPath: polyLinePoints)
guard let unwrappedPolyLinePath = polyLinePath else{
return nil
}
for index in 0..<unwrappedPolyLinePath.count()
{
path.add(unwrappedPolyLinePath.coordinate(at: index))
intermediatePath.add(unwrappedPolyLinePath.coordinate(at: index))
}
if arrSteps.count == stepIndex
{
guard let dictEndLocation = step["end_location"] as? [String:Any] else{
return nil
}
stepIndex = stepIndex + 1
guard let coordinate = RouteCreater.coordinateFromLocation(dict:dictEndLocation) else{
return nil
}
path.add(coordinate)
intermediatePath.add(coordinate)
}
}
leg.path = intermediatePath
pathInformation.arrLegs.append(leg)
}
pathInformation.path = path
return pathInformation
}
class func fetchRouteInformation(from:CLLocationCoordinate2D,to:CLLocationCoordinate2D,waypoints:[CLLocationCoordinate2D]?,successCallBack:@escaping ([String:Any]) -> Void,failureCallBack:@escaping Typealias.MapCallBack)
{
let origin = "\(from.latitude),\(from.longitude)"
let dest = "\(to.latitude),\(to.longitude)"
var baseURL = "https://maps.googleapis.com/maps/api/directions/json?origin=\(origin)&destination=\(dest)"
if let arrWaypoints = waypoints,let waypointString = self.waypointString(locations: arrWaypoints)
{
baseURL = baseURL + waypointString
}
let transitMode = "&mode=driving"
let key = "&key=\(Credentials.googleApiKey)"
baseURL = baseURL + transitMode + key
guard let url = URL(string: baseURL) else{
return
}
URLSession.shared.dataTask(with: url) { (data, url, error) in
guard let unwrappedData = data ,let jsonReceived = try? JSONSerialization.jsonObject(with: unwrappedData, options: JSONSerialization.ReadingOptions.mutableContainers) ,let dictReceived = jsonReceived as? [String:Any] else{
failureCallBack(MapError.pathFetchFailed)
return
}
successCallBack(dictReceived)
}.resume()
}
private class func coordinateFromLocation(dict:[String:Any]) -> CLLocationCoordinate2D?
{
guard let lat = dict["lat"] as? Double,let long = dict["lng"] as? Double else{
return nil
}
return CLLocationCoordinate2D(latitude: lat, longitude: long)
}
private class func waypointString(locations:[CLLocationCoordinate2D]) -> String?
{
var strTemp = "&waypoints=optimize:true|"
locations.forEach { (location) in
strTemp = strTemp + "\(location.latitude),\(location.longitude)|"
}
strTemp.remove(at: strTemp.index(before: strTemp.endIndex))
return strTemp.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)
}
}