gmaps.js 57 KB

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