Administrator
2022-09-14 58d006e05dcf2a20d0ec5367dd03d66a61db6849
提交 | 用户 | 时间
58d006 1 var travelMode, unitSystem;
A 2
3 GMaps.prototype.getRoutes = function(options) {
4   switch (options.travelMode) {
5     case 'bicycling':
6       travelMode = google.maps.TravelMode.BICYCLING;
7       break;
8     case 'transit':
9       travelMode = google.maps.TravelMode.TRANSIT;
10       break;
11     case 'driving':
12       travelMode = google.maps.TravelMode.DRIVING;
13       break;
14     default:
15       travelMode = google.maps.TravelMode.WALKING;
16       break;
17   }
18
19   if (options.unitSystem === 'imperial') {
20     unitSystem = google.maps.UnitSystem.IMPERIAL;
21   }
22   else {
23     unitSystem = google.maps.UnitSystem.METRIC;
24   }
25
26   var base_options = {
27         avoidHighways: false,
28         avoidTolls: false,
29         optimizeWaypoints: false,
30         waypoints: []
31       },
32       request_options =  extend_object(base_options, options);
33
34   request_options.origin = /string/.test(typeof options.origin) ? options.origin : new google.maps.LatLng(options.origin[0], options.origin[1]);
35   request_options.destination = /string/.test(typeof options.destination) ? options.destination : new google.maps.LatLng(options.destination[0], options.destination[1]);
36   request_options.travelMode = travelMode;
37   request_options.unitSystem = unitSystem;
38
39   delete request_options.callback;
40   delete request_options.error;
41
42   var self = this,
43       service = new google.maps.DirectionsService();
44
45   service.route(request_options, function(result, status) {
46     if (status === google.maps.DirectionsStatus.OK) {
47       for (var r in result.routes) {
48         if (result.routes.hasOwnProperty(r)) {
49           self.routes.push(result.routes[r]);
50         }
51       }
52
53       if (options.callback) {
54         options.callback(self.routes);
55       }
56     }
57     else {
58       if (options.error) {
59         options.error(result, status);
60       }
61     }
62   });
63 };
64
65 GMaps.prototype.removeRoutes = function() {
66   this.routes = [];
67 };
68
69 GMaps.prototype.getElevations = function(options) {
70   options = extend_object({
71     locations: [],
72     path : false,
73     samples : 256
74   }, options);
75
76   if (options.locations.length > 0) {
77     if (options.locations[0].length > 0) {
78       options.locations = array_flat(array_map([options.locations], arrayToLatLng,  false));
79     }
80   }
81
82   var callback = options.callback;
83   delete options.callback;
84
85   var service = new google.maps.ElevationService();
86
87   //location request
88   if (!options.path) {
89     delete options.path;
90     delete options.samples;
91
92     service.getElevationForLocations(options, function(result, status) {
93       if (callback && typeof(callback) === "function") {
94         callback(result, status);
95       }
96     });
97   //path request
98   } else {
99     var pathRequest = {
100       path : options.locations,
101       samples : options.samples
102     };
103
104     service.getElevationAlongPath(pathRequest, function(result, status) {
105      if (callback && typeof(callback) === "function") {
106         callback(result, status);
107       }
108     });
109   }
110 };
111
112 GMaps.prototype.cleanRoute = GMaps.prototype.removePolylines;
113
114 GMaps.prototype.drawRoute = function(options) {
115   var self = this;
116
117   this.getRoutes({
118     origin: options.origin,
119     destination: options.destination,
120     travelMode: options.travelMode,
121     waypoints: options.waypoints,
122     unitSystem: options.unitSystem,
123     error: options.error,
124     callback: function(e) {
125       if (e.length > 0) {
126         self.drawPolyline({
127           path: e[e.length - 1].overview_path,
128           strokeColor: options.strokeColor,
129           strokeOpacity: options.strokeOpacity,
130           strokeWeight: options.strokeWeight
131         });
132         
133         if (options.callback) {
134           options.callback(e[e.length - 1]);
135         }
136       }
137     }
138   });
139 };
140
141 GMaps.prototype.travelRoute = function(options) {
142   if (options.origin && options.destination) {
143     this.getRoutes({
144       origin: options.origin,
145       destination: options.destination,
146       travelMode: options.travelMode,
147       waypoints : options.waypoints,
148       error: options.error,
149       callback: function(e) {
150         //start callback
151         if (e.length > 0 && options.start) {
152           options.start(e[e.length - 1]);
153         }
154
155         //step callback
156         if (e.length > 0 && options.step) {
157           var route = e[e.length - 1];
158           if (route.legs.length > 0) {
159             var steps = route.legs[0].steps;
160             for (var i=0, step; step=steps[i]; i++) {
161               step.step_number = i;
162               options.step(step, (route.legs[0].steps.length - 1));
163             }
164           }
165         }
166
167         //end callback
168         if (e.length > 0 && options.end) {
169            options.end(e[e.length - 1]);
170         }
171       }
172     });
173   }
174   else if (options.route) {
175     if (options.route.legs.length > 0) {
176       var steps = options.route.legs[0].steps;
177       for (var i=0, step; step=steps[i]; i++) {
178         step.step_number = i;
179         options.step(step);
180       }
181     }
182   }
183 };
184
185 GMaps.prototype.drawSteppedRoute = function(options) {
186   var self = this;
187   
188   if (options.origin && options.destination) {
189     this.getRoutes({
190       origin: options.origin,
191       destination: options.destination,
192       travelMode: options.travelMode,
193       waypoints : options.waypoints,
194       error: options.error,
195       callback: function(e) {
196         //start callback
197         if (e.length > 0 && options.start) {
198           options.start(e[e.length - 1]);
199         }
200
201         //step callback
202         if (e.length > 0 && options.step) {
203           var route = e[e.length - 1];
204           if (route.legs.length > 0) {
205             var steps = route.legs[0].steps;
206             for (var i=0, step; step=steps[i]; i++) {
207               step.step_number = i;
208               self.drawPolyline({
209                 path: step.path,
210                 strokeColor: options.strokeColor,
211                 strokeOpacity: options.strokeOpacity,
212                 strokeWeight: options.strokeWeight
213               });
214               options.step(step, (route.legs[0].steps.length - 1));
215             }
216           }
217         }
218
219         //end callback
220         if (e.length > 0 && options.end) {
221            options.end(e[e.length - 1]);
222         }
223       }
224     });
225   }
226   else if (options.route) {
227     if (options.route.legs.length > 0) {
228       var steps = options.route.legs[0].steps;
229       for (var i=0, step; step=steps[i]; i++) {
230         step.step_number = i;
231         self.drawPolyline({
232           path: step.path,
233           strokeColor: options.strokeColor,
234           strokeOpacity: options.strokeOpacity,
235           strokeWeight: options.strokeWeight
236         });
237         options.step(step);
238       }
239     }
240   }
241 };
242
243 GMaps.Route = function(options) {
244   this.origin = options.origin;
245   this.destination = options.destination;
246   this.waypoints = options.waypoints;
247
248   this.map = options.map;
249   this.route = options.route;
250   this.step_count = 0;
251   this.steps = this.route.legs[0].steps;
252   this.steps_length = this.steps.length;
253
254   this.polyline = this.map.drawPolyline({
255     path: new google.maps.MVCArray(),
256     strokeColor: options.strokeColor,
257     strokeOpacity: options.strokeOpacity,
258     strokeWeight: options.strokeWeight
259   }).getPath();
260 };
261
262 GMaps.Route.prototype.getRoute = function(options) {
263   var self = this;
264
265   this.map.getRoutes({
266     origin : this.origin,
267     destination : this.destination,
268     travelMode : options.travelMode,
269     waypoints : this.waypoints || [],
270     error: options.error,
271     callback : function() {
272       self.route = e[0];
273
274       if (options.callback) {
275         options.callback.call(self);
276       }
277     }
278   });
279 };
280
281 GMaps.Route.prototype.back = function() {
282   if (this.step_count > 0) {
283     this.step_count--;
284     var path = this.route.legs[0].steps[this.step_count].path;
285
286     for (var p in path){
287       if (path.hasOwnProperty(p)){
288         this.polyline.pop();
289       }
290     }
291   }
292 };
293
294 GMaps.Route.prototype.forward = function() {
295   if (this.step_count < this.steps_length) {
296     var path = this.route.legs[0].steps[this.step_count].path;
297
298     for (var p in path){
299       if (path.hasOwnProperty(p)){
300         this.polyline.push(path[p]);
301       }
302     }
303     this.step_count++;
304   }
305 };