gmapsjs-rails 0.2.30.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,305 @@
1
+ var travelMode, unitSystem;
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
+ };
@@ -0,0 +1,245 @@
1
+ GMaps.prototype.toImage = function(options) {
2
+ var options = options || {},
3
+ static_map_options = {};
4
+
5
+ static_map_options['size'] = options['size'] || [this.el.clientWidth, this.el.clientHeight];
6
+ static_map_options['lat'] = this.getCenter().lat();
7
+ static_map_options['lng'] = this.getCenter().lng();
8
+
9
+ if (this.markers.length > 0) {
10
+ static_map_options['markers'] = [];
11
+
12
+ for (var i = 0; i < this.markers.length; i++) {
13
+ static_map_options['markers'].push({
14
+ lat: this.markers[i].getPosition().lat(),
15
+ lng: this.markers[i].getPosition().lng()
16
+ });
17
+ }
18
+ }
19
+
20
+ if (this.polylines.length > 0) {
21
+ var polyline = this.polylines[0];
22
+
23
+ static_map_options['polyline'] = {};
24
+ static_map_options['polyline']['path'] = google.maps.geometry.encoding.encodePath(polyline.getPath());
25
+ static_map_options['polyline']['strokeColor'] = polyline.strokeColor
26
+ static_map_options['polyline']['strokeOpacity'] = polyline.strokeOpacity
27
+ static_map_options['polyline']['strokeWeight'] = polyline.strokeWeight
28
+ }
29
+
30
+ return GMaps.staticMapURL(static_map_options);
31
+ };
32
+
33
+ GMaps.staticMapURL = function(options){
34
+ var parameters = [],
35
+ data,
36
+ static_root = 'http://maps.googleapis.com/maps/api/staticmap';
37
+
38
+ if (options.url) {
39
+ static_root = options.url;
40
+ delete options.url;
41
+ }
42
+
43
+ static_root += '?';
44
+
45
+ var markers = options.markers;
46
+
47
+ delete options.markers;
48
+
49
+ if (!markers && options.marker) {
50
+ markers = [options.marker];
51
+ delete options.marker;
52
+ }
53
+
54
+ var styles = options.styles;
55
+
56
+ delete options.styles;
57
+
58
+ var polyline = options.polyline;
59
+ delete options.polyline;
60
+
61
+ /** Map options **/
62
+ if (options.center) {
63
+ parameters.push('center=' + options.center);
64
+ delete options.center;
65
+ }
66
+ else if (options.address) {
67
+ parameters.push('center=' + options.address);
68
+ delete options.address;
69
+ }
70
+ else if (options.lat) {
71
+ parameters.push(['center=', options.lat, ',', options.lng].join(''));
72
+ delete options.lat;
73
+ delete options.lng;
74
+ }
75
+ else if (options.visible) {
76
+ var visible = encodeURI(options.visible.join('|'));
77
+ parameters.push('visible=' + visible);
78
+ }
79
+
80
+ var size = options.size;
81
+ if (size) {
82
+ if (size.join) {
83
+ size = size.join('x');
84
+ }
85
+ delete options.size;
86
+ }
87
+ else {
88
+ size = '630x300';
89
+ }
90
+ parameters.push('size=' + size);
91
+
92
+ if (!options.zoom && options.zoom !== false) {
93
+ options.zoom = 15;
94
+ }
95
+
96
+ var sensor = options.hasOwnProperty('sensor') ? !!options.sensor : true;
97
+ delete options.sensor;
98
+ parameters.push('sensor=' + sensor);
99
+
100
+ for (var param in options) {
101
+ if (options.hasOwnProperty(param)) {
102
+ parameters.push(param + '=' + options[param]);
103
+ }
104
+ }
105
+
106
+ /** Markers **/
107
+ if (markers) {
108
+ var marker, loc;
109
+
110
+ for (var i=0; data=markers[i]; i++) {
111
+ marker = [];
112
+
113
+ if (data.size && data.size !== 'normal') {
114
+ marker.push('size:' + data.size);
115
+ delete data.size;
116
+ }
117
+ else if (data.icon) {
118
+ marker.push('icon:' + encodeURI(data.icon));
119
+ delete data.icon;
120
+ }
121
+
122
+ if (data.color) {
123
+ marker.push('color:' + data.color.replace('#', '0x'));
124
+ delete data.color;
125
+ }
126
+
127
+ if (data.label) {
128
+ marker.push('label:' + data.label[0].toUpperCase());
129
+ delete data.label;
130
+ }
131
+
132
+ loc = (data.address ? data.address : data.lat + ',' + data.lng);
133
+ delete data.address;
134
+ delete data.lat;
135
+ delete data.lng;
136
+
137
+ for(var param in data){
138
+ if (data.hasOwnProperty(param)) {
139
+ marker.push(param + ':' + data[param]);
140
+ }
141
+ }
142
+
143
+ if (marker.length || i === 0) {
144
+ marker.push(loc);
145
+ marker = marker.join('|');
146
+ parameters.push('markers=' + encodeURI(marker));
147
+ }
148
+ // New marker without styles
149
+ else {
150
+ marker = parameters.pop() + encodeURI('|' + loc);
151
+ parameters.push(marker);
152
+ }
153
+ }
154
+ }
155
+
156
+ /** Map Styles **/
157
+ if (styles) {
158
+ for (var i = 0; i < styles.length; i++) {
159
+ var styleRule = [];
160
+ if (styles[i].featureType && styles[i].featureType != 'all' ) {
161
+ styleRule.push('feature:' + styles[i].featureType);
162
+ }
163
+
164
+ if (styles[i].elementType && styles[i].elementType != 'all') {
165
+ styleRule.push('element:' + styles[i].elementType);
166
+ }
167
+
168
+ for (var j = 0; j < styles[i].stylers.length; j++) {
169
+ for (var p in styles[i].stylers[j]) {
170
+ var ruleArg = styles[i].stylers[j][p];
171
+ if (p == 'hue' || p == 'color') {
172
+ ruleArg = '0x' + ruleArg.substring(1);
173
+ }
174
+ styleRule.push(p + ':' + ruleArg);
175
+ }
176
+ }
177
+
178
+ var rule = styleRule.join('|');
179
+ if (rule != '') {
180
+ parameters.push('style=' + rule);
181
+ }
182
+ }
183
+ }
184
+
185
+ /** Polylines **/
186
+ function parseColor(color, opacity) {
187
+ if (color[0] === '#'){
188
+ color = color.replace('#', '0x');
189
+
190
+ if (opacity) {
191
+ opacity = parseFloat(opacity);
192
+ opacity = Math.min(1, Math.max(opacity, 0));
193
+ if (opacity === 0) {
194
+ return '0x00000000';
195
+ }
196
+ opacity = (opacity * 255).toString(16);
197
+ if (opacity.length === 1) {
198
+ opacity += opacity;
199
+ }
200
+
201
+ color = color.slice(0,8) + opacity;
202
+ }
203
+ }
204
+ return color;
205
+ }
206
+
207
+ if (polyline) {
208
+ data = polyline;
209
+ polyline = [];
210
+
211
+ if (data.strokeWeight) {
212
+ polyline.push('weight:' + parseInt(data.strokeWeight, 10));
213
+ }
214
+
215
+ if (data.strokeColor) {
216
+ var color = parseColor(data.strokeColor, data.strokeOpacity);
217
+ polyline.push('color:' + color);
218
+ }
219
+
220
+ if (data.fillColor) {
221
+ var fillcolor = parseColor(data.fillColor, data.fillOpacity);
222
+ polyline.push('fillcolor:' + fillcolor);
223
+ }
224
+
225
+ var path = data.path;
226
+ if (path.join) {
227
+ for (var j=0, pos; pos=path[j]; j++) {
228
+ polyline.push(pos.join(','));
229
+ }
230
+ }
231
+ else {
232
+ polyline.push('enc:' + path);
233
+ }
234
+
235
+ polyline = polyline.join('|');
236
+ parameters.push('path=' + encodeURI(polyline));
237
+ }
238
+
239
+ /** Retina support **/
240
+ var dpi = window.devicePixelRatio || 1;
241
+ parameters.push('scale=' + dpi);
242
+
243
+ parameters = parameters.join('&');
244
+ return static_root + parameters;
245
+ };
@@ -0,0 +1,44 @@
1
+ GMaps.prototype.createPanorama = function(streetview_options) {
2
+ if (!streetview_options.hasOwnProperty('lat') || !streetview_options.hasOwnProperty('lng')) {
3
+ streetview_options.lat = this.getCenter().lat();
4
+ streetview_options.lng = this.getCenter().lng();
5
+ }
6
+
7
+ this.panorama = GMaps.createPanorama(streetview_options);
8
+
9
+ this.map.setStreetView(this.panorama);
10
+
11
+ return this.panorama;
12
+ };
13
+
14
+ GMaps.createPanorama = function(options) {
15
+ var el = getElementById(options.el, options.context);
16
+
17
+ options.position = new google.maps.LatLng(options.lat, options.lng);
18
+
19
+ delete options.el;
20
+ delete options.context;
21
+ delete options.lat;
22
+ delete options.lng;
23
+
24
+ var streetview_events = ['closeclick', 'links_changed', 'pano_changed', 'position_changed', 'pov_changed', 'resize', 'visible_changed'],
25
+ streetview_options = extend_object({visible : true}, options);
26
+
27
+ for (var i = 0; i < streetview_events.length; i++) {
28
+ delete streetview_options[streetview_events[i]];
29
+ }
30
+
31
+ var panorama = new google.maps.StreetViewPanorama(el, streetview_options);
32
+
33
+ for (var i = 0; i < streetview_events.length; i++) {
34
+ (function(object, name) {
35
+ if (options[name]) {
36
+ google.maps.event.addListener(object, name, function(){
37
+ options[name].apply(this);
38
+ });
39
+ }
40
+ })(panorama, streetview_events[i]);
41
+ }
42
+
43
+ return panorama;
44
+ };
@@ -0,0 +1,9 @@
1
+ GMaps.prototype.addStyle = function(options) {
2
+ var styledMapType = new google.maps.StyledMapType(options.styles, { name: options.styledMapName });
3
+
4
+ this.map.mapTypes.set(options.mapTypeId, styledMapType);
5
+ };
6
+
7
+ GMaps.prototype.setStyle = function(mapTypeId) {
8
+ this.map.setMapTypeId(mapTypeId);
9
+ };
@@ -0,0 +1,42 @@
1
+ GMaps.geolocate = function(options) {
2
+ var complete_callback = options.always || options.complete;
3
+
4
+ if (navigator.geolocation) {
5
+ navigator.geolocation.getCurrentPosition(function(position) {
6
+ options.success(position);
7
+
8
+ if (complete_callback) {
9
+ complete_callback();
10
+ }
11
+ }, function(error) {
12
+ options.error(error);
13
+
14
+ if (complete_callback) {
15
+ complete_callback();
16
+ }
17
+ }, options.options);
18
+ }
19
+ else {
20
+ options.not_supported();
21
+
22
+ if (complete_callback) {
23
+ complete_callback();
24
+ }
25
+ }
26
+ };
27
+
28
+ GMaps.geocode = function(options) {
29
+ this.geocoder = new google.maps.Geocoder();
30
+ var callback = options.callback;
31
+ if (options.hasOwnProperty('lat') && options.hasOwnProperty('lng')) {
32
+ options.latLng = new google.maps.LatLng(options.lat, options.lng);
33
+ }
34
+
35
+ delete options.lat;
36
+ delete options.lng;
37
+ delete options.callback;
38
+
39
+ this.geocoder.geocode(options, function(results, status) {
40
+ callback(results, status);
41
+ });
42
+ };
@@ -0,0 +1,6 @@
1
+ module GmapsJS
2
+ module Rails
3
+ class Engine < ::Rails::Engine
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module GmapsJS
2
+ module Rails
3
+ VERSION = "0.2.30.1"
4
+ end
5
+ end
@@ -0,0 +1,2 @@
1
+ require "gmapsjs-rails/version"
2
+ require "gmapsjs-rails/engine" if defined?(::Rails)