gmaps.js 60 KB

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