Blame view

frontend/web/js/gmaps.js 55.8 KB
ae432de6   Alexey Boroda   first commit
1
  (function(root, factory) {
9870b2b4   Alexey Boroda   -In process
2
3
4
5
6
7
    if(typeof exports === 'object') {
      module.exports = factory();
    }
    else if(typeof define === 'function' && define.amd) {
      define('GMaps', [], factory);
    }
ae432de6   Alexey Boroda   first commit
8
  
9870b2b4   Alexey Boroda   -In process
9
    root.GMaps = factory();
ae432de6   Alexey Boroda   first commit
10
  
9870b2b4   Alexey Boroda   -In process
11
  }(this, function() {
ae432de6   Alexey Boroda   first commit
12
  
9870b2b4   Alexey Boroda   -In process
13
14
15
16
17
18
19
  /*!
   * GMaps.js v0.4.15
   * http://hpneo.github.com/gmaps/
   *
   * Copyright 2014, Gustavo Leon
   * Released under the MIT License.
   */
ae432de6   Alexey Boroda   first commit
20
  
9870b2b4   Alexey Boroda   -In process
21
22
23
  if (!(typeof window.google === 'object' && window.google.maps)) {
    throw 'Google Maps API is required. Please register the following JavaScript library http://maps.google.com/maps/api/js?sensor=true.'
  }
ae432de6   Alexey Boroda   first commit
24
  
9870b2b4   Alexey Boroda   -In process
25
26
  var extend_object = function(obj, new_obj) {
    var name;
ae432de6   Alexey Boroda   first commit
27
  
9870b2b4   Alexey Boroda   -In process
28
29
30
    if (obj === new_obj) {
      return obj;
    }
ae432de6   Alexey Boroda   first commit
31
  
9870b2b4   Alexey Boroda   -In process
32
33
34
    for (name in new_obj) {
      obj[name] = new_obj[name];
    }
ae432de6   Alexey Boroda   first commit
35
  
9870b2b4   Alexey Boroda   -In process
36
37
    return obj;
  };
ae432de6   Alexey Boroda   first commit
38
  
9870b2b4   Alexey Boroda   -In process
39
40
  var replace_object = function(obj, replace) {
    var name;
ae432de6   Alexey Boroda   first commit
41
  
9870b2b4   Alexey Boroda   -In process
42
43
44
    if (obj === replace) {
      return obj;
    }
ae432de6   Alexey Boroda   first commit
45
  
9870b2b4   Alexey Boroda   -In process
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
    for (name in replace) {
      if (obj[name] != undefined) {
        obj[name] = replace[name];
      }
    }
  
    return obj;
  };
  
  var array_map = function(array, callback) {
    var original_callback_params = Array.prototype.slice.call(arguments, 2),
        array_return = [],
        array_length = array.length,
        i;
  
    if (Array.prototype.map && array.map === Array.prototype.map) {
      array_return = Array.prototype.map.call(array, function(item) {
        callback_params = original_callback_params;
        callback_params.splice(0, 0, item);
  
        return callback.apply(this, callback_params);
      });
    }
    else {
      for (i = 0; i < array_length; i++) {
        callback_params = original_callback_params;
        callback_params.splice(0, 0, array[i]);
        array_return.push(callback.apply(this, callback_params));
      }
    }
ae432de6   Alexey Boroda   first commit
76
  
9870b2b4   Alexey Boroda   -In process
77
78
    return array_return;
  };
ae432de6   Alexey Boroda   first commit
79
  
9870b2b4   Alexey Boroda   -In process
80
81
82
  var array_flat = function(array) {
    var new_array = [],
        i;
ae432de6   Alexey Boroda   first commit
83
  
9870b2b4   Alexey Boroda   -In process
84
85
86
    for (i = 0; i < array.length; i++) {
      new_array = new_array.concat(array[i]);
    }
ae432de6   Alexey Boroda   first commit
87
  
9870b2b4   Alexey Boroda   -In process
88
89
    return new_array;
  };
ae432de6   Alexey Boroda   first commit
90
  
9870b2b4   Alexey Boroda   -In process
91
92
93
  var coordsToLatLngs = function(coords, useGeoJSON) {
    var first_coord = coords[0],
        second_coord = coords[1];
ae432de6   Alexey Boroda   first commit
94
  
9870b2b4   Alexey Boroda   -In process
95
96
97
98
    if (useGeoJSON) {
      first_coord = coords[1];
      second_coord = coords[0];
    }
ae432de6   Alexey Boroda   first commit
99
  
9870b2b4   Alexey Boroda   -In process
100
101
    return new google.maps.LatLng(first_coord, second_coord);
  };
ae432de6   Alexey Boroda   first commit
102
  
9870b2b4   Alexey Boroda   -In process
103
104
  var arrayToLatLng = function(coords, useGeoJSON) {
    var i;
ae432de6   Alexey Boroda   first commit
105
  
9870b2b4   Alexey Boroda   -In process
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
    for (i = 0; i < coords.length; i++) {
      if (!(coords[i] instanceof google.maps.LatLng)) {
        if (coords[i].length > 0 && typeof(coords[i][0]) == "object") {
          coords[i] = arrayToLatLng(coords[i], useGeoJSON);
        }
        else {
          coords[i] = coordsToLatLngs(coords[i], useGeoJSON);
        }
      }
    }
  
    return coords;
  };
  
  var getElementById = function(id, context) {
    var element,
    id = id.replace('#', '');
  
    if ('jQuery' in this && context) {
      element = $("#" + id, context)[0];
    } else {
      element = document.getElementById(id);
    };
  
    return element;
  };
  
  var findAbsolutePosition = function(obj)  {
    var curleft = 0,
        curtop = 0;
  
    if (obj.offsetParent) {
      do {
        curleft += obj.offsetLeft;
        curtop += obj.offsetTop;
      } while (obj = obj.offsetParent);
    }
  
    return [curleft, curtop];
  };
  
  var GMaps = (function(global) {
    "use strict";
  
    var doc = document;
  
    var GMaps = function(options) {
      if (!this) return new GMaps(options);
  
      options.zoom = options.zoom || 15;
      options.mapType = options.mapType || 'roadmap';
  
      var self = this,
          i,
          events_that_hide_context_menu = ['bounds_changed', 'center_changed', 'click', 'dblclick', 'drag', 'dragend', 'dragstart', 'idle', 'maptypeid_changed', 'projection_changed', 'resize', 'tilesloaded', 'zoom_changed'],
          events_that_doesnt_hide_context_menu = ['mousemove', 'mouseout', 'mouseover'],
          options_to_be_deleted = ['el', 'lat', 'lng', 'mapType', 'width', 'height', 'markerClusterer', 'enableNewStyle'],
          container_id = options.el || options.div,
          markerClustererFunction = options.markerClusterer,
          mapType = google.maps.MapTypeId[options.mapType.toUpperCase()],
          map_center = new google.maps.LatLng(options.lat, options.lng),
          zoomControl = options.zoomControl || true,
          zoomControlOpt = options.zoomControlOpt || {
            style: 'DEFAULT',
            position: 'TOP_LEFT'
          },
          zoomControlStyle = zoomControlOpt.style || 'DEFAULT',
          zoomControlPosition = zoomControlOpt.position || 'TOP_LEFT',
          panControl = options.panControl || true,
          mapTypeControl = options.mapTypeControl || true,
          scaleControl = options.scaleControl || true,
          streetViewControl = options.streetViewControl || true,
          overviewMapControl = overviewMapControl || true,
          map_options = {},
          map_base_options = {
            zoom: this.zoom,
            center: map_center,
            mapTypeId: mapType
          },
          map_controls_options = {
            panControl: panControl,
            zoomControl: zoomControl,
            zoomControlOptions: {
              style: google.maps.ZoomControlStyle[zoomControlStyle],
              position: google.maps.ControlPosition[zoomControlPosition]
            },
            mapTypeControl: mapTypeControl,
            scaleControl: scaleControl,
            streetViewControl: streetViewControl,
            overviewMapControl: overviewMapControl
          };
  
      if (typeof(options.el) === 'string' || typeof(options.div) === 'string') {
        this.el = getElementById(container_id, options.context);
      } else {
        this.el = container_id;
      }
ae432de6   Alexey Boroda   first commit
203
  
9870b2b4   Alexey Boroda   -In process
204
205
206
      if (typeof(this.el) === 'undefined' || this.el === null) {
        throw 'No element defined.';
      }
ae432de6   Alexey Boroda   first commit
207
  
9870b2b4   Alexey Boroda   -In process
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
      window.context_menu = window.context_menu || {};
      window.context_menu[self.el.id] = {};
  
      this.controls = [];
      this.overlays = [];
      this.layers = []; // array with kml/georss and fusiontables layers, can be as many
      this.singleLayers = {}; // object with the other layers, only one per layer
      this.markers = [];
      this.polylines = [];
      this.routes = [];
      this.polygons = [];
      this.infoWindow = null;
      this.overlay_el = null;
      this.zoom = options.zoom;
      this.registered_events = {};
  
      this.el.style.width = options.width || this.el.scrollWidth || this.el.offsetWidth;
      this.el.style.height = options.height || this.el.scrollHeight || this.el.offsetHeight;
  
      google.maps.visualRefresh = options.enableNewStyle;
  
      for (i = 0; i < options_to_be_deleted.length; i++) {
        delete options[options_to_be_deleted[i]];
      }
ae432de6   Alexey Boroda   first commit
232
  
9870b2b4   Alexey Boroda   -In process
233
234
235
      if(options.disableDefaultUI != true) {
        map_base_options = extend_object(map_base_options, map_controls_options);
      }
ae432de6   Alexey Boroda   first commit
236
  
9870b2b4   Alexey Boroda   -In process
237
      map_options = extend_object(map_base_options, options);
ae432de6   Alexey Boroda   first commit
238
  
9870b2b4   Alexey Boroda   -In process
239
240
241
      for (i = 0; i < events_that_hide_context_menu.length; i++) {
        delete map_options[events_that_hide_context_menu[i]];
      }
ae432de6   Alexey Boroda   first commit
242
  
9870b2b4   Alexey Boroda   -In process
243
244
245
      for (i = 0; i < events_that_doesnt_hide_context_menu.length; i++) {
        delete map_options[events_that_doesnt_hide_context_menu[i]];
      }
ae432de6   Alexey Boroda   first commit
246
  
9870b2b4   Alexey Boroda   -In process
247
      this.map = new google.maps.Map(this.el, map_options);
ae432de6   Alexey Boroda   first commit
248
  
9870b2b4   Alexey Boroda   -In process
249
250
251
      if (markerClustererFunction) {
        this.markerClusterer = markerClustererFunction.apply(this, [this.map]);
      }
ae432de6   Alexey Boroda   first commit
252
  
9870b2b4   Alexey Boroda   -In process
253
254
255
      var buildContextMenuHTML = function(control, e) {
        var html = '',
            options = window.context_menu[self.el.id][control];
ae432de6   Alexey Boroda   first commit
256
  
9870b2b4   Alexey Boroda   -In process
257
258
259
        for (var i in options){
          if (options.hasOwnProperty(i)) {
            var option = options[i];
ae432de6   Alexey Boroda   first commit
260
  
9870b2b4   Alexey Boroda   -In process
261
262
263
            html += '<li><a id="' + control + '_' + i + '" href="#">' + option.title + '</a></li>';
          }
        }
ae432de6   Alexey Boroda   first commit
264
  
9870b2b4   Alexey Boroda   -In process
265
        if (!getElementById('gmaps_context_menu')) return;
ae432de6   Alexey Boroda   first commit
266
  
9870b2b4   Alexey Boroda   -In process
267
268
269
        var context_menu_element = getElementById('gmaps_context_menu');
        
        context_menu_element.innerHTML = html;
ae432de6   Alexey Boroda   first commit
270
  
9870b2b4   Alexey Boroda   -In process
271
272
273
        var context_menu_items = context_menu_element.getElementsByTagName('a'),
            context_menu_items_count = context_menu_items.length,
            i;
ae432de6   Alexey Boroda   first commit
274
  
9870b2b4   Alexey Boroda   -In process
275
276
        for (i = 0; i < context_menu_items_count; i++) {
          var context_menu_item = context_menu_items[i];
ae432de6   Alexey Boroda   first commit
277
  
9870b2b4   Alexey Boroda   -In process
278
279
          var assign_menu_item_action = function(ev){
            ev.preventDefault();
ae432de6   Alexey Boroda   first commit
280
  
9870b2b4   Alexey Boroda   -In process
281
282
            options[this.id.replace(control + '_', '')].action.apply(self, [e]);
            self.hideContextMenu();
ae432de6   Alexey Boroda   first commit
283
284
          };
  
9870b2b4   Alexey Boroda   -In process
285
286
287
          google.maps.event.clearListeners(context_menu_item, 'click');
          google.maps.event.addDomListenerOnce(context_menu_item, 'click', assign_menu_item_action, false);
        }
ae432de6   Alexey Boroda   first commit
288
  
9870b2b4   Alexey Boroda   -In process
289
290
291
        var position = findAbsolutePosition.apply(this, [self.el]),
            left = position[0] + e.pixel.x - 15,
            top = position[1] + e.pixel.y- 15;
ae432de6   Alexey Boroda   first commit
292
  
9870b2b4   Alexey Boroda   -In process
293
294
        context_menu_element.style.left = left + "px";
        context_menu_element.style.top = top + "px";
ae432de6   Alexey Boroda   first commit
295
  
9870b2b4   Alexey Boroda   -In process
296
297
        context_menu_element.style.display = 'block';
      };
ae432de6   Alexey Boroda   first commit
298
  
9870b2b4   Alexey Boroda   -In process
299
300
301
      this.buildContextMenu = function(control, e) {
        if (control === 'marker') {
          e.pixel = {};
ae432de6   Alexey Boroda   first commit
302
  
9870b2b4   Alexey Boroda   -In process
303
304
305
306
307
308
309
310
          var overlay = new google.maps.OverlayView();
          overlay.setMap(self.map);
          
          overlay.draw = function() {
            var projection = overlay.getProjection(),
                position = e.marker.getPosition();
            
            e.pixel = projection.fromLatLngToContainerPixel(position);
ae432de6   Alexey Boroda   first commit
311
  
9870b2b4   Alexey Boroda   -In process
312
            buildContextMenuHTML(control, e);
ae432de6   Alexey Boroda   first commit
313
          };
9870b2b4   Alexey Boroda   -In process
314
315
316
317
318
        }
        else {
          buildContextMenuHTML(control, e);
        }
      };
ae432de6   Alexey Boroda   first commit
319
  
9870b2b4   Alexey Boroda   -In process
320
321
      this.setContextMenu = function(options) {
        window.context_menu[self.el.id][options.control] = {};
ae432de6   Alexey Boroda   first commit
322
  
9870b2b4   Alexey Boroda   -In process
323
324
        var i,
            ul = doc.createElement('ul');
ae432de6   Alexey Boroda   first commit
325
  
9870b2b4   Alexey Boroda   -In process
326
327
328
        for (i in options.options) {
          if (options.options.hasOwnProperty(i)) {
            var option = options.options[i];
ae432de6   Alexey Boroda   first commit
329
  
9870b2b4   Alexey Boroda   -In process
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
            window.context_menu[self.el.id][options.control][option.name] = {
              title: option.title,
              action: option.action
            };
          }
        }
  
        ul.id = 'gmaps_context_menu';
        ul.style.display = 'none';
        ul.style.position = 'absolute';
        ul.style.minWidth = '100px';
        ul.style.background = 'white';
        ul.style.listStyle = 'none';
        ul.style.padding = '8px';
        ul.style.boxShadow = '2px 2px 6px #ccc';
  
        doc.body.appendChild(ul);
  
        var context_menu_element = getElementById('gmaps_context_menu')
  
        google.maps.event.addDomListener(context_menu_element, 'mouseout', function(ev) {
          if (!ev.relatedTarget || !this.contains(ev.relatedTarget)) {
            window.setTimeout(function(){
              context_menu_element.style.display = 'none';
            }, 400);
          }
        }, false);
      };
ae432de6   Alexey Boroda   first commit
358
  
9870b2b4   Alexey Boroda   -In process
359
360
      this.hideContextMenu = function() {
        var context_menu_element = getElementById('gmaps_context_menu');
ae432de6   Alexey Boroda   first commit
361
  
9870b2b4   Alexey Boroda   -In process
362
363
364
365
        if (context_menu_element) {
          context_menu_element.style.display = 'none';
        }
      };
ae432de6   Alexey Boroda   first commit
366
  
9870b2b4   Alexey Boroda   -In process
367
368
369
370
371
      var setupListener = function(object, name) {
        google.maps.event.addListener(object, name, function(e){
          if (e == undefined) {
            e = this;
          }
ae432de6   Alexey Boroda   first commit
372
  
9870b2b4   Alexey Boroda   -In process
373
          options[name].apply(this, [e]);
ae432de6   Alexey Boroda   first commit
374
  
9870b2b4   Alexey Boroda   -In process
375
376
377
          self.hideContextMenu();
        });
      };
ae432de6   Alexey Boroda   first commit
378
  
9870b2b4   Alexey Boroda   -In process
379
380
      //google.maps.event.addListener(this.map, 'idle', this.hideContextMenu);
      google.maps.event.addListener(this.map, 'zoom_changed', this.hideContextMenu);
ae432de6   Alexey Boroda   first commit
381
  
9870b2b4   Alexey Boroda   -In process
382
383
      for (var ev = 0; ev < events_that_hide_context_menu.length; ev++) {
        var name = events_that_hide_context_menu[ev];
ae432de6   Alexey Boroda   first commit
384
  
9870b2b4   Alexey Boroda   -In process
385
386
387
388
        if (name in options) {
          setupListener(this.map, name);
        }
      }
ae432de6   Alexey Boroda   first commit
389
  
9870b2b4   Alexey Boroda   -In process
390
391
      for (var ev = 0; ev < events_that_doesnt_hide_context_menu.length; ev++) {
        var name = events_that_doesnt_hide_context_menu[ev];
ae432de6   Alexey Boroda   first commit
392
  
9870b2b4   Alexey Boroda   -In process
393
394
395
396
        if (name in options) {
          setupListener(this.map, name);
        }
      }
ae432de6   Alexey Boroda   first commit
397
  
9870b2b4   Alexey Boroda   -In process
398
399
400
401
      google.maps.event.addListener(this.map, 'rightclick', function(e) {
        if (options.rightclick) {
          options.rightclick.apply(this, [e]);
        }
ae432de6   Alexey Boroda   first commit
402
  
9870b2b4   Alexey Boroda   -In process
403
404
405
406
        if(window.context_menu[self.el.id]['map'] != undefined) {
          self.buildContextMenu('map', e);
        }
      });
ae432de6   Alexey Boroda   first commit
407
  
9870b2b4   Alexey Boroda   -In process
408
409
410
      this.refresh = function() {
        google.maps.event.trigger(this.map, 'resize');
      };
ae432de6   Alexey Boroda   first commit
411
  
9870b2b4   Alexey Boroda   -In process
412
413
414
415
      this.fitZoom = function() {
        var latLngs = [],
            markers_length = this.markers.length,
            i;
ae432de6   Alexey Boroda   first commit
416
  
9870b2b4   Alexey Boroda   -In process
417
418
419
420
421
        for (i = 0; i < markers_length; i++) {
          if(typeof(this.markers[i].visible) === 'boolean' && this.markers[i].visible) {
            latLngs.push(this.markers[i].getPosition());
          }
        }
ae432de6   Alexey Boroda   first commit
422
  
9870b2b4   Alexey Boroda   -In process
423
424
        this.fitLatLngBounds(latLngs);
      };
ae432de6   Alexey Boroda   first commit
425
  
9870b2b4   Alexey Boroda   -In process
426
427
428
      this.fitLatLngBounds = function(latLngs) {
        var total = latLngs.length;
        var bounds = new google.maps.LatLngBounds();
ae432de6   Alexey Boroda   first commit
429
  
9870b2b4   Alexey Boroda   -In process
430
431
432
        for(var i=0; i < total; i++) {
          bounds.extend(latLngs[i]);
        }
ae432de6   Alexey Boroda   first commit
433
  
9870b2b4   Alexey Boroda   -In process
434
435
        this.map.fitBounds(bounds);
      };
ae432de6   Alexey Boroda   first commit
436
  
9870b2b4   Alexey Boroda   -In process
437
438
      this.setCenter = function(lat, lng, callback) {
        this.map.panTo(new google.maps.LatLng(lat, lng));
ae432de6   Alexey Boroda   first commit
439
  
9870b2b4   Alexey Boroda   -In process
440
441
442
443
        if (callback) {
          callback();
        }
      };
ae432de6   Alexey Boroda   first commit
444
  
9870b2b4   Alexey Boroda   -In process
445
446
447
      this.getElement = function() {
        return this.el;
      };
ae432de6   Alexey Boroda   first commit
448
  
9870b2b4   Alexey Boroda   -In process
449
450
      this.zoomIn = function(value) {
        value = value || 1;
ae432de6   Alexey Boroda   first commit
451
  
9870b2b4   Alexey Boroda   -In process
452
453
454
        this.zoom = this.map.getZoom() + value;
        this.map.setZoom(this.zoom);
      };
ae432de6   Alexey Boroda   first commit
455
  
9870b2b4   Alexey Boroda   -In process
456
457
      this.zoomOut = function(value) {
        value = value || 1;
ae432de6   Alexey Boroda   first commit
458
  
9870b2b4   Alexey Boroda   -In process
459
460
461
        this.zoom = this.map.getZoom() - value;
        this.map.setZoom(this.zoom);
      };
ae432de6   Alexey Boroda   first commit
462
  
9870b2b4   Alexey Boroda   -In process
463
464
      var native_methods = [],
          method;
ae432de6   Alexey Boroda   first commit
465
  
9870b2b4   Alexey Boroda   -In process
466
467
468
469
470
      for (method in this.map) {
        if (typeof(this.map[method]) == 'function' && !this[method]) {
          native_methods.push(method);
        }
      }
ae432de6   Alexey Boroda   first commit
471
  
9870b2b4   Alexey Boroda   -In process
472
473
474
475
      for (i=0; i < native_methods.length; i++) {
        (function(gmaps, scope, method_name) {
          gmaps[method_name] = function(){
            return scope[method_name].apply(scope, arguments);
ae432de6   Alexey Boroda   first commit
476
          };
9870b2b4   Alexey Boroda   -In process
477
478
479
        })(this, this.map, native_methods[i]);
      }
    };
ae432de6   Alexey Boroda   first commit
480
  
9870b2b4   Alexey Boroda   -In process
481
482
    return GMaps;
  })(this);
ae432de6   Alexey Boroda   first commit
483
  
9870b2b4   Alexey Boroda   -In process
484
485
  GMaps.prototype.createControl = function(options) {
    var control = document.createElement('div');
ae432de6   Alexey Boroda   first commit
486
  
9870b2b4   Alexey Boroda   -In process
487
488
489
490
491
492
493
    control.style.cursor = 'pointer';
    
    if (options.disableDefaultStyles !== true) {
      control.style.fontFamily = 'Roboto, Arial, sans-serif';
      control.style.fontSize = '11px';
      control.style.boxShadow = 'rgba(0, 0, 0, 0.298039) 0px 1px 4px -1px';
    }
ae432de6   Alexey Boroda   first commit
494
  
9870b2b4   Alexey Boroda   -In process
495
496
497
    for (var option in options.style) {
      control.style[option] = options.style[option];
    }
ae432de6   Alexey Boroda   first commit
498
  
9870b2b4   Alexey Boroda   -In process
499
500
501
    if (options.id) {
      control.id = options.id;
    }
ae432de6   Alexey Boroda   first commit
502
  
9870b2b4   Alexey Boroda   -In process
503
504
505
    if (options.classes) {
      control.className = options.classes;
    }
ae432de6   Alexey Boroda   first commit
506
  
9870b2b4   Alexey Boroda   -In process
507
508
509
510
511
512
513
514
    if (options.content) {
      if (typeof options.content === 'string') {
        control.innerHTML = options.content;
      }
      else if (options.content instanceof HTMLElement) {
        control.appendChild(options.content);
      }
    }
ae432de6   Alexey Boroda   first commit
515
  
9870b2b4   Alexey Boroda   -In process
516
517
518
    if (options.position) {
      control.position = google.maps.ControlPosition[options.position.toUpperCase()];
    }
ae432de6   Alexey Boroda   first commit
519
  
9870b2b4   Alexey Boroda   -In process
520
521
522
523
524
525
526
    for (var ev in options.events) {
      (function(object, name) {
        google.maps.event.addDomListener(object, name, function(){
          options.events[name].apply(this, [this]);
        });
      })(control, ev);
    }
ae432de6   Alexey Boroda   first commit
527
  
9870b2b4   Alexey Boroda   -In process
528
    control.index = 1;
ae432de6   Alexey Boroda   first commit
529
  
9870b2b4   Alexey Boroda   -In process
530
531
    return control;
  };
ae432de6   Alexey Boroda   first commit
532
  
9870b2b4   Alexey Boroda   -In process
533
534
535
536
  GMaps.prototype.addControl = function(options) {
    var control = this.createControl(options);
    this.controls.push(control);
    this.map.controls[control.position].push(control);
ae432de6   Alexey Boroda   first commit
537
  
9870b2b4   Alexey Boroda   -In process
538
539
    return control;
  };
ae432de6   Alexey Boroda   first commit
540
  
9870b2b4   Alexey Boroda   -In process
541
542
  GMaps.prototype.removeControl = function(control) {
    var position = null;
ae432de6   Alexey Boroda   first commit
543
  
9870b2b4   Alexey Boroda   -In process
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
    for (var i = 0; i < this.controls.length; i++) {
      if (this.controls[i] == control) {
        position = this.controls[i].position;
        this.controls.splice(i, 1);
      }
    }
  
    if (position) {
      for (i = 0; i < this.map.controls.length; i++) {
        var controlsForPosition = this.map.controls[control.position]
        if (controlsForPosition.getAt(i) == control) {
          controlsForPosition.removeAt(i);
          break;
        }
      }
    }
  
    return control;
  };
  
  GMaps.prototype.createMarker = function(options) {
    if (options.lat == undefined && options.lng == undefined && options.position == undefined) {
      throw 'No latitude or longitude defined.';
    }
  
    var self = this,
        details = options.details,
        fences = options.fences,
        outside = options.outside,
        base_options = {
          position: new google.maps.LatLng(options.lat, options.lng),
          map: null
        },
        marker_options = extend_object(base_options, options);
  
    delete marker_options.lat;
    delete marker_options.lng;
    delete marker_options.fences;
    delete marker_options.outside;
  
    var marker = new google.maps.Marker(marker_options);
  
    marker.fences = fences;
  
    if (options.infoWindow) {
      marker.infoWindow = new google.maps.InfoWindow(options.infoWindow);
  
      var info_window_events = ['closeclick', 'content_changed', 'domready', 'position_changed', 'zindex_changed'];
  
      for (var ev = 0; ev < info_window_events.length; ev++) {
        (function(object, name) {
          if (options.infoWindow[name]) {
            google.maps.event.addListener(object, name, function(e){
              options.infoWindow[name].apply(this, [e]);
            });
          }
        })(marker.infoWindow, info_window_events[ev]);
      }
    }
  
    var marker_events = ['animation_changed', 'clickable_changed', 'cursor_changed', 'draggable_changed', 'flat_changed', 'icon_changed', 'position_changed', 'shadow_changed', 'shape_changed', 'title_changed', 'visible_changed', 'zindex_changed'];
  
    var marker_events_with_mouse = ['dblclick', 'drag', 'dragend', 'dragstart', 'mousedown', 'mouseout', 'mouseover', 'mouseup'];
  
    for (var ev = 0; ev < marker_events.length; ev++) {
      (function(object, name) {
        if (options[name]) {
          google.maps.event.addListener(object, name, function(){
            options[name].apply(this, [this]);
          });
        }
      })(marker, marker_events[ev]);
    }
  
    for (var ev = 0; ev < marker_events_with_mouse.length; ev++) {
      (function(map, object, name) {
        if (options[name]) {
          google.maps.event.addListener(object, name, function(me){
            if(!me.pixel){
              me.pixel = map.getProjection().fromLatLngToPoint(me.latLng)
            }
            
            options[name].apply(this, [me]);
          });
        }
      })(this.map, marker, marker_events_with_mouse[ev]);
    }
  
    google.maps.event.addListener(marker, 'click', function() {
      this.details = details;
  
      if (options.click) {
        options.click.apply(this, [this]);
      }
ae432de6   Alexey Boroda   first commit
638
  
9870b2b4   Alexey Boroda   -In process
639
640
641
642
643
      if (marker.infoWindow) {
        self.hideInfoWindows();
        marker.infoWindow.open(self.map, marker);
      }
    });
ae432de6   Alexey Boroda   first commit
644
  
9870b2b4   Alexey Boroda   -In process
645
646
    google.maps.event.addListener(marker, 'rightclick', function(e) {
      e.marker = this;
ae432de6   Alexey Boroda   first commit
647
  
9870b2b4   Alexey Boroda   -In process
648
649
650
      if (options.rightclick) {
        options.rightclick.apply(this, [e]);
      }
ae432de6   Alexey Boroda   first commit
651
  
9870b2b4   Alexey Boroda   -In process
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
      if (window.context_menu[self.el.id]['marker'] != undefined) {
        self.buildContextMenu('marker', e);
      }
    });
  
    if (marker.fences) {
      google.maps.event.addListener(marker, 'dragend', function() {
        self.checkMarkerGeofence(marker, function(m, f) {
          outside(m, f);
        });
      });
    }
  
    return marker;
  };
  
  GMaps.prototype.addMarker = function(options) {
    var marker;
    if(options.hasOwnProperty('gm_accessors_')) {
      // Native google.maps.Marker object
      marker = options;
    }
    else {
      if ((options.hasOwnProperty('lat') && options.hasOwnProperty('lng')) || options.position) {
        marker = this.createMarker(options);
      }
      else {
        throw 'No latitude or longitude defined.';
      }
    }
ae432de6   Alexey Boroda   first commit
682
  
9870b2b4   Alexey Boroda   -In process
683
    marker.setMap(this.map);
ae432de6   Alexey Boroda   first commit
684
  
9870b2b4   Alexey Boroda   -In process
685
686
687
    if(this.markerClusterer) {
      this.markerClusterer.addMarker(marker);
    }
ae432de6   Alexey Boroda   first commit
688
  
9870b2b4   Alexey Boroda   -In process
689
    this.markers.push(marker);
ae432de6   Alexey Boroda   first commit
690
  
9870b2b4   Alexey Boroda   -In process
691
    GMaps.fire('marker_added', marker, this);
ae432de6   Alexey Boroda   first commit
692
  
9870b2b4   Alexey Boroda   -In process
693
694
    return marker;
  };
ae432de6   Alexey Boroda   first commit
695
  
9870b2b4   Alexey Boroda   -In process
696
697
698
699
  GMaps.prototype.addMarkers = function(array) {
    for (var i = 0, marker; marker=array[i]; i++) {
      this.addMarker(marker);
    }
ae432de6   Alexey Boroda   first commit
700
  
9870b2b4   Alexey Boroda   -In process
701
702
    return this.markers;
  };
ae432de6   Alexey Boroda   first commit
703
  
9870b2b4   Alexey Boroda   -In process
704
705
706
707
708
709
710
  GMaps.prototype.hideInfoWindows = function() {
    for (var i = 0, marker; marker = this.markers[i]; i++){
      if (marker.infoWindow) {
        marker.infoWindow.close();
      }
    }
  };
ae432de6   Alexey Boroda   first commit
711
  
9870b2b4   Alexey Boroda   -In process
712
713
714
715
716
  GMaps.prototype.removeMarker = function(marker) {
    for (var i = 0; i < this.markers.length; i++) {
      if (this.markers[i] === marker) {
        this.markers[i].setMap(null);
        this.markers.splice(i, 1);
ae432de6   Alexey Boroda   first commit
717
  
9870b2b4   Alexey Boroda   -In process
718
719
720
        if(this.markerClusterer) {
          this.markerClusterer.removeMarker(marker);
        }
ae432de6   Alexey Boroda   first commit
721
  
9870b2b4   Alexey Boroda   -In process
722
        GMaps.fire('marker_removed', marker, this);
ae432de6   Alexey Boroda   first commit
723
  
9870b2b4   Alexey Boroda   -In process
724
725
726
        break;
      }
    }
ae432de6   Alexey Boroda   first commit
727
  
9870b2b4   Alexey Boroda   -In process
728
729
    return marker;
  };
ae432de6   Alexey Boroda   first commit
730
  
9870b2b4   Alexey Boroda   -In process
731
732
  GMaps.prototype.removeMarkers = function (collection) {
    var new_markers = [];
ae432de6   Alexey Boroda   first commit
733
  
9870b2b4   Alexey Boroda   -In process
734
735
736
737
738
739
740
741
742
743
744
745
746
    if (typeof collection == 'undefined') {
      for (var i = 0; i < this.markers.length; i++) {
        this.markers[i].setMap(null);
      }
      
      this.markers = new_markers;
    }
    else {
      for (var i = 0; i < collection.length; i++) {
        if (this.markers.indexOf(collection[i]) > -1) {
          this.markers[i].setMap(null);
        }
      }
ae432de6   Alexey Boroda   first commit
747
  
9870b2b4   Alexey Boroda   -In process
748
749
750
751
752
      for (var i = 0; i < this.markers.length; i++) {
        if (this.markers[i].getMap() != null) {
          new_markers.push(this.markers[i]);
        }
      }
ae432de6   Alexey Boroda   first commit
753
  
9870b2b4   Alexey Boroda   -In process
754
755
756
      this.markers = new_markers;
    }
  };
ae432de6   Alexey Boroda   first commit
757
  
9870b2b4   Alexey Boroda   -In process
758
759
760
  GMaps.prototype.drawOverlay = function(options) {
    var overlay = new google.maps.OverlayView(),
        auto_show = true;
ae432de6   Alexey Boroda   first commit
761
  
9870b2b4   Alexey Boroda   -In process
762
    overlay.setMap(this.map);
ae432de6   Alexey Boroda   first commit
763
  
9870b2b4   Alexey Boroda   -In process
764
765
766
    if (options.auto_show != null) {
      auto_show = options.auto_show;
    }
ae432de6   Alexey Boroda   first commit
767
  
9870b2b4   Alexey Boroda   -In process
768
769
    overlay.onAdd = function() {
      var el = document.createElement('div');
ae432de6   Alexey Boroda   first commit
770
  
9870b2b4   Alexey Boroda   -In process
771
772
773
774
775
      el.style.borderStyle = "none";
      el.style.borderWidth = "0px";
      el.style.position = "absolute";
      el.style.zIndex = 100;
      el.innerHTML = options.content;
ae432de6   Alexey Boroda   first commit
776
  
9870b2b4   Alexey Boroda   -In process
777
      overlay.el = el;
ae432de6   Alexey Boroda   first commit
778
  
9870b2b4   Alexey Boroda   -In process
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
      if (!options.layer) {
        options.layer = 'overlayLayer';
      }
      
      var panes = this.getPanes(),
          overlayLayer = panes[options.layer],
          stop_overlay_events = ['contextmenu', 'DOMMouseScroll', 'dblclick', 'mousedown'];
  
      overlayLayer.appendChild(el);
  
      for (var ev = 0; ev < stop_overlay_events.length; ev++) {
        (function(object, name) {
          google.maps.event.addDomListener(object, name, function(e){
            if (navigator.userAgent.toLowerCase().indexOf('msie') != -1 && document.all) {
              e.cancelBubble = true;
              e.returnValue = false;
            }
            else {
              e.stopPropagation();
            }
          });
        })(el, stop_overlay_events[ev]);
      }
ae432de6   Alexey Boroda   first commit
802
  
9870b2b4   Alexey Boroda   -In process
803
804
805
806
807
808
      if (options.click) {
        panes.overlayMouseTarget.appendChild(overlay.el);
        google.maps.event.addDomListener(overlay.el, 'click', function() {
          options.click.apply(overlay, [overlay]);
        });
      }
ae432de6   Alexey Boroda   first commit
809
  
9870b2b4   Alexey Boroda   -In process
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
      google.maps.event.trigger(this, 'ready');
    };
  
    overlay.draw = function() {
      var projection = this.getProjection(),
          pixel = projection.fromLatLngToDivPixel(new google.maps.LatLng(options.lat, options.lng));
  
      options.horizontalOffset = options.horizontalOffset || 0;
      options.verticalOffset = options.verticalOffset || 0;
  
      var el = overlay.el,
          content = el.children[0],
          content_height = content.clientHeight,
          content_width = content.clientWidth;
  
      switch (options.verticalAlign) {
        case 'top':
          el.style.top = (pixel.y - content_height + options.verticalOffset) + 'px';
          break;
        default:
        case 'middle':
          el.style.top = (pixel.y - (content_height / 2) + options.verticalOffset) + 'px';
          break;
        case 'bottom':
          el.style.top = (pixel.y + options.verticalOffset) + 'px';
          break;
      }
ae432de6   Alexey Boroda   first commit
837
  
9870b2b4   Alexey Boroda   -In process
838
839
840
841
842
843
844
845
846
847
848
849
      switch (options.horizontalAlign) {
        case 'left':
          el.style.left = (pixel.x - content_width + options.horizontalOffset) + 'px';
          break;
        default:
        case 'center':
          el.style.left = (pixel.x - (content_width / 2) + options.horizontalOffset) + 'px';
          break;
        case 'right':
          el.style.left = (pixel.x + options.horizontalOffset) + 'px';
          break;
      }
ae432de6   Alexey Boroda   first commit
850
  
9870b2b4   Alexey Boroda   -In process
851
      el.style.display = auto_show ? 'block' : 'none';
ae432de6   Alexey Boroda   first commit
852
  
9870b2b4   Alexey Boroda   -In process
853
854
855
856
      if (!auto_show) {
        options.show.apply(this, [el]);
      }
    };
ae432de6   Alexey Boroda   first commit
857
  
9870b2b4   Alexey Boroda   -In process
858
859
    overlay.onRemove = function() {
      var el = overlay.el;
ae432de6   Alexey Boroda   first commit
860
  
9870b2b4   Alexey Boroda   -In process
861
862
863
864
865
866
867
868
      if (options.remove) {
        options.remove.apply(this, [el]);
      }
      else {
        overlay.el.parentNode.removeChild(overlay.el);
        overlay.el = null;
      }
    };
ae432de6   Alexey Boroda   first commit
869
  
9870b2b4   Alexey Boroda   -In process
870
871
872
    this.overlays.push(overlay);
    return overlay;
  };
ae432de6   Alexey Boroda   first commit
873
  
9870b2b4   Alexey Boroda   -In process
874
875
876
877
878
  GMaps.prototype.removeOverlay = function(overlay) {
    for (var i = 0; i < this.overlays.length; i++) {
      if (this.overlays[i] === overlay) {
        this.overlays[i].setMap(null);
        this.overlays.splice(i, 1);
ae432de6   Alexey Boroda   first commit
879
  
9870b2b4   Alexey Boroda   -In process
880
881
882
883
        break;
      }
    }
  };
ae432de6   Alexey Boroda   first commit
884
  
9870b2b4   Alexey Boroda   -In process
885
886
887
888
  GMaps.prototype.removeOverlays = function() {
    for (var i = 0, item; item = this.overlays[i]; i++) {
      item.setMap(null);
    }
ae432de6   Alexey Boroda   first commit
889
  
9870b2b4   Alexey Boroda   -In process
890
891
    this.overlays = [];
  };
ae432de6   Alexey Boroda   first commit
892
  
9870b2b4   Alexey Boroda   -In process
893
894
895
  GMaps.prototype.drawPolyline = function(options) {
    var path = [],
        points = options.path;
ae432de6   Alexey Boroda   first commit
896
  
9870b2b4   Alexey Boroda   -In process
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
    if (points.length) {
      if (points[0][0] === undefined) {
        path = points;
      }
      else {
        for (var i=0, latlng; latlng=points[i]; i++) {
          path.push(new google.maps.LatLng(latlng[0], latlng[1]));
        }
      }
    }
  
    var polyline_options = {
      map: this.map,
      path: path,
      strokeColor: options.strokeColor,
      strokeOpacity: options.strokeOpacity,
      strokeWeight: options.strokeWeight,
      geodesic: options.geodesic,
      clickable: true,
      editable: false,
      visible: true
    };
  
    if (options.hasOwnProperty("clickable")) {
      polyline_options.clickable = options.clickable;
    }
  
    if (options.hasOwnProperty("editable")) {
      polyline_options.editable = options.editable;
    }
  
    if (options.hasOwnProperty("icons")) {
      polyline_options.icons = options.icons;
    }
  
    if (options.hasOwnProperty("zIndex")) {
      polyline_options.zIndex = options.zIndex;
    }
  
    var polyline = new google.maps.Polyline(polyline_options);
  
    var polyline_events = ['click', 'dblclick', 'mousedown', 'mousemove', 'mouseout', 'mouseover', 'mouseup', 'rightclick'];
  
    for (var ev = 0; ev < polyline_events.length; ev++) {
      (function(object, name) {
        if (options[name]) {
          google.maps.event.addListener(object, name, function(e){
            options[name].apply(this, [e]);
          });
        }
      })(polyline, polyline_events[ev]);
    }
  
    this.polylines.push(polyline);
  
    GMaps.fire('polyline_added', polyline, this);
  
    return polyline;
  };
  
  GMaps.prototype.removePolyline = function(polyline) {
    for (var i = 0; i < this.polylines.length; i++) {
      if (this.polylines[i] === polyline) {
        this.polylines[i].setMap(null);
        this.polylines.splice(i, 1);
  
        GMaps.fire('polyline_removed', polyline, this);
  
        break;
      }
    }
  };
  
  GMaps.prototype.removePolylines = function() {
    for (var i = 0, item; item = this.polylines[i]; i++) {
      item.setMap(null);
    }
  
    this.polylines = [];
  };
  
  GMaps.prototype.drawCircle = function(options) {
    options =  extend_object({
      map: this.map,
      center: new google.maps.LatLng(options.lat, options.lng)
    }, options);
  
    delete options.lat;
    delete options.lng;
  
    var polygon = new google.maps.Circle(options),
        polygon_events = ['click', 'dblclick', 'mousedown', 'mousemove', 'mouseout', 'mouseover', 'mouseup', 'rightclick'];
  
    for (var ev = 0; ev < polygon_events.length; ev++) {
      (function(object, name) {
        if (options[name]) {
          google.maps.event.addListener(object, name, function(e){
            options[name].apply(this, [e]);
          });
        }
      })(polygon, polygon_events[ev]);
    }
  
    this.polygons.push(polygon);
  
    return polygon;
  };
  
  GMaps.prototype.drawRectangle = function(options) {
    options = extend_object({
      map: this.map
    }, options);
  
    var latLngBounds = new google.maps.LatLngBounds(
      new google.maps.LatLng(options.bounds[0][0], options.bounds[0][1]),
      new google.maps.LatLng(options.bounds[1][0], options.bounds[1][1])
    );
  
    options.bounds = latLngBounds;
  
    var polygon = new google.maps.Rectangle(options),
        polygon_events = ['click', 'dblclick', 'mousedown', 'mousemove', 'mouseout', 'mouseover', 'mouseup', 'rightclick'];
  
    for (var ev = 0; ev < polygon_events.length; ev++) {
      (function(object, name) {
        if (options[name]) {
          google.maps.event.addListener(object, name, function(e){
            options[name].apply(this, [e]);
          });
        }
      })(polygon, polygon_events[ev]);
    }
  
    this.polygons.push(polygon);
  
    return polygon;
  };
  
  GMaps.prototype.drawPolygon = function(options) {
    var useGeoJSON = false;
  
    if(options.hasOwnProperty("useGeoJSON")) {
      useGeoJSON = options.useGeoJSON;
    }
  
    delete options.useGeoJSON;
  
    options = extend_object({
      map: this.map
    }, options);
  
    if (useGeoJSON == false) {
      options.paths = [options.paths.slice(0)];
    }
  
    if (options.paths.length > 0) {
      if (options.paths[0].length > 0) {
        options.paths = array_flat(array_map(options.paths, arrayToLatLng, useGeoJSON));
      }
    }
ae432de6   Alexey Boroda   first commit
1057
  
9870b2b4   Alexey Boroda   -In process
1058
1059
    var polygon = new google.maps.Polygon(options),
        polygon_events = ['click', 'dblclick', 'mousedown', 'mousemove', 'mouseout', 'mouseover', 'mouseup', 'rightclick'];
ae432de6   Alexey Boroda   first commit
1060
  
9870b2b4   Alexey Boroda   -In process
1061
1062
1063
1064
1065
1066
1067
1068
1069
    for (var ev = 0; ev < polygon_events.length; ev++) {
      (function(object, name) {
        if (options[name]) {
          google.maps.event.addListener(object, name, function(e){
            options[name].apply(this, [e]);
          });
        }
      })(polygon, polygon_events[ev]);
    }
ae432de6   Alexey Boroda   first commit
1070
  
9870b2b4   Alexey Boroda   -In process
1071
    this.polygons.push(polygon);
ae432de6   Alexey Boroda   first commit
1072
  
9870b2b4   Alexey Boroda   -In process
1073
    GMaps.fire('polygon_added', polygon, this);
ae432de6   Alexey Boroda   first commit
1074
  
9870b2b4   Alexey Boroda   -In process
1075
1076
    return polygon;
  };
ae432de6   Alexey Boroda   first commit
1077
  
9870b2b4   Alexey Boroda   -In process
1078
1079
1080
1081
1082
  GMaps.prototype.removePolygon = function(polygon) {
    for (var i = 0; i < this.polygons.length; i++) {
      if (this.polygons[i] === polygon) {
        this.polygons[i].setMap(null);
        this.polygons.splice(i, 1);
ae432de6   Alexey Boroda   first commit
1083
  
9870b2b4   Alexey Boroda   -In process
1084
        GMaps.fire('polygon_removed', polygon, this);
ae432de6   Alexey Boroda   first commit
1085
  
9870b2b4   Alexey Boroda   -In process
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
        break;
      }
    }
  };
  
  GMaps.prototype.removePolygons = function() {
    for (var i = 0, item; item = this.polygons[i]; i++) {
      item.setMap(null);
    }
  
    this.polygons = [];
  };
  
  GMaps.prototype.getFromFusionTables = function(options) {
    var events = options.events;
  
    delete options.events;
  
    var fusion_tables_options = options,
        layer = new google.maps.FusionTablesLayer(fusion_tables_options);
  
    for (var ev in events) {
      (function(object, name) {
        google.maps.event.addListener(object, name, function(e) {
          events[name].apply(this, [e]);
        });
      })(layer, ev);
    }
  
    this.layers.push(layer);
  
    return layer;
  };
  
  GMaps.prototype.loadFromFusionTables = function(options) {
    var layer = this.getFromFusionTables(options);
    layer.setMap(this.map);
  
    return layer;
  };
  
  GMaps.prototype.getFromKML = function(options) {
    var url = options.url,
        events = options.events;
  
    delete options.url;
    delete options.events;
  
    var kml_options = options,
        layer = new google.maps.KmlLayer(url, kml_options);
  
    for (var ev in events) {
      (function(object, name) {
        google.maps.event.addListener(object, name, function(e) {
          events[name].apply(this, [e]);
        });
      })(layer, ev);
    }
  
    this.layers.push(layer);
  
    return layer;
  };
  
  GMaps.prototype.loadFromKML = function(options) {
    var layer = this.getFromKML(options);
    layer.setMap(this.map);
  
    return layer;
  };
  
  GMaps.prototype.addLayer = function(layerName, options) {
    //var default_layers = ['weather', 'clouds', 'traffic', 'transit', 'bicycling', 'panoramio', 'places'];
    options = options || {};
    var layer;
  
    switch(layerName) {
      case 'weather': this.singleLayers.weather = layer = new google.maps.weather.WeatherLayer();
        break;
      case 'clouds': this.singleLayers.clouds = layer = new google.maps.weather.CloudLayer();
        break;
      case 'traffic': this.singleLayers.traffic = layer = new google.maps.TrafficLayer();
        break;
      case 'transit': this.singleLayers.transit = layer = new google.maps.TransitLayer();
        break;
      case 'bicycling': this.singleLayers.bicycling = layer = new google.maps.BicyclingLayer();
        break;
      case 'panoramio':
          this.singleLayers.panoramio = layer = new google.maps.panoramio.PanoramioLayer();
          layer.setTag(options.filter);
          delete options.filter;
  
          //click event
          if (options.click) {
            google.maps.event.addListener(layer, 'click', function(event) {
              options.click(event);
              delete options.click;
            });
          }
        break;
        case 'places':
          this.singleLayers.places = layer = new google.maps.places.PlacesService(this.map);
  
          //search, nearbySearch, radarSearch callback, Both are the same
          if (options.search || options.nearbySearch || options.radarSearch) {
            var placeSearchRequest  = {
              bounds : options.bounds || null,
              keyword : options.keyword || null,
              location : options.location || null,
              name : options.name || null,
              radius : options.radius || null,
              rankBy : options.rankBy || null,
              types : options.types || null
            };
  
            if (options.radarSearch) {
              layer.radarSearch(placeSearchRequest, options.radarSearch);
            }
  
            if (options.search) {
              layer.search(placeSearchRequest, options.search);
            }
  
            if (options.nearbySearch) {
              layer.nearbySearch(placeSearchRequest, options.nearbySearch);
            }
          }
ae432de6   Alexey Boroda   first commit
1213
  
9870b2b4   Alexey Boroda   -In process
1214
1215
1216
1217
1218
1219
1220
1221
          //textSearch callback
          if (options.textSearch) {
            var textSearchRequest  = {
              bounds : options.bounds || null,
              location : options.location || null,
              query : options.query || null,
              radius : options.radius || null
            };
ae432de6   Alexey Boroda   first commit
1222
  
9870b2b4   Alexey Boroda   -In process
1223
1224
1225
1226
            layer.textSearch(textSearchRequest, options.textSearch);
          }
        break;
    }
ae432de6   Alexey Boroda   first commit
1227
  
9870b2b4   Alexey Boroda   -In process
1228
1229
1230
1231
1232
1233
1234
    if (layer !== undefined) {
      if (typeof layer.setOptions == 'function') {
        layer.setOptions(options);
      }
      if (typeof layer.setMap == 'function') {
        layer.setMap(this.map);
      }
ae432de6   Alexey Boroda   first commit
1235
  
9870b2b4   Alexey Boroda   -In process
1236
1237
1238
      return layer;
    }
  };
ae432de6   Alexey Boroda   first commit
1239
  
9870b2b4   Alexey Boroda   -In process
1240
1241
1242
  GMaps.prototype.removeLayer = function(layer) {
    if (typeof(layer) == "string" && this.singleLayers[layer] !== undefined) {
       this.singleLayers[layer].setMap(null);
ae432de6   Alexey Boroda   first commit
1243
  
9870b2b4   Alexey Boroda   -In process
1244
1245
1246
1247
1248
1249
1250
       delete this.singleLayers[layer];
    }
    else {
      for (var i = 0; i < this.layers.length; i++) {
        if (this.layers[i] === layer) {
          this.layers[i].setMap(null);
          this.layers.splice(i, 1);
ae432de6   Alexey Boroda   first commit
1251
  
9870b2b4   Alexey Boroda   -In process
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
          break;
        }
      }
    }
  };
  
  var travelMode, unitSystem;
  
  GMaps.prototype.getRoutes = function(options) {
    switch (options.travelMode) {
      case 'bicycling':
        travelMode = google.maps.TravelMode.BICYCLING;
        break;
      case 'transit':
        travelMode = google.maps.TravelMode.TRANSIT;
        break;
      case 'driving':
        travelMode = google.maps.TravelMode.DRIVING;
        break;
      default:
        travelMode = google.maps.TravelMode.WALKING;
        break;
    }
  
    if (options.unitSystem === 'imperial') {
      unitSystem = google.maps.UnitSystem.IMPERIAL;
    }
    else {
      unitSystem = google.maps.UnitSystem.METRIC;
    }
  
    var base_options = {
          avoidHighways: false,
          avoidTolls: false,
          optimizeWaypoints: false,
          waypoints: []
        },
        request_options =  extend_object(base_options, options);
  
    request_options.origin = /string/.test(typeof options.origin) ? options.origin : new google.maps.LatLng(options.origin[0], options.origin[1]);
    request_options.destination = /string/.test(typeof options.destination) ? options.destination : new google.maps.LatLng(options.destination[0], options.destination[1]);
    request_options.travelMode = travelMode;
    request_options.unitSystem = unitSystem;
  
    delete request_options.callback;
    delete request_options.error;
  
    var self = this,
        service = new google.maps.DirectionsService();
  
    service.route(request_options, function(result, status) {
      if (status === google.maps.DirectionsStatus.OK) {
        for (var r in result.routes) {
          if (result.routes.hasOwnProperty(r)) {
            self.routes.push(result.routes[r]);
          }
        }
ae432de6   Alexey Boroda   first commit
1309
  
9870b2b4   Alexey Boroda   -In process
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
        if (options.callback) {
          options.callback(self.routes);
        }
      }
      else {
        if (options.error) {
          options.error(result, status);
        }
      }
    });
  };
  
  GMaps.prototype.removeRoutes = function() {
    this.routes = [];
  };
  
  GMaps.prototype.getElevations = function(options) {
    options = extend_object({
      locations: [],
      path : false,
      samples : 256
    }, options);
  
    if (options.locations.length > 0) {
      if (options.locations[0].length > 0) {
        options.locations = array_flat(array_map([options.locations], arrayToLatLng,  false));
      }
    }
  
    var callback = options.callback;
    delete options.callback;
  
    var service = new google.maps.ElevationService();
  
    //location request
    if (!options.path) {
      delete options.path;
      delete options.samples;
  
      service.getElevationForLocations(options, function(result, status) {
        if (callback && typeof(callback) === "function") {
          callback(result, status);
        }
      });
    //path request
    } else {
      var pathRequest = {
        path : options.locations,
        samples : options.samples
      };
  
      service.getElevationAlongPath(pathRequest, function(result, status) {
       if (callback && typeof(callback) === "function") {
          callback(result, status);
        }
      });
    }
  };
  
  GMaps.prototype.cleanRoute = GMaps.prototype.removePolylines;
  
  GMaps.prototype.drawRoute = function(options) {
    var self = this;
  
    this.getRoutes({
      origin: options.origin,
      destination: options.destination,
      travelMode: options.travelMode,
      waypoints: options.waypoints,
      unitSystem: options.unitSystem,
      error: options.error,
      callback: function(e) {
        if (e.length > 0) {
          self.drawPolyline({
            path: e[e.length - 1].overview_path,
            strokeColor: options.strokeColor,
            strokeOpacity: options.strokeOpacity,
            strokeWeight: options.strokeWeight
          });
          
          if (options.callback) {
            options.callback(e[e.length - 1]);
          }
        }
      }
    });
  };
  
  GMaps.prototype.travelRoute = function(options) {
    if (options.origin && options.destination) {
      this.getRoutes({
        origin: options.origin,
        destination: options.destination,
        travelMode: options.travelMode,
        waypoints : options.waypoints,
        unitSystem: options.unitSystem,
        error: options.error,
        callback: function(e) {
          //start callback
          if (e.length > 0 && options.start) {
            options.start(e[e.length - 1]);
          }
ae432de6   Alexey Boroda   first commit
1412
  
9870b2b4   Alexey Boroda   -In process
1413
1414
1415
1416
1417
1418
1419
1420
          //step callback
          if (e.length > 0 && options.step) {
            var route = e[e.length - 1];
            if (route.legs.length > 0) {
              var steps = route.legs[0].steps;
              for (var i=0, step; step=steps[i]; i++) {
                step.step_number = i;
                options.step(step, (route.legs[0].steps.length - 1));
ae432de6   Alexey Boroda   first commit
1421
              }
9870b2b4   Alexey Boroda   -In process
1422
1423
            }
          }
ae432de6   Alexey Boroda   first commit
1424
  
9870b2b4   Alexey Boroda   -In process
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
          //end callback
          if (e.length > 0 && options.end) {
             options.end(e[e.length - 1]);
          }
        }
      });
    }
    else if (options.route) {
      if (options.route.legs.length > 0) {
        var steps = options.route.legs[0].steps;
        for (var i=0, step; step=steps[i]; i++) {
          step.step_number = i;
          options.step(step);
        }
      }
    }
  };
  
  GMaps.prototype.drawSteppedRoute = function(options) {
    var self = this;
    
    if (options.origin && options.destination) {
      this.getRoutes({
        origin: options.origin,
        destination: options.destination,
        travelMode: options.travelMode,
        waypoints : options.waypoints,
        error: options.error,
        callback: function(e) {
          //start callback
          if (e.length > 0 && options.start) {
            options.start(e[e.length - 1]);
          }
ae432de6   Alexey Boroda   first commit
1458
  
9870b2b4   Alexey Boroda   -In process
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
          //step callback
          if (e.length > 0 && options.step) {
            var route = e[e.length - 1];
            if (route.legs.length > 0) {
              var steps = route.legs[0].steps;
              for (var i=0, step; step=steps[i]; i++) {
                step.step_number = i;
                self.drawPolyline({
                  path: step.path,
                  strokeColor: options.strokeColor,
                  strokeOpacity: options.strokeOpacity,
                  strokeWeight: options.strokeWeight
                });
                options.step(step, (route.legs[0].steps.length - 1));
ae432de6   Alexey Boroda   first commit
1473
              }
9870b2b4   Alexey Boroda   -In process
1474
1475
            }
          }
ae432de6   Alexey Boroda   first commit
1476
  
9870b2b4   Alexey Boroda   -In process
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
          //end callback
          if (e.length > 0 && options.end) {
             options.end(e[e.length - 1]);
          }
        }
      });
    }
    else if (options.route) {
      if (options.route.legs.length > 0) {
        var steps = options.route.legs[0].steps;
        for (var i=0, step; step=steps[i]; i++) {
          step.step_number = i;
          self.drawPolyline({
            path: step.path,
            strokeColor: options.strokeColor,
            strokeOpacity: options.strokeOpacity,
            strokeWeight: options.strokeWeight
          });
          options.step(step);
        }
      }
    }
  };
  
  GMaps.Route = function(options) {
    this.origin = options.origin;
    this.destination = options.destination;
    this.waypoints = options.waypoints;
  
    this.map = options.map;
    this.route = options.route;
    this.step_count = 0;
    this.steps = this.route.legs[0].steps;
    this.steps_length = this.steps.length;
  
    this.polyline = this.map.drawPolyline({
      path: new google.maps.MVCArray(),
      strokeColor: options.strokeColor,
      strokeOpacity: options.strokeOpacity,
      strokeWeight: options.strokeWeight
    }).getPath();
  };
  
  GMaps.Route.prototype.getRoute = function(options) {
    var self = this;
  
    this.map.getRoutes({
      origin : this.origin,
      destination : this.destination,
      travelMode : options.travelMode,
      waypoints : this.waypoints || [],
      error: options.error,
      callback : function() {
        self.route = e[0];
  
        if (options.callback) {
          options.callback.call(self);
        }
      }
    });
  };
  
  GMaps.Route.prototype.back = function() {
    if (this.step_count > 0) {
      this.step_count--;
      var path = this.route.legs[0].steps[this.step_count].path;
  
      for (var p in path){
        if (path.hasOwnProperty(p)){
          this.polyline.pop();
        }
      }
    }
  };
ae432de6   Alexey Boroda   first commit
1551
  
9870b2b4   Alexey Boroda   -In process
1552
1553
1554
  GMaps.Route.prototype.forward = function() {
    if (this.step_count < this.steps_length) {
      var path = this.route.legs[0].steps[this.step_count].path;
ae432de6   Alexey Boroda   first commit
1555
  
9870b2b4   Alexey Boroda   -In process
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
      for (var p in path){
        if (path.hasOwnProperty(p)){
          this.polyline.push(path[p]);
        }
      }
      this.step_count++;
    }
  };
  
  GMaps.prototype.checkGeofence = function(lat, lng, fence) {
    return fence.containsLatLng(new google.maps.LatLng(lat, lng));
  };
  
  GMaps.prototype.checkMarkerGeofence = function(marker, outside_callback) {
    if (marker.fences) {
      for (var i = 0, fence; fence = marker.fences[i]; i++) {
        var pos = marker.getPosition();
        if (!this.checkGeofence(pos.lat(), pos.lng(), fence)) {
          outside_callback(marker, fence);
        }
      }
    }
  };
  
  GMaps.prototype.toImage = function(options) {
    var options = options || {},
        static_map_options = {};
  
    static_map_options['size'] = options['size'] || [this.el.clientWidth, this.el.clientHeight];
    static_map_options['lat'] = this.getCenter().lat();
    static_map_options['lng'] = this.getCenter().lng();
  
    if (this.markers.length > 0) {
      static_map_options['markers'] = [];
      
      for (var i = 0; i < this.markers.length; i++) {
        static_map_options['markers'].push({
          lat: this.markers[i].getPosition().lat(),
          lng: this.markers[i].getPosition().lng()
        });
      }
    }
  
    if (this.polylines.length > 0) {
      var polyline = this.polylines[0];
      
      static_map_options['polyline'] = {};
      static_map_options['polyline']['path'] = google.maps.geometry.encoding.encodePath(polyline.getPath());
      static_map_options['polyline']['strokeColor'] = polyline.strokeColor
      static_map_options['polyline']['strokeOpacity'] = polyline.strokeOpacity
      static_map_options['polyline']['strokeWeight'] = polyline.strokeWeight
    }
  
    return GMaps.staticMapURL(static_map_options);
  };
  
  GMaps.staticMapURL = function(options){
    var parameters = [],
        data,
        static_root = 'http://maps.googleapis.com/maps/api/staticmap';
  
    if (options.url) {
      static_root = options.url;
      delete options.url;
    }
  
    static_root += '?';
  
    var markers = options.markers;
    
    delete options.markers;
  
    if (!markers && options.marker) {
      markers = [options.marker];
      delete options.marker;
    }
  
    var styles = options.styles;
  
    delete options.styles;
  
    var polyline = options.polyline;
    delete options.polyline;
  
    /** Map options **/
    if (options.center) {
      parameters.push('center=' + options.center);
      delete options.center;
    }
    else if (options.address) {
      parameters.push('center=' + options.address);
      delete options.address;
    }
    else if (options.lat) {
      parameters.push(['center=', options.lat, ',', options.lng].join(''));
      delete options.lat;
      delete options.lng;
    }
    else if (options.visible) {
      var visible = encodeURI(options.visible.join('|'));
      parameters.push('visible=' + visible);
    }
  
    var size = options.size;
    if (size) {
      if (size.join) {
        size = size.join('x');
      }
      delete options.size;
    }
    else {
      size = '630x300';
    }
    parameters.push('size=' + size);
  
    if (!options.zoom && options.zoom !== false) {
      options.zoom = 15;
    }
  
    var sensor = options.hasOwnProperty('sensor') ? !!options.sensor : true;
    delete options.sensor;
    parameters.push('sensor=' + sensor);
  
    for (var param in options) {
      if (options.hasOwnProperty(param)) {
        parameters.push(param + '=' + options[param]);
      }
    }
  
    /** Markers **/
    if (markers) {
      var marker, loc;
  
      for (var i=0; data=markers[i]; i++) {
        marker = [];
  
        if (data.size && data.size !== 'normal') {
          marker.push('size:' + data.size);
          delete data.size;
        }
        else if (data.icon) {
          marker.push('icon:' + encodeURI(data.icon));
          delete data.icon;
        }
  
        if (data.color) {
          marker.push('color:' + data.color.replace('#', '0x'));
          delete data.color;
        }
  
        if (data.label) {
          marker.push('label:' + data.label[0].toUpperCase());
          delete data.label;
        }
  
        loc = (data.address ? data.address : data.lat + ',' + data.lng);
        delete data.address;
        delete data.lat;
        delete data.lng;
  
        for(var param in data){
          if (data.hasOwnProperty(param)) {
            marker.push(param + ':' + data[param]);
          }
        }
  
        if (marker.length || i === 0) {
          marker.push(loc);
          marker = marker.join('|');
          parameters.push('markers=' + encodeURI(marker));
        }
        // New marker without styles
        else {
          marker = parameters.pop() + encodeURI('|' + loc);
          parameters.push(marker);
        }
      }
    }
  
    /** Map Styles **/
    if (styles) {
      for (var i = 0; i < styles.length; i++) {
        var styleRule = [];
        if (styles[i].featureType){
          styleRule.push('feature:' + styles[i].featureType.toLowerCase());
        }
  
        if (styles[i].elementType) {
          styleRule.push('element:' + styles[i].elementType.toLowerCase());
        }
  
        for (var j = 0; j < styles[i].stylers.length; j++) {
          for (var p in styles[i].stylers[j]) {
            var ruleArg = styles[i].stylers[j][p];
            if (p == 'hue' || p == 'color') {
              ruleArg = '0x' + ruleArg.substring(1);
            }
            styleRule.push(p + ':' + ruleArg);
          }
        }
ae432de6   Alexey Boroda   first commit
1756
  
9870b2b4   Alexey Boroda   -In process
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
        var rule = styleRule.join('|');
        if (rule != '') {
          parameters.push('style=' + rule);
        }
      }
    }
  
    /** Polylines **/
    function parseColor(color, opacity) {
      if (color[0] === '#'){
        color = color.replace('#', '0x');
  
        if (opacity) {
          opacity = parseFloat(opacity);
          opacity = Math.min(1, Math.max(opacity, 0));
          if (opacity === 0) {
            return '0x00000000';
          }
          opacity = (opacity * 255).toString(16);
          if (opacity.length === 1) {
            opacity += opacity;
          }
ae432de6   Alexey Boroda   first commit
1779
  
9870b2b4   Alexey Boroda   -In process
1780
1781
1782
1783
1784
          color = color.slice(0,8) + opacity;
        }
      }
      return color;
    }
ae432de6   Alexey Boroda   first commit
1785
  
9870b2b4   Alexey Boroda   -In process
1786
1787
1788
    if (polyline) {
      data = polyline;
      polyline = [];
ae432de6   Alexey Boroda   first commit
1789
  
9870b2b4   Alexey Boroda   -In process
1790
1791
1792
      if (data.strokeWeight) {
        polyline.push('weight:' + parseInt(data.strokeWeight, 10));
      }
ae432de6   Alexey Boroda   first commit
1793
  
9870b2b4   Alexey Boroda   -In process
1794
1795
1796
1797
      if (data.strokeColor) {
        var color = parseColor(data.strokeColor, data.strokeOpacity);
        polyline.push('color:' + color);
      }
ae432de6   Alexey Boroda   first commit
1798
  
9870b2b4   Alexey Boroda   -In process
1799
1800
1801
1802
      if (data.fillColor) {
        var fillcolor = parseColor(data.fillColor, data.fillOpacity);
        polyline.push('fillcolor:' + fillcolor);
      }
ae432de6   Alexey Boroda   first commit
1803
  
9870b2b4   Alexey Boroda   -In process
1804
1805
1806
1807
1808
1809
1810
1811
1812
      var path = data.path;
      if (path.join) {
        for (var j=0, pos; pos=path[j]; j++) {
          polyline.push(pos.join(','));
        }
      }
      else {
        polyline.push('enc:' + path);
      }
ae432de6   Alexey Boroda   first commit
1813
  
9870b2b4   Alexey Boroda   -In process
1814
1815
1816
      polyline = polyline.join('|');
      parameters.push('path=' + encodeURI(polyline));
    }
ae432de6   Alexey Boroda   first commit
1817
  
9870b2b4   Alexey Boroda   -In process
1818
1819
1820
    /** Retina support **/
    var dpi = window.devicePixelRatio || 1;
    parameters.push('scale=' + dpi);
ae432de6   Alexey Boroda   first commit
1821
  
9870b2b4   Alexey Boroda   -In process
1822
1823
1824
    parameters = parameters.join('&');
    return static_root + parameters;
  };
ae432de6   Alexey Boroda   first commit
1825
  
9870b2b4   Alexey Boroda   -In process
1826
1827
1828
  GMaps.prototype.addMapType = function(mapTypeId, options) {
    if (options.hasOwnProperty("getTileUrl") && typeof(options["getTileUrl"]) == "function") {
      options.tileSize = options.tileSize || new google.maps.Size(256, 256);
ae432de6   Alexey Boroda   first commit
1829
  
9870b2b4   Alexey Boroda   -In process
1830
      var mapType = new google.maps.ImageMapType(options);
ae432de6   Alexey Boroda   first commit
1831
  
9870b2b4   Alexey Boroda   -In process
1832
1833
1834
1835
1836
1837
      this.map.mapTypes.set(mapTypeId, mapType);
    }
    else {
      throw "'getTileUrl' function required.";
    }
  };
ae432de6   Alexey Boroda   first commit
1838
  
9870b2b4   Alexey Boroda   -In process
1839
1840
1841
  GMaps.prototype.addOverlayMapType = function(options) {
    if (options.hasOwnProperty("getTile") && typeof(options["getTile"]) == "function") {
      var overlayMapTypeIndex = options.index;
ae432de6   Alexey Boroda   first commit
1842
  
9870b2b4   Alexey Boroda   -In process
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
      delete options.index;
  
      this.map.overlayMapTypes.insertAt(overlayMapTypeIndex, options);
    }
    else {
      throw "'getTile' function required.";
    }
  };
  
  GMaps.prototype.removeOverlayMapType = function(overlayMapTypeIndex) {
    this.map.overlayMapTypes.removeAt(overlayMapTypeIndex);
  };
  
  GMaps.prototype.addStyle = function(options) {
    var styledMapType = new google.maps.StyledMapType(options.styles, { name: options.styledMapName });
  
    this.map.mapTypes.set(options.mapTypeId, styledMapType);
  };
  
  GMaps.prototype.setStyle = function(mapTypeId) {
    this.map.setMapTypeId(mapTypeId);
  };
  
  GMaps.prototype.createPanorama = function(streetview_options) {
    if (!streetview_options.hasOwnProperty('lat') || !streetview_options.hasOwnProperty('lng')) {
      streetview_options.lat = this.getCenter().lat();
      streetview_options.lng = this.getCenter().lng();
    }
  
    this.panorama = GMaps.createPanorama(streetview_options);
  
    this.map.setStreetView(this.panorama);
  
    return this.panorama;
  };
  
  GMaps.createPanorama = function(options) {
    var el = getElementById(options.el, options.context);
  
    options.position = new google.maps.LatLng(options.lat, options.lng);
  
    delete options.el;
    delete options.context;
    delete options.lat;
    delete options.lng;
  
    var streetview_events = ['closeclick', 'links_changed', 'pano_changed', 'position_changed', 'pov_changed', 'resize', 'visible_changed'],
        streetview_options = extend_object({visible : true}, options);
  
    for (var i = 0; i < streetview_events.length; i++) {
      delete streetview_options[streetview_events[i]];
    }
  
    var panorama = new google.maps.StreetViewPanorama(el, streetview_options);
  
    for (var i = 0; i < streetview_events.length; i++) {
      (function(object, name) {
        if (options[name]) {
          google.maps.event.addListener(object, name, function(){
            options[name].apply(this);
          });
        }
      })(panorama, streetview_events[i]);
    }
  
    return panorama;
  };
  
  GMaps.prototype.on = function(event_name, handler) {
    return GMaps.on(event_name, this, handler);
  };
  
  GMaps.prototype.off = function(event_name) {
    GMaps.off(event_name, this);
  };
  
  GMaps.custom_events = ['marker_added', 'marker_removed', 'polyline_added', 'polyline_removed', 'polygon_added', 'polygon_removed', 'geolocated', 'geolocation_failed'];
  
  GMaps.on = function(event_name, object, handler) {
    if (GMaps.custom_events.indexOf(event_name) == -1) {
      if(object instanceof GMaps) object = object.map; 
      return google.maps.event.addListener(object, event_name, handler);
    }
    else {
      var registered_event = {
        handler : handler,
        eventName : event_name
      };
  
      object.registered_events[event_name] = object.registered_events[event_name] || [];
      object.registered_events[event_name].push(registered_event);
  
      return registered_event;
    }
  };
  
  GMaps.off = function(event_name, object) {
    if (GMaps.custom_events.indexOf(event_name) == -1) {
      if(object instanceof GMaps) object = object.map; 
      google.maps.event.clearListeners(object, event_name);
    }
    else {
      object.registered_events[event_name] = [];
    }
  };
  
  GMaps.fire = function(event_name, object, scope) {
    if (GMaps.custom_events.indexOf(event_name) == -1) {
      google.maps.event.trigger(object, event_name, Array.prototype.slice.apply(arguments).slice(2));
    }
    else {
      if(event_name in scope.registered_events) {
        var firing_events = scope.registered_events[event_name];
  
        for(var i = 0; i < firing_events.length; i++) {
          (function(handler, scope, object) {
            handler.apply(scope, [object]);
          })(firing_events[i]['handler'], scope, object);
        }
      }
    }
  };
  
  GMaps.geolocate = function(options) {
    var complete_callback = options.always || options.complete;
  
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(function(position) {
        options.success(position);
  
        if (complete_callback) {
          complete_callback();
        }
      }, function(error) {
        options.error(error);
  
        if (complete_callback) {
          complete_callback();
        }
      }, options.options);
    }
    else {
      options.not_supported();
  
      if (complete_callback) {
        complete_callback();
      }
    }
  };
  
  GMaps.geocode = function(options) {
    this.geocoder = new google.maps.Geocoder();
    var callback = options.callback;
    if (options.hasOwnProperty('lat') && options.hasOwnProperty('lng')) {
      options.latLng = new google.maps.LatLng(options.lat, options.lng);
    }
  
    delete options.lat;
    delete options.lng;
    delete options.callback;
    
    this.geocoder.geocode(options, function(results, status) {
      callback(results, status);
    });
  };
  
  //==========================
  // Polygon containsLatLng
  // https://github.com/tparkin/Google-Maps-Point-in-Polygon
  // Poygon getBounds extension - google-maps-extensions
  // http://code.google.com/p/google-maps-extensions/source/browse/google.maps.Polygon.getBounds.js
  if (!google.maps.Polygon.prototype.getBounds) {
    google.maps.Polygon.prototype.getBounds = function(latLng) {
      var bounds = new google.maps.LatLngBounds();
      var paths = this.getPaths();
      var path;
  
      for (var p = 0; p < paths.getLength(); p++) {
        path = paths.getAt(p);
        for (var i = 0; i < path.getLength(); i++) {
          bounds.extend(path.getAt(i));
        }
      }
ae432de6   Alexey Boroda   first commit
2026
  
9870b2b4   Alexey Boroda   -In process
2027
2028
2029
      return bounds;
    };
  }
ae432de6   Alexey Boroda   first commit
2030
  
9870b2b4   Alexey Boroda   -In process
2031
2032
2033
2034
2035
  if (!google.maps.Polygon.prototype.containsLatLng) {
    // Polygon containsLatLng - method to determine if a latLng is within a polygon
    google.maps.Polygon.prototype.containsLatLng = function(latLng) {
      // Exclude points outside of bounds as there is no way they are in the poly
      var bounds = this.getBounds();
ae432de6   Alexey Boroda   first commit
2036
  
9870b2b4   Alexey Boroda   -In process
2037
2038
2039
      if (bounds !== null && !bounds.contains(latLng)) {
        return false;
      }
ae432de6   Alexey Boroda   first commit
2040
  
9870b2b4   Alexey Boroda   -In process
2041
2042
      // Raycast point in polygon method
      var inPoly = false;
ae432de6   Alexey Boroda   first commit
2043
  
9870b2b4   Alexey Boroda   -In process
2044
2045
2046
2047
2048
      var numPaths = this.getPaths().getLength();
      for (var p = 0; p < numPaths; p++) {
        var path = this.getPaths().getAt(p);
        var numPoints = path.getLength();
        var j = numPoints - 1;
ae432de6   Alexey Boroda   first commit
2049
  
9870b2b4   Alexey Boroda   -In process
2050
2051
2052
        for (var i = 0; i < numPoints; i++) {
          var vertex1 = path.getAt(i);
          var vertex2 = path.getAt(j);
ae432de6   Alexey Boroda   first commit
2053
  
9870b2b4   Alexey Boroda   -In process
2054
2055
2056
2057
          if (vertex1.lng() < latLng.lng() && vertex2.lng() >= latLng.lng() || vertex2.lng() < latLng.lng() && vertex1.lng() >= latLng.lng()) {
            if (vertex1.lat() + (latLng.lng() - vertex1.lng()) / (vertex2.lng() - vertex1.lng()) * (vertex2.lat() - vertex1.lat()) < latLng.lat()) {
              inPoly = !inPoly;
            }
ae432de6   Alexey Boroda   first commit
2058
2059
          }
  
9870b2b4   Alexey Boroda   -In process
2060
2061
2062
          j = i;
        }
      }
ae432de6   Alexey Boroda   first commit
2063
  
9870b2b4   Alexey Boroda   -In process
2064
2065
2066
      return inPoly;
    };
  }
ae432de6   Alexey Boroda   first commit
2067
  
9870b2b4   Alexey Boroda   -In process
2068
2069
2070
2071
2072
2073
2074
  if (!google.maps.Circle.prototype.containsLatLng) {
    google.maps.Circle.prototype.containsLatLng = function(latLng) {
      if (google.maps.geometry) {
        return google.maps.geometry.spherical.computeDistanceBetween(this.getCenter(), latLng) <= this.getRadius();
      }
      else {
        return true;
ae432de6   Alexey Boroda   first commit
2075
      }
9870b2b4   Alexey Boroda   -In process
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
    };
  }
  
  google.maps.LatLngBounds.prototype.containsLatLng = function(latLng) {
    return this.contains(latLng);
  };
  
  google.maps.Marker.prototype.setFences = function(fences) {
    this.fences = fences;
  };
  
  google.maps.Marker.prototype.addFence = function(fence) {
    this.fences.push(fence);
  };
  
  google.maps.Marker.prototype.getId = function() {
    return this['__gm_id'];
  };
  
  //==========================
  // Array indexOf
  // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/indexOf
  if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
        "use strict";
        if (this == null) {
            throw new TypeError();
        }
        var t = Object(this);
        var len = t.length >>> 0;
        if (len === 0) {
            return -1;
        }
        var n = 0;
        if (arguments.length > 1) {
            n = Number(arguments[1]);
            if (n != n) { // shortcut for verifying if it's NaN
                n = 0;
            } else if (n != 0 && n != Infinity && n != -Infinity) {
                n = (n > 0 || -1) * Math.floor(Math.abs(n));
            }
        }
        if (n >= len) {
            return -1;
        }
        var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
        for (; k < len; k++) {
            if (k in t && t[k] === searchElement) {
                return k;
            }
        }
        return -1;
    }
  }
    
  return GMaps;
  }));