-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathroute.js
executable file
·242 lines (165 loc) · 6.37 KB
/
route.js
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
var application = require("application");
var polyline
function GoogleParser(params) {
this.feedUrl = params.feedUrl,
this.parse = function() {
// Cria uma rota vazia
var route = new Route()
var self = this
return fetch(this.feedUrl, {
method: "GET",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
}).then(function(response){
if (!response.ok) {
console.log(JSON.stringify(response));
throw "Ocorreu um erro desconhecido ao conectar com o servidor. Detalhes: " + response.statusText
}
return response;
}).then(function(response) {
var result = JSON.parse(response._bodyText)
//console.log("###########")
//console.log(JSON.stringify(result.routes[0]))
//console.log("###########")
// Transforma a string em JSON
//var json = new JSONObject(result);
// Pega a primeira rota retornada
var jsonRoute = result.routes[0]
var leg = jsonRoute.legs[0]
// Obtém os passos do caminho
var steps = leg.steps
var numSteps = steps.length
/*
* Itera através dos passos, decodificando
* a polyline e adicionando à rota.
*/
var step
for (var i = 0; i < numSteps; i++) {
// Obtém o passo corrente
step = steps[i]
// Decodifica a polyline e adiciona à rota
route.addPoints(self.decodePolyLine(step.polyline.points));
}
//console.log("### route.getPoints().length()=" + route.getPoints().length)
return route
})
}
this.decodePolyLine = function(poly) {
var len = poly.length
var index = 0, lat = 0, lng = 0;
var decoded = []
while (index < len) {
var b, shift = 0, result = 0
do {
b = poly.charCodeAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20 && index < len);
if(index >= len)
break
var dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = poly.charCodeAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
if(index >= len)
break
var dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lng += dlng;
//console.log("{ lat: " + (lat / 1E5) + ", lng: " + (lng / 1E5) + "},")
if(application.android){
decoded.push(new com.google.android.gms.maps.model.LatLng(lat / 1E5, lng / 1E5));
}else if(application.ios){
decoded.push(CLLocationCoordinate2DMake(lat / 1E5, lng / 1E5));
}
}
return decoded;
}
};
function Route() {
this.points = [],
this.polyline,
this.addPoints = function(pts) {
for(var i = 0; i < pts.length; i++)
this.points.push(pts[i])
},
this.getPoints = function() {
return this.points;
},
this.setPolyline = function(p) {
this.polyline = p;
},
this.getPolyline = function() {
return this.polyline;
}
};
function RouteTask(){
this.routeObj = undefined,
this.execute = function(params){
var self = this
this.directions(params.origin, params.destination, function(route){
self.routeObj = route
if(application.android){
var options = new com.google.android.gms.maps.model
.PolylineOptions()
.width(12)
.color(android.graphics.Color.parseColor("#05b1fb"))
.geodesic(true)
for(var i = 0; i < route.getPoints().length; i++)
options.add(route.getPoints()[i]);
if(polyline)
polyline.remove()
polyline = params.mapView.addPolyline(options);
polyline.setClickable(true)
//params.mapView.invalidate()
}else if(application.ios){
var path = GMSMutablePath.alloc().init()
for(var i = 0; i < route.getPoints().length; i++)
path.addCoordinate(route.getPoints()[i]);
if(polyline)
polyline.map = null
polyline = GMSPolyline.polylineWithPath(path)
polyline.strokeWidth = 6;
polyline.strokeColor = UIColor.blueColor()
polyline.map = params.mapView
console.log("### add points end")
}
if(params.doneCallback)
params.doneCallback(params)
})
},
this.directions = function(start, dest, done) {
// Formatando a URL com a latitude e longitude
// de origem e destino.
var urlRota = "http://maps.googleapis.com/maps/api/"
+ "directions/json?origin=" + start.latitude + "," + start.longitude + "&"
+ "destination=" + dest.latitude + "," + dest.longitude + "&"
+ "sensor=true&mode=driving"
//console.log(urlRota)
new GoogleParser({'feedUrl': urlRota})
.parse()
.then(function(rota){
done(rota)
});
},
this.remove = function(){
if(polyline){
if(application.android)
polyline.remove()
else if(application.ios)
polyline.map = null
}
},
this.getCoordenates = function(){
if(this.routeObj)
return this.routeObj.getPoints()
return []
}
}
exports.RouteTask = RouteTask;