gmaps.js 55 KB

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