gmaps.js 56 KB

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