gmaps.js 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968
  1. /*!
  2. * GMaps.js v0.4.3
  3. * http://hpneo.github.com/gmaps/
  4. *
  5. * Copyright 2012, Gustavo Leon
  6. * Released under the MIT License.
  7. */
  8. if (window.google == undefined && window.google.maps == undefined) {
  9. throw 'Google Maps API is required. Please register the following JavaScript library http://maps.google.com/maps/api/js?sensor=true.'
  10. }
  11. var extend_object = function(obj, new_obj) {
  12. var name;
  13. if (obj === new_obj) {
  14. return obj;
  15. }
  16. for (name in new_obj) {
  17. obj[name] = new_obj[name];
  18. }
  19. return obj;
  20. };
  21. var replace_object = function(obj, replace) {
  22. var name;
  23. if (obj === replace) {
  24. return obj;
  25. }
  26. for (name in replace) {
  27. if (obj[name] != undefined) {
  28. obj[name] = replace[name];
  29. }
  30. }
  31. return obj;
  32. };
  33. var array_map = function(array, callback) {
  34. var original_callback_params = Array.prototype.slice.call(arguments, 2),
  35. array_return = [],
  36. array_length = array.length,
  37. i;
  38. if (Array.prototype.map && array.map === Array.prototype.map) {
  39. array_return = Array.prototype.map.call(array, function(item) {
  40. callback_params = original_callback_params;
  41. callback_params.splice(0, 0, item);
  42. return callback.apply(this, callback_params);
  43. });
  44. }
  45. else {
  46. for (i = 0; i < array_length; i++) {
  47. callback_params = original_callback_params;
  48. callback_params = callback_params.splice(0, 0, array[i]);
  49. array_return.push(callback.apply(this, callback_params));
  50. }
  51. }
  52. return array_return;
  53. };
  54. var array_flat = function(array) {
  55. var new_array = [],
  56. i;
  57. for (i = 0; i < array.length; i++) {
  58. new_array = new_array.concat(array[i]);
  59. }
  60. return new_array;
  61. };
  62. var coordsToLatLngs = function(coords, useGeoJSON) {
  63. var first_coord = coords[0],
  64. second_coord = coords[1];
  65. if (useGeoJSON) {
  66. first_coord = coords[1];
  67. second_coord = coords[0];
  68. }
  69. return new google.maps.LatLng(first_coord, second_coord);
  70. };
  71. var arrayToLatLng = function(coords, useGeoJSON) {
  72. var i;
  73. for (i = 0; i < coords.length; i++) {
  74. if (coords[i].length > 0 && typeof(coords[i][0]) != "number") {
  75. coords[i] = arrayToLatLng(coords[i], useGeoJSON);
  76. }
  77. else {
  78. coords[i] = coordsToLatLngs(coords[i], useGeoJSON);
  79. }
  80. }
  81. return coords;
  82. };
  83. var getElementById = function(id, context) {
  84. var element,
  85. id = id.replace('#', '');
  86. if ('jQuery' in this && context) {
  87. element = $("#" + id, context)[0];
  88. } else {
  89. element = document.getElementById(id);
  90. };
  91. return element;
  92. };
  93. var findAbsolutePosition = function(obj) {
  94. var curleft = 0,
  95. curtop = 0;
  96. if (obj.offsetParent) {
  97. do {
  98. curleft += obj.offsetLeft;
  99. curtop += obj.offsetTop;
  100. } while (obj = obj.offsetParent);
  101. }
  102. return [curleft, curtop];
  103. };
  104. var GMaps = (function(global) {
  105. "use strict";
  106. var doc = document;
  107. var GMaps = function(options) {
  108. options.zoom = options.zoom || 15;
  109. options.mapType = options.mapType || 'roadmap';
  110. var self = this,
  111. i,
  112. events_that_hide_context_menu = ['bounds_changed', 'center_changed', 'click', 'dblclick', 'drag', 'dragend', 'dragstart', 'idle', 'maptypeid_changed', 'projection_changed', 'resize', 'tilesloaded', 'zoom_changed'],
  113. events_that_doesnt_hide_context_menu = ['mousemove', 'mouseout', 'mouseover'],
  114. options_to_be_deleted = ['el', 'lat', 'lng', 'mapType', 'width', 'height', 'markerClusterer', 'enableNewStyle'],
  115. container_id = options.el || options.div,
  116. markerClustererFunction = options.markerClusterer,
  117. mapType = google.maps.MapTypeId[options.mapType.toUpperCase()],
  118. map_center = new google.maps.LatLng(options.lat, options.lng),
  119. zoomControl = options.zoomControl || true,
  120. zoomControlOpt = options.zoomControlOpt || {
  121. style: 'DEFAULT',
  122. position: 'TOP_LEFT'
  123. },
  124. zoomControlStyle = zoomControlOpt.style || 'DEFAULT',
  125. zoomControlPosition = zoomControlOpt.position || 'TOP_LEFT',
  126. panControl = options.panControl || true,
  127. mapTypeControl = options.mapTypeControl || true,
  128. scaleControl = options.scaleControl || true,
  129. streetViewControl = options.streetViewControl || true,
  130. overviewMapControl = overviewMapControl || true,
  131. map_options = {},
  132. map_base_options = {
  133. zoom: this.zoom,
  134. center: map_center,
  135. mapTypeId: mapType
  136. },
  137. map_controls_options = {
  138. panControl: panControl,
  139. zoomControl: zoomControl,
  140. zoomControlOptions: {
  141. style: google.maps.ZoomControlStyle[zoomControlStyle],
  142. position: google.maps.ControlPosition[zoomControlPosition]
  143. },
  144. mapTypeControl: mapTypeControl,
  145. scaleControl: scaleControl,
  146. streetViewControl: streetViewControl,
  147. overviewMapControl: overviewMapControl
  148. };
  149. if (typeof(options.el) === 'string' || typeof(options.div) === 'string') {
  150. this.el = getElementById(container_id, options.context);
  151. } else {
  152. this.el = container_id;
  153. }
  154. if (typeof(this.el) === 'undefined' || this.el === null) {
  155. throw 'No element defined.';
  156. }
  157. window.context_menu = window.context_menu || {};
  158. window.context_menu[self.el.id] = {};
  159. this.controls = [];
  160. this.overlays = [];
  161. this.layers = []; // array with kml/georss and fusiontables layers, can be as many
  162. this.singleLayers = {}; // object with the other layers, only one per layer
  163. this.markers = [];
  164. this.polylines = [];
  165. this.routes = [];
  166. this.polygons = [];
  167. this.infoWindow = null;
  168. this.overlay_el = null;
  169. this.zoom = options.zoom;
  170. this.registered_events = {};
  171. this.el.style.width = options.width || this.el.scrollWidth || this.el.offsetWidth;
  172. this.el.style.height = options.height || this.el.scrollHeight || this.el.offsetHeight;
  173. google.maps.visualRefresh = options.enableNewStyle;
  174. for (i = 0; i < options_to_be_deleted.length; i++) {
  175. delete options[options_to_be_deleted[i]];
  176. }
  177. if(options.disableDefaultUI != true) {
  178. map_base_options = extend_object(map_base_options, map_controls_options);
  179. }
  180. map_options = extend_object(map_base_options, options);
  181. for (i = 0; i < events_that_hide_context_menu.length; i++) {
  182. delete map_options[events_that_hide_context_menu[i]];
  183. }
  184. for (i = 0; i < events_that_doesnt_hide_context_menu.length; i++) {
  185. delete map_options[events_that_doesnt_hide_context_menu[i]];
  186. }
  187. this.map = new google.maps.Map(this.el, map_options);
  188. if (markerClustererFunction) {
  189. this.markerClusterer = markerClustererFunction.apply(this, [this.map]);
  190. }
  191. var buildContextMenuHTML = function(control, e) {
  192. var html = '',
  193. options = window.context_menu[self.el.id][control];
  194. for (var i in options){
  195. if (options.hasOwnProperty(i)) {
  196. var option = options[i];
  197. html += '<li><a id="' + control + '_' + i + '" href="#">' + option.title + '</a></li>';
  198. }
  199. }
  200. if (!getElementById('gmaps_context_menu')) return;
  201. var context_menu_element = getElementById('gmaps_context_menu');
  202. context_menu_element.innerHTML = html;
  203. var context_menu_items = context_menu_element.getElementsByTagName('a'),
  204. context_menu_items_count = context_menu_items.length
  205. i;
  206. for (i = 0; i < context_menu_items_count; i++) {
  207. var context_menu_item = context_menu_items[i];
  208. var assign_menu_item_action = function(ev){
  209. ev.preventDefault();
  210. options[this.id.replace(control + '_', '')].action.apply(self, [e]);
  211. self.hideContextMenu();
  212. };
  213. google.maps.event.clearListeners(context_menu_item, 'click');
  214. google.maps.event.addDomListenerOnce(context_menu_item, 'click', assign_menu_item_action, false);
  215. }
  216. var position = findAbsolutePosition.apply(this, [self.el]),
  217. left = position[0] + e.pixel.x - 15,
  218. top = position[1] + e.pixel.y- 15;
  219. context_menu_element.style.left = left + "px";
  220. context_menu_element.style.top = top + "px";
  221. context_menu_element.style.display = 'block';
  222. };
  223. var buildContextMenu = function(control, e) {
  224. if (control === 'marker') {
  225. e.pixel = {};
  226. var overlay = new google.maps.OverlayView();
  227. overlay.setMap(self.map);
  228. overlay.draw = function() {
  229. var projection = overlay.getProjection(),
  230. position = e.marker.getPosition();
  231. e.pixel = projection.fromLatLngToContainerPixel(position);
  232. buildContextMenuHTML(control, e);
  233. };
  234. }
  235. else {
  236. buildContextMenuHTML(control, e);
  237. }
  238. };
  239. this.setContextMenu = function(options) {
  240. window.context_menu[self.el.id][options.control] = {};
  241. var i,
  242. ul = doc.createElement('ul');
  243. for (i in options.options) {
  244. if (options.options.hasOwnProperty(i)) {
  245. var option = options.options[i];
  246. window.context_menu[self.el.id][options.control][option.name] = {
  247. title: option.title,
  248. action: option.action
  249. };
  250. }
  251. }
  252. ul.id = 'gmaps_context_menu';
  253. ul.style.display = 'none';
  254. ul.style.position = 'absolute';
  255. ul.style.minWidth = '100px';
  256. ul.style.background = 'white';
  257. ul.style.listStyle = 'none';
  258. ul.style.padding = '8px';
  259. ul.style.boxShadow = '2px 2px 6px #ccc';
  260. doc.body.appendChild(ul);
  261. var context_menu_element = getElementById('gmaps_context_menu')
  262. google.maps.event.addDomListener(context_menu_element, 'mouseout', function(ev) {
  263. if (!ev.relatedTarget || !this.contains(ev.relatedTarget)) {
  264. window.setTimeout(function(){
  265. context_menu_element.style.display = 'none';
  266. }, 400);
  267. }
  268. }, false);
  269. };
  270. this.hideContextMenu = function() {
  271. var context_menu_element = getElementById('gmaps_context_menu');
  272. if (context_menu_element) {
  273. context_menu_element.style.display = 'none';
  274. }
  275. };
  276. var setupListener = function(object, name) {
  277. google.maps.event.addListener(object, name, function(e){
  278. if (e == undefined) {
  279. e = this;
  280. }
  281. options[name].apply(this, [e]);
  282. self.hideContextMenu();
  283. });
  284. };
  285. for (var ev = 0; ev < events_that_hide_context_menu.length; ev++) {
  286. var name = events_that_hide_context_menu[ev];
  287. if (name in options) {
  288. setupListener(this.map, name);
  289. }
  290. }
  291. for (var ev = 0; ev < events_that_doesnt_hide_context_menu.length; ev++) {
  292. var name = events_that_doesnt_hide_context_menu[ev];
  293. if (name in options) {
  294. setupListener(this.map, name);
  295. }
  296. }
  297. google.maps.event.addListener(this.map, 'rightclick', function(e) {
  298. if (options.rightclick) {
  299. options.rightclick.apply(this, [e]);
  300. }
  301. if(window.context_menu[self.el.id]['map'] != undefined) {
  302. buildContextMenu('map', e);
  303. }
  304. });
  305. this.refresh = function() {
  306. google.maps.event.trigger(this.map, 'resize');
  307. };
  308. this.fitZoom = function() {
  309. var latLngs = [],
  310. markers_length = this.markers.length,
  311. i;
  312. for (i = 0; i < markers_length; i++) {
  313. latLngs.push(this.markers[i].getPosition());
  314. }
  315. this.fitLatLngBounds(latLngs);
  316. };
  317. this.fitLatLngBounds = function(latLngs) {
  318. var total = latLngs.length;
  319. var bounds = new google.maps.LatLngBounds();
  320. for(var i=0; i < total; i++) {
  321. bounds.extend(latLngs[i]);
  322. }
  323. this.map.fitBounds(bounds);
  324. };
  325. this.setCenter = function(lat, lng, callback) {
  326. this.map.panTo(new google.maps.LatLng(lat, lng));
  327. if (callback) {
  328. callback();
  329. }
  330. };
  331. this.getElement = function() {
  332. return this.el;
  333. };
  334. this.zoomIn = function(value) {
  335. value = value || 1;
  336. this.zoom = this.map.getZoom() + value;
  337. this.map.setZoom(this.zoom);
  338. };
  339. this.zoomOut = function(value) {
  340. value = value || 1;
  341. this.zoom = this.map.getZoom() - value;
  342. this.map.setZoom(this.zoom);
  343. };
  344. var native_methods = [],
  345. method;
  346. for (method in this.map) {
  347. if (typeof(this.map[method]) == 'function' && !this[method]) {
  348. native_methods.push(method);
  349. }
  350. }
  351. for (i=0; i < native_methods.length; i++) {
  352. (function(gmaps, scope, method_name) {
  353. gmaps[method_name] = function(){
  354. return scope[method_name].apply(scope, arguments);
  355. };
  356. })(this, this.map, native_methods[i]);
  357. }
  358. };
  359. return GMaps;
  360. })(this);
  361. GMaps.prototype.createControl = function(options) {
  362. var control = document.createElement('div');
  363. control.style.cursor = 'pointer';
  364. control.style.fontFamily = 'Arial, sans-serif';
  365. control.style.fontSize = '13px';
  366. control.style.boxShadow = 'rgba(0, 0, 0, 0.398438) 0px 2px 4px';
  367. for (var option in options.style) {
  368. control.style[option] = options.style[option];
  369. }
  370. if (options.id) {
  371. control.id = options.id;
  372. }
  373. if (options.classes) {
  374. control.className = options.classes;
  375. }
  376. if (options.content) {
  377. control.innerHTML = options.content;
  378. }
  379. for (var ev in options.events) {
  380. (function(object, name) {
  381. google.maps.event.addDomListener(object, name, function(){
  382. options.events[name].apply(this, [this]);
  383. });
  384. })(control, ev);
  385. }
  386. control.index = 1;
  387. return control;
  388. };
  389. GMaps.prototype.addControl = function(options) {
  390. var position = google.maps.ControlPosition[options.position.toUpperCase()];
  391. delete options.position;
  392. var control = this.createControl(options);
  393. this.controls.push(control);
  394. this.map.controls[position].push(control);
  395. return control;
  396. };
  397. GMaps.prototype.createMarker = function(options) {
  398. if (options.lat == undefined && options.lng == undefined && options.position == undefined) {
  399. throw 'No latitude or longitude defined.';
  400. }
  401. var self = this,
  402. details = options.details,
  403. fences = options.fences,
  404. outside = options.outside,
  405. base_options = {
  406. position: new google.maps.LatLng(options.lat, options.lng),
  407. map: null
  408. };
  409. delete options.lat;
  410. delete options.lng;
  411. delete options.fences;
  412. delete options.outside;
  413. var marker_options = extend_object(base_options, options),
  414. marker = new google.maps.Marker(marker_options);
  415. marker.fences = fences;
  416. if (options.infoWindow) {
  417. marker.infoWindow = new google.maps.InfoWindow(options.infoWindow);
  418. var info_window_events = ['closeclick', 'content_changed', 'domready', 'position_changed', 'zindex_changed'];
  419. for (var ev = 0; ev < info_window_events.length; ev++) {
  420. (function(object, name) {
  421. if (options.infoWindow[name]) {
  422. google.maps.event.addListener(object, name, function(e){
  423. options.infoWindow[name].apply(this, [e]);
  424. });
  425. }
  426. })(marker.infoWindow, info_window_events[ev]);
  427. }
  428. }
  429. 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'];
  430. var marker_events_with_mouse = ['dblclick', 'drag', 'dragend', 'dragstart', 'mousedown', 'mouseout', 'mouseover', 'mouseup'];
  431. for (var ev = 0; ev < marker_events.length; ev++) {
  432. (function(object, name) {
  433. if (options[name]) {
  434. google.maps.event.addListener(object, name, function(){
  435. options[name].apply(this, [this]);
  436. });
  437. }
  438. })(marker, marker_events[ev]);
  439. }
  440. for (var ev = 0; ev < marker_events_with_mouse.length; ev++) {
  441. (function(map, object, name) {
  442. if (options[name]) {
  443. google.maps.event.addListener(object, name, function(me){
  444. if(!me.pixel){
  445. me.pixel = map.getProjection().fromLatLngToPoint(me.latLng)
  446. }
  447. options[name].apply(this, [me]);
  448. });
  449. }
  450. })(this.map, marker, marker_events_with_mouse[ev]);
  451. }
  452. google.maps.event.addListener(marker, 'click', function() {
  453. this.details = details;
  454. if (options.click) {
  455. options.click.apply(this, [this]);
  456. }
  457. if (marker.infoWindow) {
  458. self.hideInfoWindows();
  459. marker.infoWindow.open(self.map, marker);
  460. }
  461. });
  462. google.maps.event.addListener(marker, 'rightclick', function(e) {
  463. e.marker = this;
  464. if (options.rightclick) {
  465. options.rightclick.apply(this, [e]);
  466. }
  467. if (window.context_menu[self.el.id]['marker'] != undefined) {
  468. buildContextMenu('marker', e);
  469. }
  470. });
  471. if (marker.fences) {
  472. google.maps.event.addListener(marker, 'dragend', function() {
  473. self.checkMarkerGeofence(marker, function(m, f) {
  474. outside(m, f);
  475. });
  476. });
  477. }
  478. return marker;
  479. };
  480. GMaps.prototype.addMarker = function(options) {
  481. var marker;
  482. if(options.hasOwnProperty('gm_accessors_')) {
  483. // Native google.maps.Marker object
  484. marker = options;
  485. }
  486. else {
  487. if ((options.hasOwnProperty('lat') && options.hasOwnProperty('lng')) || options.position) {
  488. marker = this.createMarker(options);
  489. }
  490. else {
  491. throw 'No latitude or longitude defined.';
  492. }
  493. }
  494. marker.setMap(this.map);
  495. if(this.markerClusterer) {
  496. this.markerClusterer.addMarker(marker);
  497. }
  498. this.markers.push(marker);
  499. GMaps.fire('marker_added', marker, this);
  500. return marker;
  501. };
  502. GMaps.prototype.addMarkers = function(array) {
  503. for (var i = 0, marker; marker=array[i]; i++) {
  504. this.addMarker(marker);
  505. }
  506. return this.markers;
  507. };
  508. GMaps.prototype.hideInfoWindows = function() {
  509. for (var i = 0, marker; marker = this.markers[i]; i++){
  510. if (marker.infoWindow){
  511. marker.infoWindow.close();
  512. }
  513. }
  514. };
  515. GMaps.prototype.removeMarker = function(marker) {
  516. for (var i = 0; i < this.markers.length; i++) {
  517. if (this.markers[i] === marker) {
  518. this.markers[i].setMap(null);
  519. this.markers.splice(i, 1);
  520. GMaps.fire('marker_removed', marker, this);
  521. break;
  522. }
  523. }
  524. return marker;
  525. };
  526. GMaps.prototype.removeMarkers = function(collection) {
  527. var collection = (collection || this.markers);
  528. for (var i = 0;i < this.markers.length; i++) {
  529. if(this.markers[i] === collection[i]) {
  530. this.markers[i].setMap(null);
  531. }
  532. }
  533. var new_markers = [];
  534. for (var i = 0;i < this.markers.length; i++) {
  535. if(this.markers[i].getMap() != null) {
  536. new_markers.push(this.markers[i]);
  537. }
  538. }
  539. this.markers = new_markers;
  540. };
  541. GMaps.prototype.drawOverlay = function(options) {
  542. var overlay = new google.maps.OverlayView(),
  543. auto_show = true;
  544. overlay.setMap(this.map);
  545. if (options.auto_show != null) {
  546. auto_show = options.auto_show;
  547. }
  548. overlay.onAdd = function() {
  549. var el = document.createElement('div');
  550. el.style.borderStyle = "none";
  551. el.style.borderWidth = "0px";
  552. el.style.position = "absolute";
  553. el.style.zIndex = 100;
  554. el.innerHTML = options.content;
  555. overlay.el = el;
  556. if (!options.layer) {
  557. options.layer = 'overlayLayer';
  558. }
  559. var panes = this.getPanes(),
  560. overlayLayer = panes[options.layer],
  561. stop_overlay_events = ['contextmenu', 'DOMMouseScroll', 'dblclick', 'mousedown'];
  562. overlayLayer.appendChild(el);
  563. for (var ev = 0; ev < stop_overlay_events.length; ev++) {
  564. (function(object, name) {
  565. google.maps.event.addDomListener(object, name, function(e){
  566. if (navigator.userAgent.toLowerCase().indexOf('msie') != -1 && document.all) {
  567. e.cancelBubble = true;
  568. e.returnValue = false;
  569. }
  570. else {
  571. e.stopPropagation();
  572. }
  573. });
  574. })(el, stop_overlay_events[ev]);
  575. }
  576. google.maps.event.trigger(this, 'ready');
  577. };
  578. overlay.draw = function() {
  579. var projection = this.getProjection(),
  580. pixel = projection.fromLatLngToDivPixel(new google.maps.LatLng(options.lat, options.lng));
  581. options.horizontalOffset = options.horizontalOffset || 0;
  582. options.verticalOffset = options.verticalOffset || 0;
  583. var el = overlay.el,
  584. content = el.children[0],
  585. content_height = content.clientHeight,
  586. content_width = content.clientWidth;
  587. switch (options.verticalAlign) {
  588. case 'top':
  589. el.style.top = (pixel.y - content_height + options.verticalOffset) + 'px';
  590. break;
  591. default:
  592. case 'middle':
  593. el.style.top = (pixel.y - (content_height / 2) + options.verticalOffset) + 'px';
  594. break;
  595. case 'bottom':
  596. el.style.top = (pixel.y + options.verticalOffset) + 'px';
  597. break;
  598. }
  599. switch (options.horizontalAlign) {
  600. case 'left':
  601. el.style.left = (pixel.x - content_width + options.horizontalOffset) + 'px';
  602. break;
  603. default:
  604. case 'center':
  605. el.style.left = (pixel.x - (content_width / 2) + options.horizontalOffset) + 'px';
  606. break;
  607. case 'right':
  608. el.style.left = (pixel.x + options.horizontalOffset) + 'px';
  609. break;
  610. }
  611. el.style.display = auto_show ? 'block' : 'none';
  612. if (!auto_show) {
  613. options.show.apply(this, [el]);
  614. }
  615. };
  616. overlay.onRemove = function() {
  617. var el = overlay.el;
  618. if (options.remove) {
  619. options.remove.apply(this, [el]);
  620. }
  621. else {
  622. overlay.el.parentNode.removeChild(overlay.el);
  623. overlay.el = null;
  624. }
  625. };
  626. this.overlays.push(overlay);
  627. return overlay;
  628. };
  629. GMaps.prototype.removeOverlay = function(overlay) {
  630. for (var i = 0; i < this.overlays.length; i++) {
  631. if (this.overlays[i] === overlay) {
  632. this.overlays[i].setMap(null);
  633. this.overlays.splice(i, 1);
  634. break;
  635. }
  636. }
  637. };
  638. GMaps.prototype.removeOverlays = function() {
  639. for (var i = 0, item; item = this.overlays[i]; i++) {
  640. item.setMap(null);
  641. }
  642. this.overlays = [];
  643. };
  644. GMaps.prototype.drawPolyline = function(options) {
  645. var path = [],
  646. points = options.path;
  647. if (points.length) {
  648. if (points[0][0] === undefined) {
  649. path = points;
  650. }
  651. else {
  652. for (var i=0, latlng; latlng=points[i]; i++) {
  653. path.push(new google.maps.LatLng(latlng[0], latlng[1]));
  654. }
  655. }
  656. }
  657. var polyline_options = {
  658. map: this.map,
  659. path: path,
  660. strokeColor: options.strokeColor,
  661. strokeOpacity: options.strokeOpacity,
  662. strokeWeight: options.strokeWeight,
  663. geodesic: options.geodesic,
  664. clickable: true,
  665. editable: false,
  666. visible: true
  667. };
  668. if (options.hasOwnProperty("clickable")) {
  669. polyline_options.clickable = options.clickable;
  670. }
  671. if (options.hasOwnProperty("editable")) {
  672. polyline_options.editable = options.editable;
  673. }
  674. if (options.hasOwnProperty("icons")) {
  675. polyline_options.icons = options.icons;
  676. }
  677. if (options.hasOwnProperty("zIndex")) {
  678. polyline_options.zIndex = options.zIndex;
  679. }
  680. var polyline = new google.maps.Polyline(polyline_options);
  681. var polyline_events = ['click', 'dblclick', 'mousedown', 'mousemove', 'mouseout', 'mouseover', 'mouseup', 'rightclick'];
  682. for (var ev = 0; ev < polyline_events.length; ev++) {
  683. (function(object, name) {
  684. if (options[name]) {
  685. google.maps.event.addListener(object, name, function(e){
  686. options[name].apply(this, [e]);
  687. });
  688. }
  689. })(polyline, polyline_events[ev]);
  690. }
  691. this.polylines.push(polyline);
  692. GMaps.fire('polyline_added', polyline, this);
  693. return polyline;
  694. };
  695. GMaps.prototype.removePolyline = function(polyline) {
  696. for (var i = 0; i < this.polylines.length; i++) {
  697. if (this.polylines[i] === polyline) {
  698. this.polylines[i].setMap(null);
  699. this.polylines.splice(i, 1);
  700. GMaps.fire('polyline_removed', polyline, this);
  701. break;
  702. }
  703. }
  704. };
  705. GMaps.prototype.removePolylines = function() {
  706. for (var i = 0, item; item = this.polylines[i]; i++) {
  707. item.setMap(null);
  708. }
  709. this.polylines = [];
  710. };
  711. GMaps.prototype.drawCircle = function(options) {
  712. options = extend_object({
  713. map: this.map,
  714. center: new google.maps.LatLng(options.lat, options.lng)
  715. }, options);
  716. delete options.lat;
  717. delete options.lng;
  718. var polygon = new google.maps.Circle(options),
  719. polygon_events = ['click', 'dblclick', 'mousedown', 'mousemove', 'mouseout', 'mouseover', 'mouseup', 'rightclick'];
  720. for (var ev = 0; ev < polygon_events.length; ev++) {
  721. (function(object, name) {
  722. if (options[name]) {
  723. google.maps.event.addListener(object, name, function(e){
  724. options[name].apply(this, [e]);
  725. });
  726. }
  727. })(polygon, polygon_events[ev]);
  728. }
  729. this.polygons.push(polygon);
  730. return polygon;
  731. };
  732. GMaps.prototype.drawRectangle = function(options) {
  733. options = extend_object({
  734. map: this.map
  735. }, options);
  736. var latLngBounds = new google.maps.LatLngBounds(
  737. new google.maps.LatLng(options.bounds[0][0], options.bounds[0][1]),
  738. new google.maps.LatLng(options.bounds[1][0], options.bounds[1][1])
  739. );
  740. options.bounds = latLngBounds;
  741. var polygon = new google.maps.Rectangle(options),
  742. polygon_events = ['click', 'dblclick', 'mousedown', 'mousemove', 'mouseout', 'mouseover', 'mouseup', 'rightclick'];
  743. for (var ev = 0; ev < polygon_events.length; ev++) {
  744. (function(object, name) {
  745. if (options[name]) {
  746. google.maps.event.addListener(object, name, function(e){
  747. options[name].apply(this, [e]);
  748. });
  749. }
  750. })(polygon, polygon_events[ev]);
  751. }
  752. this.polygons.push(polygon);
  753. return polygon;
  754. };
  755. GMaps.prototype.drawPolygon = function(options) {
  756. var useGeoJSON = false;
  757. if(options.hasOwnProperty("useGeoJSON")) {
  758. useGeoJSON = options.useGeoJSON;
  759. }
  760. delete options.useGeoJSON;
  761. options = extend_object({
  762. map: this.map
  763. }, options);
  764. if (useGeoJSON == false) {
  765. options.paths = [options.paths.slice(0)];
  766. }
  767. if (options.paths.length > 0) {
  768. if (options.paths[0].length > 0) {
  769. options.paths = array_flat(array_map(options.paths, arrayToLatLng, useGeoJSON));
  770. }
  771. }
  772. var polygon = new google.maps.Polygon(options),
  773. polygon_events = ['click', 'dblclick', 'mousedown', 'mousemove', 'mouseout', 'mouseover', 'mouseup', 'rightclick'];
  774. for (var ev = 0; ev < polygon_events.length; ev++) {
  775. (function(object, name) {
  776. if (options[name]) {
  777. google.maps.event.addListener(object, name, function(e){
  778. options[name].apply(this, [e]);
  779. });
  780. }
  781. })(polygon, polygon_events[ev]);
  782. }
  783. this.polygons.push(polygon);
  784. GMaps.fire('polygon_added', polygon, this);
  785. return polygon;
  786. };
  787. GMaps.prototype.removePolygon = function(polygon) {
  788. for (var i = 0; i < this.polygons.length; i++) {
  789. if (this.polygons[i] === polygon) {
  790. this.polygons[i].setMap(null);
  791. this.polygons.splice(i, 1);
  792. GMaps.fire('polygon_removed', polygon, this);
  793. break;
  794. }
  795. }
  796. };
  797. GMaps.prototype.removePolygons = function() {
  798. for (var i = 0, item; item = this.polygons[i]; i++) {
  799. item.setMap(null);
  800. }
  801. this.polygons = [];
  802. };
  803. GMaps.prototype.getFromFusionTables = function(options) {
  804. var events = options.events;
  805. delete options.events;
  806. var fusion_tables_options = options,
  807. layer = new google.maps.FusionTablesLayer(fusion_tables_options);
  808. for (var ev in events) {
  809. (function(object, name) {
  810. google.maps.event.addListener(object, name, function(e) {
  811. events[name].apply(this, [e]);
  812. });
  813. })(layer, ev);
  814. }
  815. this.layers.push(layer);
  816. return layer;
  817. };
  818. GMaps.prototype.loadFromFusionTables = function(options) {
  819. var layer = this.getFromFusionTables(options);
  820. layer.setMap(this.map);
  821. return layer;
  822. };
  823. GMaps.prototype.getFromKML = function(options) {
  824. var url = options.url,
  825. events = options.events;
  826. delete options.url;
  827. delete options.events;
  828. var kml_options = options,
  829. layer = new google.maps.KmlLayer(url, kml_options);
  830. for (var ev in events) {
  831. (function(object, name) {
  832. google.maps.event.addListener(object, name, function(e) {
  833. events[name].apply(this, [e]);
  834. });
  835. })(layer, ev);
  836. }
  837. this.layers.push(layer);
  838. return layer;
  839. };
  840. GMaps.prototype.loadFromKML = function(options) {
  841. var layer = this.getFromKML(options);
  842. layer.setMap(this.map);
  843. return layer;
  844. };
  845. GMaps.prototype.addLayer = function(layerName, options) {
  846. //var default_layers = ['weather', 'clouds', 'traffic', 'transit', 'bicycling', 'panoramio', 'places'];
  847. options = options || {};
  848. var layer;
  849. switch(layerName) {
  850. case 'weather': this.singleLayers.weather = layer = new google.maps.weather.WeatherLayer();
  851. break;
  852. case 'clouds': this.singleLayers.clouds = layer = new google.maps.weather.CloudLayer();
  853. break;
  854. case 'traffic': this.singleLayers.traffic = layer = new google.maps.TrafficLayer();
  855. break;
  856. case 'transit': this.singleLayers.transit = layer = new google.maps.TransitLayer();
  857. break;
  858. case 'bicycling': this.singleLayers.bicycling = layer = new google.maps.BicyclingLayer();
  859. break;
  860. case 'panoramio':
  861. this.singleLayers.panoramio = layer = new google.maps.panoramio.PanoramioLayer();
  862. layer.setTag(options.filter);
  863. delete options.filter;
  864. //click event
  865. if (options.click) {
  866. google.maps.event.addListener(layer, 'click', function(event) {
  867. options.click(event);
  868. delete options.click;
  869. });
  870. }
  871. break;
  872. case 'places':
  873. this.singleLayers.places = layer = new google.maps.places.PlacesService(this.map);
  874. //search and nearbySearch callback, Both are the same
  875. if (options.search || options.nearbySearch) {
  876. var placeSearchRequest = {
  877. bounds : options.bounds || null,
  878. keyword : options.keyword || null,
  879. location : options.location || null,
  880. name : options.name || null,
  881. radius : options.radius || null,
  882. rankBy : options.rankBy || null,
  883. types : options.types || null
  884. };
  885. if (options.search) {
  886. layer.search(placeSearchRequest, options.search);
  887. }
  888. if (options.nearbySearch) {
  889. layer.nearbySearch(placeSearchRequest, options.nearbySearch);
  890. }
  891. }
  892. //textSearch callback
  893. if (options.textSearch) {
  894. var textSearchRequest = {
  895. bounds : options.bounds || null,
  896. location : options.location || null,
  897. query : options.query || null,
  898. radius : options.radius || null
  899. };
  900. layer.textSearch(textSearchRequest, options.textSearch);
  901. }
  902. break;
  903. }
  904. if (layer !== undefined) {
  905. if (typeof layer.setOptions == 'function') {
  906. layer.setOptions(options);
  907. }
  908. if (typeof layer.setMap == 'function') {
  909. layer.setMap(this.map);
  910. }
  911. return layer;
  912. }
  913. };
  914. GMaps.prototype.removeLayer = function(layer) {
  915. if (typeof(layer) == "string" && this.singleLayers[layer] !== undefined) {
  916. this.singleLayers[layer].setMap(null);
  917. delete this.singleLayers[layer];
  918. }
  919. else {
  920. for (var i = 0; i < this.layers.length; i++) {
  921. if (this.layers[i] === layer) {
  922. this.layers[i].setMap(null);
  923. this.layers.splice(i, 1);
  924. break;
  925. }
  926. }
  927. }
  928. };
  929. var travelMode, unitSystem;
  930. GMaps.prototype.getRoutes = function(options) {
  931. switch (options.travelMode) {
  932. case 'bicycling':
  933. travelMode = google.maps.TravelMode.BICYCLING;
  934. break;
  935. case 'transit':
  936. travelMode = google.maps.TravelMode.TRANSIT;
  937. break;
  938. case 'driving':
  939. travelMode = google.maps.TravelMode.DRIVING;
  940. break;
  941. default:
  942. travelMode = google.maps.TravelMode.WALKING;
  943. break;
  944. }
  945. if (options.unitSystem === 'imperial') {
  946. unitSystem = google.maps.UnitSystem.IMPERIAL;
  947. }
  948. else {
  949. unitSystem = google.maps.UnitSystem.METRIC;
  950. }
  951. var base_options = {
  952. avoidHighways: false,
  953. avoidTolls: false,
  954. optimizeWaypoints: false,
  955. waypoints: []
  956. },
  957. request_options = extend_object(base_options, options);
  958. request_options.origin = /string/.test(typeof options.origin) ? options.origin : new google.maps.LatLng(options.origin[0], options.origin[1]);
  959. request_options.destination = /string/.test(typeof options.destination) ? options.destination : new google.maps.LatLng(options.destination[0], options.destination[1]);
  960. request_options.travelMode = travelMode;
  961. request_options.unitSystem = unitSystem;
  962. delete request_options.callback;
  963. var self = this,
  964. service = new google.maps.DirectionsService();
  965. service.route(request_options, function(result, status) {
  966. if (status === google.maps.DirectionsStatus.OK) {
  967. for (var r in result.routes) {
  968. if (result.routes.hasOwnProperty(r)) {
  969. self.routes.push(result.routes[r]);
  970. }
  971. }
  972. }
  973. if (options.callback) {
  974. options.callback(self.routes);
  975. }
  976. });
  977. };
  978. GMaps.prototype.removeRoutes = function() {
  979. this.routes = [];
  980. };
  981. GMaps.prototype.getElevations = function(options) {
  982. options = extend_object({
  983. locations: [],
  984. path : false,
  985. samples : 256
  986. }, options);
  987. if (options.locations.length > 0) {
  988. if (options.locations[0].length > 0) {
  989. options.locations = array_flat(array_map([options.locations], arrayToLatLng, false));
  990. }
  991. }
  992. var callback = options.callback;
  993. delete options.callback;
  994. var service = new google.maps.ElevationService();
  995. //location request
  996. if (!options.path) {
  997. delete options.path;
  998. delete options.samples;
  999. service.getElevationForLocations(options, function(result, status) {
  1000. if (callback && typeof(callback) === "function") {
  1001. callback(result, status);
  1002. }
  1003. });
  1004. //path request
  1005. } else {
  1006. var pathRequest = {
  1007. path : options.locations,
  1008. samples : options.samples
  1009. };
  1010. service.getElevationAlongPath(pathRequest, function(result, status) {
  1011. if (callback && typeof(callback) === "function") {
  1012. callback(result, status);
  1013. }
  1014. });
  1015. }
  1016. };
  1017. GMaps.prototype.cleanRoute = GMaps.prototype.removePolylines;
  1018. GMaps.prototype.drawRoute = function(options) {
  1019. var self = this;
  1020. this.getRoutes({
  1021. origin: options.origin,
  1022. destination: options.destination,
  1023. travelMode: options.travelMode,
  1024. waypoints: options.waypoints,
  1025. unitSystem: options.unitSystem,
  1026. callback: function(e) {
  1027. if (e.length > 0) {
  1028. self.drawPolyline({
  1029. path: e[e.length - 1].overview_path,
  1030. strokeColor: options.strokeColor,
  1031. strokeOpacity: options.strokeOpacity,
  1032. strokeWeight: options.strokeWeight
  1033. });
  1034. if (options.callback) {
  1035. options.callback(e[e.length - 1]);
  1036. }
  1037. }
  1038. }
  1039. });
  1040. };
  1041. GMaps.prototype.travelRoute = function(options) {
  1042. if (options.origin && options.destination) {
  1043. this.getRoutes({
  1044. origin: options.origin,
  1045. destination: options.destination,
  1046. travelMode: options.travelMode,
  1047. waypoints : options.waypoints,
  1048. callback: function(e) {
  1049. //start callback
  1050. if (e.length > 0 && options.start) {
  1051. options.start(e[e.length - 1]);
  1052. }
  1053. //step callback
  1054. if (e.length > 0 && options.step) {
  1055. var route = e[e.length - 1];
  1056. if (route.legs.length > 0) {
  1057. var steps = route.legs[0].steps;
  1058. for (var i=0, step; step=steps[i]; i++) {
  1059. step.step_number = i;
  1060. options.step(step, (route.legs[0].steps.length - 1));
  1061. }
  1062. }
  1063. }
  1064. //end callback
  1065. if (e.length > 0 && options.end) {
  1066. options.end(e[e.length - 1]);
  1067. }
  1068. }
  1069. });
  1070. }
  1071. else if (options.route) {
  1072. if (options.route.legs.length > 0) {
  1073. var steps = options.route.legs[0].steps;
  1074. for (var i=0, step; step=steps[i]; i++) {
  1075. step.step_number = i;
  1076. options.step(step);
  1077. }
  1078. }
  1079. }
  1080. };
  1081. GMaps.prototype.drawSteppedRoute = function(options) {
  1082. var self = this;
  1083. if (options.origin && options.destination) {
  1084. this.getRoutes({
  1085. origin: options.origin,
  1086. destination: options.destination,
  1087. travelMode: options.travelMode,
  1088. waypoints : options.waypoints,
  1089. callback: function(e) {
  1090. //start callback
  1091. if (e.length > 0 && options.start) {
  1092. options.start(e[e.length - 1]);
  1093. }
  1094. //step callback
  1095. if (e.length > 0 && options.step) {
  1096. var route = e[e.length - 1];
  1097. if (route.legs.length > 0) {
  1098. var steps = route.legs[0].steps;
  1099. for (var i=0, step; step=steps[i]; i++) {
  1100. step.step_number = i;
  1101. self.drawPolyline({
  1102. path: step.path,
  1103. strokeColor: options.strokeColor,
  1104. strokeOpacity: options.strokeOpacity,
  1105. strokeWeight: options.strokeWeight
  1106. });
  1107. options.step(step, (route.legs[0].steps.length - 1));
  1108. }
  1109. }
  1110. }
  1111. //end callback
  1112. if (e.length > 0 && options.end) {
  1113. options.end(e[e.length - 1]);
  1114. }
  1115. }
  1116. });
  1117. }
  1118. else if (options.route) {
  1119. if (options.route.legs.length > 0) {
  1120. var steps = options.route.legs[0].steps;
  1121. for (var i=0, step; step=steps[i]; i++) {
  1122. step.step_number = i;
  1123. self.drawPolyline({
  1124. path: step.path,
  1125. strokeColor: options.strokeColor,
  1126. strokeOpacity: options.strokeOpacity,
  1127. strokeWeight: options.strokeWeight
  1128. });
  1129. options.step(step);
  1130. }
  1131. }
  1132. }
  1133. };
  1134. GMaps.Route = function(options) {
  1135. this.origin = options.origin;
  1136. this.destination = options.destination;
  1137. this.waypoints = options.waypoints;
  1138. this.map = options.map;
  1139. this.route = options.route;
  1140. this.step_count = 0;
  1141. this.steps = this.route.legs[0].steps;
  1142. this.steps_length = this.steps.length;
  1143. this.polyline = this.map.drawPolyline({
  1144. path: new google.maps.MVCArray(),
  1145. strokeColor: options.strokeColor,
  1146. strokeOpacity: options.strokeOpacity,
  1147. strokeWeight: options.strokeWeight
  1148. }).getPath();
  1149. };
  1150. GMaps.Route.prototype.getRoute = function(options) {
  1151. var self = this;
  1152. this.map.getRoutes({
  1153. origin : this.origin,
  1154. destination : this.destination,
  1155. travelMode : options.travelMode,
  1156. waypoints : this.waypoints || [],
  1157. callback : function() {
  1158. self.route = e[0];
  1159. if (options.callback) {
  1160. options.callback.call(self);
  1161. }
  1162. }
  1163. });
  1164. };
  1165. GMaps.Route.prototype.back = function() {
  1166. if (this.step_count > 0) {
  1167. this.step_count--;
  1168. var path = this.route.legs[0].steps[this.step_count].path;
  1169. for (var p in path){
  1170. if (path.hasOwnProperty(p)){
  1171. this.polyline.pop();
  1172. }
  1173. }
  1174. }
  1175. };
  1176. GMaps.Route.prototype.forward = function() {
  1177. if (this.step_count < this.steps_length) {
  1178. var path = this.route.legs[0].steps[this.step_count].path;
  1179. for (var p in path){
  1180. if (path.hasOwnProperty(p)){
  1181. this.polyline.push(path[p]);
  1182. }
  1183. }
  1184. this.step_count++;
  1185. }
  1186. };
  1187. GMaps.prototype.checkGeofence = function(lat, lng, fence) {
  1188. return fence.containsLatLng(new google.maps.LatLng(lat, lng));
  1189. };
  1190. GMaps.prototype.checkMarkerGeofence = function(marker, outside_callback) {
  1191. if (marker.fences) {
  1192. for (var i = 0, fence; fence = marker.fences[i]; i++) {
  1193. var pos = marker.getPosition();
  1194. if (!this.checkGeofence(pos.lat(), pos.lng(), fence)) {
  1195. outside_callback(marker, fence);
  1196. }
  1197. }
  1198. }
  1199. };
  1200. GMaps.prototype.toImage = function(options) {
  1201. var options = options || {},
  1202. static_map_options = {};
  1203. static_map_options['size'] = options['size'] || [this.el.clientWidth, this.el.clientHeight];
  1204. static_map_options['lat'] = this.getCenter().lat();
  1205. static_map_options['lng'] = this.getCenter().lng();
  1206. if (this.markers.length > 0) {
  1207. static_map_options['markers'] = [];
  1208. for (var i = 0; i < this.markers.length; i++) {
  1209. static_map_options['markers'].push({
  1210. lat: this.markers[i].getPosition().lat(),
  1211. lng: this.markers[i].getPosition().lng()
  1212. });
  1213. }
  1214. }
  1215. if (this.polylines.length > 0) {
  1216. var polyline = this.polylines[0];
  1217. static_map_options['polyline'] = {};
  1218. static_map_options['polyline']['path'] = google.maps.geometry.encoding.encodePath(polyline.getPath());
  1219. static_map_options['polyline']['strokeColor'] = polyline.strokeColor
  1220. static_map_options['polyline']['strokeOpacity'] = polyline.strokeOpacity
  1221. static_map_options['polyline']['strokeWeight'] = polyline.strokeWeight
  1222. }
  1223. return GMaps.staticMapURL(static_map_options);
  1224. };
  1225. GMaps.staticMapURL = function(options){
  1226. var parameters = [],
  1227. data,
  1228. static_root = 'http://maps.googleapis.com/maps/api/staticmap';
  1229. if (options.url) {
  1230. static_root = options.url;
  1231. delete options.url;
  1232. }
  1233. static_root += '?';
  1234. var markers = options.markers;
  1235. delete options.markers;
  1236. if (!markers && options.marker) {
  1237. markers = [options.marker];
  1238. delete options.marker;
  1239. }
  1240. var polyline = options.polyline;
  1241. delete options.polyline;
  1242. /** Map options **/
  1243. if (options.center) {
  1244. parameters.push('center=' + options.center);
  1245. delete options.center;
  1246. }
  1247. else if (options.address) {
  1248. parameters.push('center=' + options.address);
  1249. delete options.address;
  1250. }
  1251. else if (options.lat) {
  1252. parameters.push(['center=', options.lat, ',', options.lng].join(''));
  1253. delete options.lat;
  1254. delete options.lng;
  1255. }
  1256. else if (options.visible) {
  1257. var visible = encodeURI(options.visible.join('|'));
  1258. parameters.push('visible=' + visible);
  1259. }
  1260. var size = options.size;
  1261. if (size) {
  1262. if (size.join) {
  1263. size = size.join('x');
  1264. }
  1265. delete options.size;
  1266. }
  1267. else {
  1268. size = '630x300';
  1269. }
  1270. parameters.push('size=' + size);
  1271. if (!options.zoom) {
  1272. options.zoom = 15;
  1273. }
  1274. var sensor = options.hasOwnProperty('sensor') ? !!options.sensor : true;
  1275. delete options.sensor;
  1276. parameters.push('sensor=' + sensor);
  1277. for (var param in options) {
  1278. if (options.hasOwnProperty(param)) {
  1279. parameters.push(param + '=' + options[param]);
  1280. }
  1281. }
  1282. /** Markers **/
  1283. if (markers) {
  1284. var marker, loc;
  1285. for (var i=0; data=markers[i]; i++) {
  1286. marker = [];
  1287. if (data.size && data.size !== 'normal') {
  1288. marker.push('size:' + data.size);
  1289. }
  1290. else if (data.icon) {
  1291. marker.push('icon:' + encodeURI(data.icon));
  1292. }
  1293. if (data.color) {
  1294. marker.push('color:' + data.color.replace('#', '0x'));
  1295. }
  1296. if (data.label) {
  1297. marker.push('label:' + data.label[0].toUpperCase());
  1298. }
  1299. loc = (data.address ? data.address : data.lat + ',' + data.lng);
  1300. if (marker.length || i === 0) {
  1301. marker.push(loc);
  1302. marker = marker.join('|');
  1303. parameters.push('markers=' + encodeURI(marker));
  1304. }
  1305. // New marker without styles
  1306. else {
  1307. marker = parameters.pop() + encodeURI('|' + loc);
  1308. parameters.push(marker);
  1309. }
  1310. }
  1311. }
  1312. /** Polylines **/
  1313. function parseColor(color, opacity) {
  1314. if (color[0] === '#'){
  1315. color = color.replace('#', '0x');
  1316. if (opacity) {
  1317. opacity = parseFloat(opacity);
  1318. opacity = Math.min(1, Math.max(opacity, 0));
  1319. if (opacity === 0) {
  1320. return '0x00000000';
  1321. }
  1322. opacity = (opacity * 255).toString(16);
  1323. if (opacity.length === 1) {
  1324. opacity += opacity;
  1325. }
  1326. color = color.slice(0,8) + opacity;
  1327. }
  1328. }
  1329. return color;
  1330. }
  1331. if (polyline) {
  1332. data = polyline;
  1333. polyline = [];
  1334. if (data.strokeWeight) {
  1335. polyline.push('weight:' + parseInt(data.strokeWeight, 10));
  1336. }
  1337. if (data.strokeColor) {
  1338. var color = parseColor(data.strokeColor, data.strokeOpacity);
  1339. polyline.push('color:' + color);
  1340. }
  1341. if (data.fillColor) {
  1342. var fillcolor = parseColor(data.fillColor, data.fillOpacity);
  1343. polyline.push('fillcolor:' + fillcolor);
  1344. }
  1345. var path = data.path;
  1346. if (path.join) {
  1347. for (var j=0, pos; pos=path[j]; j++) {
  1348. polyline.push(pos.join(','));
  1349. }
  1350. }
  1351. else {
  1352. polyline.push('enc:' + path);
  1353. }
  1354. polyline = polyline.join('|');
  1355. parameters.push('path=' + encodeURI(polyline));
  1356. }
  1357. parameters = parameters.join('&');
  1358. return static_root + parameters;
  1359. };
  1360. GMaps.prototype.addMapType = function(mapTypeId, options) {
  1361. if (options.hasOwnProperty("getTileUrl") && typeof(options["getTileUrl"]) == "function") {
  1362. options.tileSize = options.tileSize || new google.maps.Size(256, 256);
  1363. var mapType = new google.maps.ImageMapType(options);
  1364. this.map.mapTypes.set(mapTypeId, mapType);
  1365. }
  1366. else {
  1367. throw "'getTileUrl' function required.";
  1368. }
  1369. };
  1370. GMaps.prototype.addOverlayMapType = function(options) {
  1371. if (options.hasOwnProperty("getTile") && typeof(options["getTile"]) == "function") {
  1372. var overlayMapTypeIndex = options.index;
  1373. delete options.index;
  1374. this.map.overlayMapTypes.insertAt(overlayMapTypeIndex, options);
  1375. }
  1376. else {
  1377. throw "'getTile' function required.";
  1378. }
  1379. };
  1380. GMaps.prototype.removeOverlayMapType = function(overlayMapTypeIndex) {
  1381. this.map.overlayMapTypes.removeAt(overlayMapTypeIndex);
  1382. };
  1383. GMaps.prototype.addStyle = function(options) {
  1384. var styledMapType = new google.maps.StyledMapType(options.styles, options.styledMapName);
  1385. this.map.mapTypes.set(options.mapTypeId, styledMapType);
  1386. };
  1387. GMaps.prototype.setStyle = function(mapTypeId) {
  1388. this.map.setMapTypeId(mapTypeId);
  1389. };
  1390. GMaps.prototype.createPanorama = function(streetview_options) {
  1391. if (!streetview_options.hasOwnProperty('lat') || !streetview_options.hasOwnProperty('lng')) {
  1392. streetview_options.lat = this.getCenter().lat();
  1393. streetview_options.lng = this.getCenter().lng();
  1394. }
  1395. this.panorama = GMaps.createPanorama(streetview_options);
  1396. this.map.setStreetView(this.panorama);
  1397. return this.panorama;
  1398. };
  1399. GMaps.createPanorama = function(options) {
  1400. var el = getElementById(options.el, options.context);
  1401. options.position = new google.maps.LatLng(options.lat, options.lng);
  1402. delete options.el;
  1403. delete options.context;
  1404. delete options.lat;
  1405. delete options.lng;
  1406. var streetview_events = ['closeclick', 'links_changed', 'pano_changed', 'position_changed', 'pov_changed', 'resize', 'visible_changed'],
  1407. streetview_options = extend_object({visible : true}, options);
  1408. for (var i = 0; i < streetview_events.length; i++) {
  1409. delete streetview_options[streetview_events[i]];
  1410. }
  1411. var panorama = new google.maps.StreetViewPanorama(el, streetview_options);
  1412. for (var i = 0; i < streetview_events.length; i++) {
  1413. (function(object, name) {
  1414. if (options[name]) {
  1415. google.maps.event.addListener(object, name, function(){
  1416. options[name].apply(this);
  1417. });
  1418. }
  1419. })(panorama, streetview_events[i]);
  1420. }
  1421. return panorama;
  1422. };
  1423. GMaps.prototype.on = function(event_name, handler) {
  1424. return GMaps.on(event_name, this, handler);
  1425. };
  1426. GMaps.prototype.off = function(event_name) {
  1427. GMaps.off(event_name, this);
  1428. };
  1429. GMaps.custom_events = ['marker_added', 'marker_removed', 'polyline_added', 'polyline_removed', 'polygon_added', 'polygon_removed', 'geolocated', 'geolocation_failed'];
  1430. GMaps.on = function(event_name, object, handler) {
  1431. if (GMaps.custom_events.indexOf(event_name) == -1) {
  1432. return google.maps.event.addListener(object, event_name, handler);
  1433. }
  1434. else {
  1435. var registered_event = {
  1436. handler : handler,
  1437. eventName : event_name
  1438. };
  1439. object.registered_events[event_name] = object.registered_events[event_name] || [];
  1440. object.registered_events[event_name].push(registered_event);
  1441. return registered_event;
  1442. }
  1443. };
  1444. GMaps.off = function(event_name, object) {
  1445. if (GMaps.custom_events.indexOf(event_name) == -1) {
  1446. google.maps.event.clearListeners(object, event_name);
  1447. }
  1448. else {
  1449. object.registered_events[event_name] = [];
  1450. }
  1451. };
  1452. GMaps.fire = function(event_name, object, scope) {
  1453. if (GMaps.custom_events.indexOf(event_name) == -1) {
  1454. google.maps.event.trigger(object, event_name, Array.prototype.slice.apply(arguments).slice(2));
  1455. }
  1456. else {
  1457. if(event_name in scope.registered_events) {
  1458. var firing_events = scope.registered_events[event_name];
  1459. for(var i = 0; i < firing_events.length; i++) {
  1460. (function(handler, scope, object) {
  1461. handler.apply(scope, [object]);
  1462. })(firing_events[i]['handler'], scope, object);
  1463. }
  1464. }
  1465. }
  1466. };
  1467. GMaps.geolocate = function(options) {
  1468. var complete_callback = options.always || options.complete;
  1469. if (navigator.geolocation) {
  1470. navigator.geolocation.getCurrentPosition(function(position) {
  1471. options.success(position);
  1472. if (complete_callback) {
  1473. complete_callback();
  1474. }
  1475. }, function(error) {
  1476. options.error(error);
  1477. if (complete_callback) {
  1478. complete_callback();
  1479. }
  1480. }, options.options);
  1481. }
  1482. else {
  1483. options.not_supported();
  1484. if (complete_callback) {
  1485. complete_callback();
  1486. }
  1487. }
  1488. };
  1489. GMaps.geocode = function(options) {
  1490. this.geocoder = new google.maps.Geocoder();
  1491. var callback = options.callback;
  1492. if (options.hasOwnProperty('lat') && options.hasOwnProperty('lng')) {
  1493. options.latLng = new google.maps.LatLng(options.lat, options.lng);
  1494. }
  1495. delete options.lat;
  1496. delete options.lng;
  1497. delete options.callback;
  1498. this.geocoder.geocode(options, function(results, status) {
  1499. callback(results, status);
  1500. });
  1501. };
  1502. //==========================
  1503. // Polygon containsLatLng
  1504. // https://github.com/tparkin/Google-Maps-Point-in-Polygon
  1505. // Poygon getBounds extension - google-maps-extensions
  1506. // http://code.google.com/p/google-maps-extensions/source/browse/google.maps.Polygon.getBounds.js
  1507. if (!google.maps.Polygon.prototype.getBounds) {
  1508. google.maps.Polygon.prototype.getBounds = function(latLng) {
  1509. var bounds = new google.maps.LatLngBounds();
  1510. var paths = this.getPaths();
  1511. var path;
  1512. for (var p = 0; p < paths.getLength(); p++) {
  1513. path = paths.getAt(p);
  1514. for (var i = 0; i < path.getLength(); i++) {
  1515. bounds.extend(path.getAt(i));
  1516. }
  1517. }
  1518. return bounds;
  1519. };
  1520. }
  1521. if (!google.maps.Polygon.prototype.containsLatLng) {
  1522. // Polygon containsLatLng - method to determine if a latLng is within a polygon
  1523. google.maps.Polygon.prototype.containsLatLng = function(latLng) {
  1524. // Exclude points outside of bounds as there is no way they are in the poly
  1525. var bounds = this.getBounds();
  1526. if (bounds !== null && !bounds.contains(latLng)) {
  1527. return false;
  1528. }
  1529. // Raycast point in polygon method
  1530. var inPoly = false;
  1531. var numPaths = this.getPaths().getLength();
  1532. for (var p = 0; p < numPaths; p++) {
  1533. var path = this.getPaths().getAt(p);
  1534. var numPoints = path.getLength();
  1535. var j = numPoints - 1;
  1536. for (var i = 0; i < numPoints; i++) {
  1537. var vertex1 = path.getAt(i);
  1538. var vertex2 = path.getAt(j);
  1539. if (vertex1.lng() < latLng.lng() && vertex2.lng() >= latLng.lng() || vertex2.lng() < latLng.lng() && vertex1.lng() >= latLng.lng()) {
  1540. if (vertex1.lat() + (latLng.lng() - vertex1.lng()) / (vertex2.lng() - vertex1.lng()) * (vertex2.lat() - vertex1.lat()) < latLng.lat()) {
  1541. inPoly = !inPoly;
  1542. }
  1543. }
  1544. j = i;
  1545. }
  1546. }
  1547. return inPoly;
  1548. };
  1549. }
  1550. google.maps.LatLngBounds.prototype.containsLatLng = function(latLng) {
  1551. return this.contains(latLng);
  1552. };
  1553. google.maps.Marker.prototype.setFences = function(fences) {
  1554. this.fences = fences;
  1555. };
  1556. google.maps.Marker.prototype.addFence = function(fence) {
  1557. this.fences.push(fence);
  1558. };
  1559. google.maps.Marker.prototype.getId = function() {
  1560. return this['__gm_id'];
  1561. };
  1562. //==========================
  1563. // Array indexOf
  1564. // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/indexOf
  1565. if (!Array.prototype.indexOf) {
  1566. Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
  1567. "use strict";
  1568. if (this == null) {
  1569. throw new TypeError();
  1570. }
  1571. var t = Object(this);
  1572. var len = t.length >>> 0;
  1573. if (len === 0) {
  1574. return -1;
  1575. }
  1576. var n = 0;
  1577. if (arguments.length > 1) {
  1578. n = Number(arguments[1]);
  1579. if (n != n) { // shortcut for verifying if it's NaN
  1580. n = 0;
  1581. } else if (n != 0 && n != Infinity && n != -Infinity) {
  1582. n = (n > 0 || -1) * Math.floor(Math.abs(n));
  1583. }
  1584. }
  1585. if (n >= len) {
  1586. return -1;
  1587. }
  1588. var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
  1589. for (; k < len; k++) {
  1590. if (k in t && t[k] === searchElement) {
  1591. return k;
  1592. }
  1593. }
  1594. return -1;
  1595. }
  1596. }