gmapsjs 0.1.12 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,13 +1,35 @@
1
- var GMaps = (function($) {
1
+ /*!
2
+ * GMaps.js
3
+ * http://hpneo.github.com/gmaps/
4
+ *
5
+ * Copyright 2012, Gustavo Leon
6
+ * Released under the MIT License.
7
+ */
8
+
9
+ var GMaps = (function(global) {
2
10
  "use strict";
3
11
 
12
+ var doc = document;
13
+ var getElementById = function(id, context) {
14
+ var ele
15
+ if('jQuery' in global && context){
16
+ ele = $("#"+id.replace('#', ''), context)[0]
17
+ }else{
18
+ ele = doc.getElementById(id.replace('#', ''));
19
+ };
20
+ return ele;
21
+ };
22
+
4
23
  var GMaps = function(options) {
5
24
  var self = this;
6
- var context_menu_style = 'position:absolute;display:none;min-width:100px;' +
7
- 'background:white;list-style:none;padding:8px;box-shadow:2px 2px 6px #ccc';
8
25
  window.context_menu = {};
9
26
 
10
- this.div = $(options.div)[0];
27
+ if(typeof(options.div)=='string'){
28
+ this.div = getElementById(options.div, options.context);
29
+ }else{this.div = options.div;};
30
+ this.div.style.width = this.div.clientWidth || options.width;
31
+ this.div.style.height = this.div.clientHeight || options.height;
32
+
11
33
  this.controls = [];
12
34
  this.overlays = [];
13
35
  this.layers = [];
@@ -31,17 +53,46 @@ var GMaps = (function($) {
31
53
 
32
54
  var map_center = new google.maps.LatLng(options.lat, options.lng);
33
55
 
56
+ delete options.div;
34
57
  delete options.lat;
35
58
  delete options.lng;
36
59
  delete options.mapType;
60
+ delete options.width;
61
+ delete options.height;
62
+
63
+ var zoomControlOpt = options.zoomControlOpt || {
64
+ style: 'DEFAULT',
65
+ position: 'TOP_LEFT'
66
+ };
67
+
68
+ var zoomControl = options.zoomControl || true,
69
+ zoomControlStyle = zoomControlOpt.style || 'DEFAULT',
70
+ zoomControlPosition = zoomControlOpt.position || 'TOP_LEFT',
71
+ panControl = options.panControl || true,
72
+ mapTypeControl = options.mapTypeControl || true,
73
+ scaleControl = options.scaleControl || true,
74
+ streetViewControl = options.streetViewControl || true,
75
+ overviewMapControl = overviewMapControl || true;
76
+
77
+
37
78
 
38
79
  var map_base_options = {
39
80
  zoom: this.zoom,
40
81
  center: map_center,
41
- mapTypeId: mapType
82
+ mapTypeId: mapType,
83
+ panControl: panControl,
84
+ zoomControl: zoomControl,
85
+ zoomControlOptions: {
86
+ style: google.maps.ZoomControlStyle[zoomControlStyle], // DEFAULT LARGE SMALL
87
+ position: google.maps.ControlPosition[zoomControlPosition]
88
+ },
89
+ mapTypeControl: mapTypeControl,
90
+ scaleControl: scaleControl,
91
+ streetViewControl: streetViewControl,
92
+ overviewMapControl: overviewMapControl
42
93
  };
43
94
 
44
- var map_options = $.extend(map_base_options, options);
95
+ var map_options = extend_object(map_base_options, options);
45
96
 
46
97
  this.map = new google.maps.Map(this.div, map_options);
47
98
 
@@ -52,25 +103,39 @@ var GMaps = (function($) {
52
103
  for (var i in options){
53
104
  if (options.hasOwnProperty(i)){
54
105
  var option = options[i];
55
- html += '<li><a id="' + control + '_' + i + '" href="#">' +
106
+ html += '<li><a id="' + control + '_' + i + '" href="#">' +
56
107
  option.title + '</a></li>';
57
108
  }
58
109
  }
59
- var $context_menu = $('#gmaps_context_menu');
60
- $context_menu.html(html);
61
- $context_menu.undelegate();
62
- $context_menu.delegate('li a', 'click', function(ev) {
63
- ev.preventDefault();
64
- options[$(this).attr('id').replace(control + '_', '')].action.call(self, e);
65
- self.hideContextMenu();
66
- });
110
+ if(!getElementById('gmaps_context_menu')) return;
111
+ var context_menu_element = getElementById('gmaps_context_menu');
112
+ context_menu_element.innerHTML = html;
113
+
114
+ var context_menu_items = context_menu_element.getElementsByTagName('a');
115
+
116
+ var context_menu_items_count = context_menu_items.length;
67
117
 
68
- var left = $(self.div).offset().left + e.pixel.x - 15;
69
- var top = $(self.div).offset().top + e.pixel.y - 15;
70
- $context_menu.css({
71
- left: left,
72
- top: top
73
- }).fadeIn(200);
118
+ for(var i=0;i<context_menu_items_count;i++){
119
+ var context_menu_item = context_menu_items[i];
120
+
121
+ var assign_menu_item_action = function(ev){
122
+ ev.preventDefault();
123
+
124
+ options[this.id.replace(control + '_', '')].action.call(self, e);
125
+ self.hideContextMenu();
126
+ };
127
+
128
+ google.maps.event.clearListeners(context_menu_item, 'click');
129
+ google.maps.event.addDomListenerOnce(context_menu_item, 'click', assign_menu_item_action, false);
130
+ }
131
+
132
+ var left = self.div.offsetLeft + e.pixel.x - 15;
133
+ var top = self.div.offsetTop + e.pixel.y - 15;
134
+
135
+ context_menu_element.style.left = left + "px";
136
+ context_menu_element.style.top = top + "px";
137
+
138
+ context_menu_element.style.display = 'block';
74
139
  };
75
140
 
76
141
  var buildContextMenu = function(control, e) {
@@ -93,7 +158,6 @@ var GMaps = (function($) {
93
158
 
94
159
  this.setContextMenu = function(options) {
95
160
  window.context_menu[options.control] = {};
96
- var html = '<ul id="gmaps_context_menu" style="' + context_menu_style + '"></ul>';
97
161
  for (var i in options.options){
98
162
  if (options.options.hasOwnProperty(i)){
99
163
  var option = options.options[i];
@@ -103,117 +167,93 @@ var GMaps = (function($) {
103
167
  };
104
168
  }
105
169
  }
106
- $('body').append(html);
107
- $('#gmaps_context_menu').mouseleave(function() {
108
- $(this).delay(100).fadeOut(200);
109
- });
170
+ var ul = doc.createElement('ul');
171
+ ul.id = 'gmaps_context_menu';
172
+ ul.style.display = 'none';
173
+ ul.style.position = 'absolute';
174
+ ul.style.minWidth = '100px';
175
+ ul.style.background = 'white';
176
+ ul.style.listStyle = 'none';
177
+ ul.style.padding = '8px';
178
+ ul.style.boxShadow = '2px 2px 6px #ccc';
179
+
180
+ doc.body.appendChild(ul);
181
+
182
+ var context_menu_element = getElementById('gmaps_context_menu');
183
+
184
+ google.maps.event.addDomListener(context_menu_element, 'mouseout', function(ev) {
185
+ if(!ev.relatedTarget || !this.contains(ev.relatedTarget)){
186
+ window.setTimeout(function(){
187
+ context_menu_element.style.display = 'none';
188
+ }, 400);
189
+ }
190
+ }, false);
110
191
  };
111
192
 
112
193
  this.hideContextMenu = function() {
113
- $('#gmaps_context_menu').fadeOut(200);
194
+ var context_menu_element = getElementById('gmaps_context_menu');
195
+ if(context_menu_element)
196
+ context_menu_element.style.display = 'none';
114
197
  };
115
198
 
116
199
  //Events
117
- google.maps.event.addListener(this.map, 'bounds_changed', function(e) {
118
- if (options.bounds_changed) {
119
- options.bounds_changed(e);
120
- }
121
- self.hideContextMenu();
122
- });
123
- google.maps.event.addListener(this.map, 'center_changed', function(e) {
124
- if (options.center_changed) {
125
- options.center_changed(e);
126
- }
127
- self.hideContextMenu();
128
- });
129
- google.maps.event.addListener(this.map, 'click', function(e) {
130
- if (options.click) {
131
- options.click(e);
132
- }
133
- self.hideContextMenu();
134
- });
135
- google.maps.event.addListener(this.map, 'dblclick', function(e) {
136
- if (options.dblclick) {
137
- options.dblclick(e);
138
- }
139
- self.hideContextMenu();
140
- });
141
- google.maps.event.addListener(this.map, 'drag', function(e) {
142
- if (options.drag) {
143
- options.drag(e);
144
- }
145
- self.hideContextMenu();
146
- });
147
- google.maps.event.addListener(this.map, 'dragend', function(e) {
148
- if (options.dragend) {
149
- options.dragend(e);
150
- }
151
- self.hideContextMenu();
152
- });
153
- google.maps.event.addListener(this.map, 'dragstart', function(e) {
154
- if (options.dragstart) {
155
- options.dragstart(e);
156
- }
157
- self.hideContextMenu();
158
- });
159
- google.maps.event.addListener(this.map, 'idle', function(e) {
160
- if (options.idle) {
161
- options.idle(e);
162
- }
163
- self.hideContextMenu();
164
- });
165
- google.maps.event.addListener(this.map, 'maptypeid_changed', function(e) {
166
- if (options.maptypeid_changed) {
167
- options.maptypeid_changed(e);
168
- }
169
- self.hideContextMenu();
170
- });
171
- google.maps.event.addListener(this.map, 'mousemove', function(e) {
172
- if (options.mousemove) {
173
- options.mousemove(e);
174
- }
175
- });
176
- google.maps.event.addListener(this.map, 'mouseout', function(e) {
177
- if (options.mouseout) {
178
- options.mouseout(e);
179
- }
180
- });
181
- google.maps.event.addListener(this.map, 'mouseover', function(e) {
182
- if (options.mouseover) {
183
- options.mouseover(e);
184
- }
185
- });
186
- google.maps.event.addListener(this.map, 'projection_changed', function(e) {
187
- if (options.projection_changed) {
188
- options.projection_changed(e);
189
- }
190
- self.hideContextMenu();
191
- });
192
- google.maps.event.addListener(this.map, 'resize', function(e) {
193
- if (options.resize) {
194
- options.resize(e);
195
- }
196
- self.hideContextMenu();
197
- });
200
+
201
+ var events_that_hide_context_menu = ['bounds_changed', 'center_changed', 'click', 'dblclick', 'drag', 'dragend', 'dragstart', 'idle', 'maptypeid_changed', 'projection_changed', 'resize', 'tilesloaded', 'zoom_changed'];
202
+ var events_that_doesnt_hide_context_menu = ['mousemove', 'mouseout', 'mouseover'];
203
+
204
+ for (var ev = 0; ev < events_that_hide_context_menu.length; ev++) {
205
+ (function(object, name) {
206
+ google.maps.event.addListener(object, name, function(e){
207
+ if (options[name])
208
+ options[name].apply(this, [e]);
209
+
210
+ self.hideContextMenu();
211
+ });
212
+ })(this.map, events_that_hide_context_menu[ev]);
213
+ }
214
+
215
+ for (var ev = 0; ev < events_that_doesnt_hide_context_menu.length; ev++) {
216
+ (function(object, name) {
217
+ google.maps.event.addListener(object, name, function(e){
218
+ if (options[name])
219
+ options[name].apply(this, [e]);
220
+ });
221
+ })(this.map, events_that_doesnt_hide_context_menu[ev]);
222
+ }
223
+
198
224
  google.maps.event.addListener(this.map, 'rightclick', function(e) {
199
225
  if (options.rightclick) {
200
- options.rightclick(e);
226
+ options.rightclick.apply(this, [e]);
201
227
  }
202
228
 
203
229
  buildContextMenu('map', e);
204
230
  });
205
- google.maps.event.addListener(this.map, 'tilesloaded', function(e) {
206
- if (options.tilesloaded) {
207
- options.tilesloaded(e);
231
+
232
+ this.refresh = function() {
233
+ google.maps.event.trigger(this.map, 'resize');
234
+ };
235
+
236
+ this.fitZoom = function() {
237
+ var latLngs = [];
238
+ var markers_length = this.markers.length;
239
+
240
+ for(var i=0; i < markers_length; i++) {
241
+ latLngs.push(this.markers[i].getPosition());
208
242
  }
209
- self.hideContextMenu();
210
- });
211
- google.maps.event.addListener(this.map, 'zoom_changed', function(e) {
212
- if (options.zoom_changed) {
213
- options.zoom_changed(e);
243
+
244
+ this.fitBounds(latLngs);
245
+ };
246
+
247
+ this.fitBounds = function(latLngs) {
248
+ var total = latLngs.length;
249
+ var bounds = new google.maps.LatLngBounds();
250
+
251
+ for(var i=0; i < total; i++) {
252
+ bounds.extend(latLngs[i]);
214
253
  }
215
- self.hideContextMenu();
216
- });
254
+
255
+ this.map.fitBounds(bounds);
256
+ };
217
257
 
218
258
  // Map methods
219
259
  this.setCenter = function(lat, lng, callback) {
@@ -235,6 +275,10 @@ var GMaps = (function($) {
235
275
  this.map.setZoom(value);
236
276
  };
237
277
 
278
+ this.getZoom = function() {
279
+ return this.map.getZoom();
280
+ };
281
+
238
282
  this.zoomIn = function(value) {
239
283
  this.map.setZoom(this.map.getZoom() + value);
240
284
  };
@@ -244,21 +288,25 @@ var GMaps = (function($) {
244
288
  };
245
289
 
246
290
  this.createControl = function(options) {
247
- options.style.cursor = 'pointer';
248
- options.style.fontFamily = 'Arial, sans-serif';
249
- options.style.fontSize = '13px';
250
- options.style.boxShadow = 'rgba(0, 0, 0, 0.398438) 0px 2px 4px';
291
+ var control = doc.createElement('div');
251
292
 
252
- var controlDiv = $('<div></div>');
253
- controlDiv.css(options.style);
254
- controlDiv.text(options.text);
293
+ control.style.cursor = 'pointer';
294
+ control.style.fontFamily = 'Arial, sans-serif';
295
+ control.style.fontSize = '13px';
296
+ control.style.boxShadow = 'rgba(0, 0, 0, 0.398438) 0px 2px 4px';
255
297
 
256
- var control = controlDiv[0];
298
+ for(var option in options.style){
299
+ control.style[option] = options.style[option];
300
+ }
257
301
 
258
- for(var e in options.events) {
259
- google.maps.event.addDomListener(control, e, function() {
260
- options.events[e].apply(this, [this]);
261
- });
302
+ control.textContent = options.text;
303
+
304
+ for (var ev in options.events) {
305
+ (function(object, name) {
306
+ google.maps.event.addDomListener(object, name, function(){
307
+ options.events[name].apply(this, [this]);
308
+ });
309
+ })(control, ev);
262
310
  }
263
311
 
264
312
  control.index = 1;
@@ -285,16 +333,18 @@ var GMaps = (function($) {
285
333
  var details = options.details;
286
334
  var fences = options.fences;
287
335
  var outside = options.outside;
336
+
288
337
  var base_options = {
289
338
  position: new google.maps.LatLng(options.lat, options.lng),
290
339
  map: null
291
340
  };
341
+
292
342
  delete options.lat;
293
343
  delete options.lng;
294
344
  delete options.fences;
295
345
  delete options.outside;
296
346
 
297
- var marker_options = $.extend(base_options, options);
347
+ var marker_options = extend_object(base_options, options);
298
348
 
299
349
  var marker = new google.maps.Marker(marker_options);
300
350
 
@@ -303,31 +353,27 @@ var GMaps = (function($) {
303
353
  if (options.infoWindow) {
304
354
  marker.infoWindow = new google.maps.InfoWindow(options.infoWindow);
305
355
 
306
- if (options.infoWindow.closeclick) {
307
- google.maps.event.addListener(marker.infoWindow, 'closeclick', function() {
308
- options.infoWindow.closeclick();
309
- });
310
- }
311
- if (options.infoWindow.content_changed) {
312
- google.maps.event.addListener(marker.infoWindow, 'content_changed', function() {
313
- options.infoWindow.content_changed();
314
- });
315
- }
316
- if (options.infoWindow.domready) {
317
- google.maps.event.addListener(marker.infoWindow, 'domready', function() {
318
- options.infoWindow.domready();
319
- });
320
- }
321
- if (options.infoWindow.position_changed) {
322
- google.maps.event.addListener(marker.infoWindow, 'position_changed', function() {
323
- options.infoWindow.position_changed();
324
- });
356
+ var info_window_events = ['closeclick', 'content_changed', 'domready', 'position_changed', 'zindex_changed'];
357
+
358
+ for (var ev = 0; ev < info_window_events.length; ev++) {
359
+ (function(object, name) {
360
+ google.maps.event.addListener(object, name, function(e){
361
+ if (options.infoWindow[name])
362
+ options.infoWindow[name].apply(this, [e]);
363
+ });
364
+ })(marker.infoWindow, info_window_events[ev]);
325
365
  }
326
- if (options.infoWindow.zindex_changed) {
327
- google.maps.event.addListener(marker.infoWindow, 'zindex_changed', function() {
328
- options.infoWindow.zindex_changed();
366
+ }
367
+
368
+ var marker_events = ['drag', 'dragstart', 'mouseout', 'mouseover', 'mouseup', 'position_changed'];
369
+
370
+ for (var ev = 0; ev < marker_events.length; ev++) {
371
+ (function(object, name) {
372
+ google.maps.event.addListener(object, name, function(){
373
+ if (options[name])
374
+ options[name].apply(this, [this]);
329
375
  });
330
- }
376
+ })(marker, marker_events[ev]);
331
377
  }
332
378
 
333
379
  google.maps.event.addListener(marker, 'click', function() {
@@ -343,15 +389,10 @@ var GMaps = (function($) {
343
389
  }
344
390
  });
345
391
 
346
- if (options.drag) {
347
- google.maps.event.addListener(marker, 'drag', function() {
348
- options.drag(this);
349
- });
350
- }
351
392
  if (options.dragend || marker.fences) {
352
393
  google.maps.event.addListener(marker, 'dragend', function() {
353
394
  if (options.dragend) {
354
- options.dragend(this);
395
+ options.dragend.apply(this, [this]);
355
396
  }
356
397
  if (marker.fences) {
357
398
  self.checkMarkerGeofence(marker, function(m, f) {
@@ -360,31 +401,6 @@ var GMaps = (function($) {
360
401
  }
361
402
  });
362
403
  }
363
- if (options.dragstart) {
364
- google.maps.event.addListener(marker, 'dragstart', function() {
365
- options.dragstart(this);
366
- });
367
- }
368
- if (options.mouseout) {
369
- google.maps.event.addListener(marker, 'mouseout', function() {
370
- options.mouseout(this);
371
- });
372
- }
373
- if (options.mouseover) {
374
- google.maps.event.addListener(marker, 'mouseover', function() {
375
- options.mouseover(this);
376
- });
377
- }
378
- if (options.mouseup) {
379
- google.maps.event.addListener(marker, 'mouseup', function() {
380
- options.mouseup(this);
381
- });
382
- }
383
- if (options.position_changed) {
384
- google.maps.event.addListener(marker, 'position_changed', function() {
385
- options.position_changed(this);
386
- });
387
- }
388
404
 
389
405
  return marker;
390
406
  }
@@ -412,7 +428,7 @@ var GMaps = (function($) {
412
428
  }
413
429
  return this.markers;
414
430
  };
415
-
431
+
416
432
  this.hideInfoWindows = function() {
417
433
  for (var i=0, marker; marker=this.markers[i]; i++){
418
434
  if (marker.infoWindow){
@@ -420,7 +436,7 @@ var GMaps = (function($) {
420
436
  }
421
437
  }
422
438
  };
423
-
439
+
424
440
  this.removeMarkers = function() {
425
441
  for (var i=0, marker; marker=this.markers[i]; i++){
426
442
  marker.setMap(null);
@@ -434,10 +450,11 @@ var GMaps = (function($) {
434
450
  overlay.setMap(self.map);
435
451
 
436
452
  overlay.onAdd = function() {
437
- var div = document.createElement('div');
453
+ var div = doc.createElement('div');
438
454
  div.style.borderStyle = "none";
439
455
  div.style.borderWidth = "0px";
440
456
  div.style.position = "absolute";
457
+ div.style.zIndex = 100;
441
458
  div.innerHTML = options.content;
442
459
 
443
460
  self.overlay_div = div;
@@ -458,32 +475,35 @@ var GMaps = (function($) {
458
475
  options.verticalOffset = options.verticalOffset || 0;
459
476
 
460
477
  var div = self.overlay_div;
461
- var content = div.children;
478
+ var content = div.children[0];
479
+
480
+ var content_height = content.clientHeight;
481
+ var content_width = content.clientWidth;
462
482
 
463
483
  switch (options.verticalAlign) {
464
- case 'top':
465
- div.style.top = (pixel.y - $(content).height() + options.verticalOffset) + 'px';
466
- break;
467
- default:
468
- case 'middle':
469
- div.style.top = (pixel.y - ($(content).height() / 2) + options.verticalOffset) + 'px';
470
- break;
471
- case 'bottom':
472
- div.style.top = (pixel.y + options.verticalOffset) + 'px';
473
- break;
484
+ case 'top':
485
+ div.style.top = (pixel.y - content_height + options.verticalOffset) + 'px';
486
+ break;
487
+ default:
488
+ case 'middle':
489
+ div.style.top = (pixel.y - (content_height / 2) + options.verticalOffset) + 'px';
490
+ break;
491
+ case 'bottom':
492
+ div.style.top = (pixel.y + options.verticalOffset) + 'px';
493
+ break;
474
494
  }
475
495
 
476
496
  switch (options.horizontalAlign) {
477
- case 'left':
478
- div.style.left = (pixel.x - $(content).width() + options.horizontalOffset) + 'px';
479
- break;
480
- default:
481
- case 'center':
482
- div.style.left = (pixel.x - ($(content).width() / 2) + options.horizontalOffset) + 'px';
483
- break;
484
- case 'right':
485
- div.style.left = (pixel.x + options.horizontalOffset) + 'px';
486
- break;
497
+ case 'left':
498
+ div.style.left = (pixel.x - content_width + options.horizontalOffset) + 'px';
499
+ break;
500
+ default:
501
+ case 'center':
502
+ div.style.left = (pixel.x - (content_width / 2) + options.horizontalOffset) + 'px';
503
+ break;
504
+ case 'right':
505
+ div.style.left = (pixel.x + options.horizontalOffset) + 'px';
506
+ break;
487
507
  }
488
508
  };
489
509
 
@@ -529,45 +549,15 @@ var GMaps = (function($) {
529
549
  strokeWeight: options.strokeWeight
530
550
  });
531
551
 
532
- if (options.click) {
533
- google.maps.event.addListener(polyline, 'click', function(e) {
534
- options.click(e);
535
- });
536
- }
537
- if (options.dblclick) {
538
- google.maps.event.addListener(polyline, 'dblclick', function(e) {
539
- options.dblclick(e);
540
- });
541
- }
542
- if (options.mousedown) {
543
- google.maps.event.addListener(polyline, 'mousedown', function(e) {
544
- options.mousedown(e);
545
- });
546
- }
547
- if (options.mousemove) {
548
- google.maps.event.addListener(polyline, 'mousemove', function(e) {
549
- options.mousemove(e);
550
- });
551
- }
552
- if (options.mouseout) {
553
- google.maps.event.addListener(polyline, 'mouseout', function(e) {
554
- options.mouseout(e);
555
- });
556
- }
557
- if (options.mouseover) {
558
- google.maps.event.addListener(polyline, 'mouseover', function(e) {
559
- options.mouseover(e);
560
- });
561
- }
562
- if (options.mouseup) {
563
- google.maps.event.addListener(polyline, 'mouseup', function(e) {
564
- options.mouseup(e);
565
- });
566
- }
567
- if (options.rightclick) {
568
- google.maps.event.addListener(polyline, 'rightclick', function(e) {
569
- options.rightclick(e);
570
- });
552
+ var polyline_events = ['click', 'dblclick', 'mousedown', 'mousemove', 'mouseout', 'mouseover', 'mouseup', 'rightclick'];
553
+
554
+ for (var ev = 0; ev < polyline_events.length; ev++) {
555
+ (function(object, name) {
556
+ google.maps.event.addListener(object, name, function(e){
557
+ if (options[name])
558
+ options[name].apply(this, [e]);
559
+ });
560
+ })(polyline, polyline_events[ev]);
571
561
  }
572
562
 
573
563
  this.polylines.push(polyline);
@@ -576,7 +566,7 @@ var GMaps = (function($) {
576
566
  };
577
567
 
578
568
  this.drawCircle = function(options) {
579
- options = $.extend({
569
+ options = extend_object({
580
570
  map: this.map,
581
571
  center: new google.maps.LatLng(options.lat, options.lng)
582
572
  }, options);
@@ -585,100 +575,51 @@ var GMaps = (function($) {
585
575
  delete options.lng;
586
576
  var polygon = new google.maps.Circle(options);
587
577
 
588
- google.maps.event.addListener(polygon, 'click', function(e) {
589
- if (options.click) {
590
- options.click(e);
591
- }
592
- });
593
- google.maps.event.addListener(polygon, 'dblclick', function(e) {
594
- if (options.dblclick) {
595
- options.dblclick(e);
596
- }
597
- });
598
- google.maps.event.addListener(polygon, 'mousedown', function(e) {
599
- if (options.mousedown) {
600
- options.mousedown(e);
601
- }
602
- });
603
- google.maps.event.addListener(polygon, 'mousemove', function(e) {
604
- if (options.mousemove) {
605
- options.mousemove(e);
606
- }
607
- });
608
- google.maps.event.addListener(polygon, 'mouseout', function(e) {
609
- if (options.mouseout) {
610
- options.mouseout(e);
611
- }
612
- });
613
- google.maps.event.addListener(polygon, 'mouseover', function(e) {
614
- if (options.mouseover) {
615
- options.mouseover(e);
616
- }
617
- });
618
- google.maps.event.addListener(polygon, 'mouseup', function(e) {
619
- if (options.mouseup) {
620
- options.mouseup(e);
621
- }
622
- });
623
- google.maps.event.addListener(polygon, 'rightclick', function(e) {
624
- if (options.rightclick) {
625
- options.rightclick(e);
626
- }
627
- });
578
+ var polygon_events = ['click', 'dblclick', 'mousedown', 'mousemove', 'mouseout', 'mouseover', 'mouseup', 'rightclick'];
579
+
580
+ for (var ev = 0; ev < polygon_events.length; ev++) {
581
+ (function(object, name) {
582
+ google.maps.event.addListener(object, name, function(e){
583
+ if (options[name])
584
+ options[name].apply(this, [e]);
585
+ });
586
+ })(polygon, polygon_events[ev]);
587
+ }
628
588
 
629
589
  return polygon;
630
590
  };
631
591
 
632
592
  this.drawPolygon = function(options) {
633
- options = $.extend({
593
+ options = extend_object({
634
594
  map: this.map
635
595
  }, options);
596
+
597
+ if(options.paths.length > 0) {
598
+ if(options.paths[0].length > 0) {
599
+ options.paths = array_map(options.paths, arrayToLatLng);
600
+ }
601
+ }
602
+
636
603
  var polygon = new google.maps.Polygon(options);
637
604
 
638
- google.maps.event.addListener(polygon, 'click', function(e) {
639
- if (options.click) {
640
- options.click(e);
641
- }
642
- });
643
- google.maps.event.addListener(polygon, 'dblclick', function(e) {
644
- if (options.dblclick) {
645
- options.dblclick(e);
646
- }
647
- });
648
- google.maps.event.addListener(polygon, 'mousedown', function(e) {
649
- if (options.mousedown) {
650
- options.mousedown(e);
651
- }
652
- });
653
- google.maps.event.addListener(polygon, 'mousemove', function(e) {
654
- if (options.mousemove) {
655
- options.mousemove(e);
656
- }
657
- });
658
- google.maps.event.addListener(polygon, 'mouseout', function(e) {
659
- if (options.mouseout) {
660
- options.mouseout(e);
661
- }
662
- });
663
- google.maps.event.addListener(polygon, 'mouseover', function(e) {
664
- if (options.mouseover) {
665
- options.mouseover(e);
666
- }
667
- });
668
- google.maps.event.addListener(polygon, 'mouseup', function(e) {
669
- if (options.mouseup) {
670
- options.mouseup(e);
671
- }
672
- });
673
- google.maps.event.addListener(polygon, 'rightclick', function(e) {
674
- if (options.rightclick) {
675
- options.rightclick(e);
676
- }
677
- });
605
+ var polygon_events = ['click', 'dblclick', 'mousedown', 'mousemove', 'mouseout', 'mouseover', 'mouseup', 'rightclick'];
606
+
607
+ for (var ev = 0; ev < polygon_events.length; ev++) {
608
+ (function(object, name) {
609
+ google.maps.event.addListener(object, name, function(e){
610
+ if (options[name])
611
+ options[name].apply(this, [e]);
612
+ });
613
+ })(polygon, polygon_events[ev]);
614
+ }
678
615
 
679
616
  this.polygons.push(polygon);
680
617
 
681
618
  return polygon;
619
+
620
+ function arrayToLatLng(coords) {
621
+ return new google.maps.LatLng(coords[0], coords[1]);
622
+ }
682
623
  };
683
624
 
684
625
  this.getFromFusionTables = function(options) {
@@ -687,14 +628,15 @@ var GMaps = (function($) {
687
628
  delete options.events;
688
629
 
689
630
  var fusion_tables_options = options;
690
- fusion_tables_options.map = this.map;
691
631
 
692
632
  var layer = new google.maps.FusionTablesLayer(fusion_tables_options);
693
633
 
694
- for (var e in events) {
695
- google.maps.event.addListener(layer, e, function(ev){
696
- events[e].apply(this, [ev]);
697
- });
634
+ for (var ev in events) {
635
+ (function(object, name) {
636
+ google.maps.event.addListener(object, name, function(e){
637
+ events[name].apply(this, [e]);
638
+ });
639
+ })(layer, ev);
698
640
  }
699
641
 
700
642
  this.layers.push(layer);
@@ -702,22 +644,30 @@ var GMaps = (function($) {
702
644
  return layer;
703
645
  };
704
646
 
647
+ this.loadFromFusionTables = function(options) {
648
+ var layer = this.getFromFusionTables(options);
649
+ layer.setMap(this.map);
650
+
651
+ return layer;
652
+ };
653
+
705
654
  this.getFromKML = function(options) {
706
655
  var url = options.url;
707
656
  var events = options.events;
708
657
 
709
658
  delete options.url;
710
659
  delete options.events;
711
-
660
+
712
661
  var kml_options = options;
713
- kml_options.map = this.map;
714
662
 
715
663
  var layer = new google.maps.KmlLayer(url, kml_options);
716
664
 
717
- for (var e in events) {
718
- google.maps.event.addListener(layer, e, function(ev){
719
- events[e].apply(this, [ev]);
720
- });
665
+ for (var ev in events) {
666
+ (function(object, name) {
667
+ google.maps.event.addListener(object, name, function(e){
668
+ events[name].apply(this, [e]);
669
+ });
670
+ })(layer, ev);
721
671
  }
722
672
 
723
673
  this.layers.push(layer);
@@ -725,6 +675,13 @@ var GMaps = (function($) {
725
675
  return layer;
726
676
  };
727
677
 
678
+ this.loadFromKML = function(options) {
679
+ var layer = this.getFromKML(options);
680
+ layer.setMap(this.map);
681
+
682
+ return layer;
683
+ };
684
+
728
685
  // Services
729
686
  var travelMode, unitSystem;
730
687
  this.getRoutes = function(options) {
@@ -749,12 +706,14 @@ var GMaps = (function($) {
749
706
  }
750
707
 
751
708
  var base_options = {
752
- avoidHighways: true,
753
- avoidTolls: true,
754
- optimizeWaypoints: true,
709
+ avoidHighways: false,
710
+ avoidTolls: false,
711
+ optimizeWaypoints: false,
755
712
  waypoints: []
756
713
  };
757
- var request_options = $.extend(base_options, options);
714
+
715
+ var request_options = extend_object(base_options, options);
716
+
758
717
  request_options.origin = new google.maps.LatLng(options.origin[0], options.origin[1]);
759
718
  request_options.destination = new google.maps.LatLng(options.destination[0], options.destination[1]);
760
719
  request_options.travelMode = travelMode;
@@ -779,12 +738,24 @@ var GMaps = (function($) {
779
738
  });
780
739
  };
781
740
 
741
+ this.removePolylines = function(){
742
+ var index;
743
+ for(index in this.polylines){
744
+ this.polylines[index].setMap(null);
745
+ }
746
+ this.polylines = [];
747
+ }
748
+
749
+ // Alias for the method "drawRoute"
750
+ this.cleanRoute = this.removePolylines;
751
+
782
752
  this.drawRoute = function(options) {
783
753
  var self = this;
784
754
  this.getRoutes({
785
755
  origin: options.origin,
786
756
  destination: options.destination,
787
757
  travelMode: options.travelMode,
758
+ waypoints : options.waypoints,
788
759
  callback: function(e) {
789
760
  if (e.length > 0) {
790
761
  self.drawPolyline({
@@ -807,6 +778,7 @@ var GMaps = (function($) {
807
778
  origin: options.origin,
808
779
  destination: options.destination,
809
780
  travelMode: options.travelMode,
781
+ waypoints : options.waypoints,
810
782
  callback: function(e) {
811
783
  if (e.length > 0 && options.step) {
812
784
  var route = e[e.length - 1];
@@ -882,7 +854,7 @@ var GMaps = (function($) {
882
854
 
883
855
  this.checkMarkerGeofence = function(marker, outside_callback) {
884
856
  if (marker.fences) {
885
- for (var i=0, fence; fence=marker.fences[i]; i++) {
857
+ for (var i=0, fence; fence=marker.fences[i]; i++) {
886
858
  var pos = marker.getPosition();
887
859
  if (!self.checkGeofence(pos.lat(), pos.lng(), fence)) {
888
860
  outside_callback(marker, fence);
@@ -1000,7 +972,7 @@ var GMaps = (function($) {
1000
972
  else if (options.address){
1001
973
  parameters.push('center=' + options.address);
1002
974
  delete options.address;
1003
- }
975
+ }
1004
976
  else if (options.lat){
1005
977
  parameters.push(['center=', options.lat, ',', options.lng].join(''));
1006
978
  delete options.lat;
@@ -1091,7 +1063,7 @@ var GMaps = (function($) {
1091
1063
  }
1092
1064
 
1093
1065
  color = color.slice(0,8) + opacity;
1094
- }
1066
+ }
1095
1067
  }
1096
1068
  return color;
1097
1069
  }
@@ -1099,7 +1071,7 @@ var GMaps = (function($) {
1099
1071
  if (polyline){
1100
1072
  data = polyline;
1101
1073
  polyline = [];
1102
-
1074
+
1103
1075
  if (data.strokeWeight){
1104
1076
  polyline.push('weight:' + parseInt(data.strokeWeight, 10));
1105
1077
  }
@@ -1113,7 +1085,7 @@ var GMaps = (function($) {
1113
1085
  var fillcolor = parseColor(data.fillColor, data.fillOpacity);
1114
1086
  polyline.push('fillcolor:' + fillcolor);
1115
1087
  }
1116
-
1088
+
1117
1089
  var path = data.path;
1118
1090
  if (path.join){
1119
1091
  for (var j=0, pos; pos=path[j]; j++){
@@ -1121,7 +1093,7 @@ var GMaps = (function($) {
1121
1093
  }
1122
1094
  }
1123
1095
  else {
1124
- polyline.push('enc:' + path);
1096
+ polyline.push('enc:' + path);
1125
1097
  }
1126
1098
 
1127
1099
  polyline = polyline.join('|');
@@ -1202,4 +1174,30 @@ var GMaps = (function($) {
1202
1174
  };
1203
1175
 
1204
1176
  return GMaps;
1205
- }(jQuery));
1177
+ }(this));
1178
+
1179
+ var extend_object = function(obj, new_obj) {
1180
+ if(obj === new_obj) return obj;
1181
+
1182
+ for(var name in new_obj){
1183
+ obj[name] = new_obj[name];
1184
+ }
1185
+
1186
+ return obj;
1187
+ };
1188
+
1189
+ var array_map = function(array, callback) {
1190
+ if (Array.prototype.map && array.map === Array.prototype.map) {
1191
+ return array.map(callback);
1192
+ } else {
1193
+ var array_return = [];
1194
+
1195
+ var array_length = array.length;
1196
+
1197
+ for(var i = 0; i < array_length; i++) {
1198
+ array_return.push(callback(array[i]));
1199
+ }
1200
+
1201
+ return array_return;
1202
+ }
1203
+ }
@@ -1,3 +1,3 @@
1
1
  module GmapsJS
2
- VERSION = "0.1.12"
2
+ VERSION = "0.2.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gmapsjs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.12
4
+ version: 0.2.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-05-31 00:00:00.000000000Z
13
+ date: 2012-07-18 00:00:00.000000000Z
14
14
  dependencies: []
15
15
  description: ! 'gmaps.js allows you to use the potential of Google Maps in a simple
16
16
  way.