Keine Beschreibung

googlemaps.js 58KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151
  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.16
  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. var marker = this.markers[i];
  577. marker.setMap(null);
  578. if(this.markerClusterer) {
  579. this.markerClusterer.removeMarker(marker);
  580. }
  581. GMaps.fire('marker_removed', marker, this);
  582. }
  583. this.markers = new_markers;
  584. }
  585. else {
  586. for (var i = 0; i < collection.length; i++) {
  587. var index = this.markers.indexOf(collection[i]);
  588. if (index > -1) {
  589. var marker = this.markers[index];
  590. marker.setMap(null);
  591. if(this.markerClusterer) {
  592. this.markerClusterer.removeMarker(marker);
  593. }
  594. GMaps.fire('marker_removed', marker, this);
  595. }
  596. }
  597. for (var i = 0; i < this.markers.length; i++) {
  598. var marker = this.markers[i];
  599. if (marker.getMap() != null) {
  600. new_markers.push(marker);
  601. }
  602. }
  603. this.markers = new_markers;
  604. }
  605. };
  606. GMaps.prototype.drawOverlay = function(options) {
  607. var overlay = new google.maps.OverlayView(),
  608. auto_show = true;
  609. overlay.setMap(this.map);
  610. if (options.auto_show != null) {
  611. auto_show = options.auto_show;
  612. }
  613. overlay.onAdd = function() {
  614. var el = document.createElement('div');
  615. el.style.borderStyle = "none";
  616. el.style.borderWidth = "0px";
  617. el.style.position = "absolute";
  618. el.style.zIndex = 100;
  619. el.innerHTML = options.content;
  620. overlay.el = el;
  621. if (!options.layer) {
  622. options.layer = 'overlayLayer';
  623. }
  624. var panes = this.getPanes(),
  625. overlayLayer = panes[options.layer],
  626. stop_overlay_events = ['contextmenu', 'DOMMouseScroll', 'dblclick', 'mousedown'];
  627. overlayLayer.appendChild(el);
  628. for (var ev = 0; ev < stop_overlay_events.length; ev++) {
  629. (function(object, name) {
  630. google.maps.event.addDomListener(object, name, function(e){
  631. if (navigator.userAgent.toLowerCase().indexOf('msie') != -1 && document.all) {
  632. e.cancelBubble = true;
  633. e.returnValue = false;
  634. }
  635. else {
  636. e.stopPropagation();
  637. }
  638. });
  639. })(el, stop_overlay_events[ev]);
  640. }
  641. if (options.click) {
  642. panes.overlayMouseTarget.appendChild(overlay.el);
  643. google.maps.event.addDomListener(overlay.el, 'click', function() {
  644. options.click.apply(overlay, [overlay]);
  645. });
  646. }
  647. google.maps.event.trigger(this, 'ready');
  648. };
  649. overlay.draw = function() {
  650. var projection = this.getProjection(),
  651. pixel = projection.fromLatLngToDivPixel(new google.maps.LatLng(options.lat, options.lng));
  652. options.horizontalOffset = options.horizontalOffset || 0;
  653. options.verticalOffset = options.verticalOffset || 0;
  654. var el = overlay.el,
  655. content = el.children[0],
  656. content_height = content.clientHeight,
  657. content_width = content.clientWidth;
  658. switch (options.verticalAlign) {
  659. case 'top':
  660. el.style.top = (pixel.y - content_height + options.verticalOffset) + 'px';
  661. break;
  662. default:
  663. case 'middle':
  664. el.style.top = (pixel.y - (content_height / 2) + options.verticalOffset) + 'px';
  665. break;
  666. case 'bottom':
  667. el.style.top = (pixel.y + options.verticalOffset) + 'px';
  668. break;
  669. }
  670. switch (options.horizontalAlign) {
  671. case 'left':
  672. el.style.left = (pixel.x - content_width + options.horizontalOffset) + 'px';
  673. break;
  674. default:
  675. case 'center':
  676. el.style.left = (pixel.x - (content_width / 2) + options.horizontalOffset) + 'px';
  677. break;
  678. case 'right':
  679. el.style.left = (pixel.x + options.horizontalOffset) + 'px';
  680. break;
  681. }
  682. el.style.display = auto_show ? 'block' : 'none';
  683. if (!auto_show) {
  684. options.show.apply(this, [el]);
  685. }
  686. };
  687. overlay.onRemove = function() {
  688. var el = overlay.el;
  689. if (options.remove) {
  690. options.remove.apply(this, [el]);
  691. }
  692. else {
  693. overlay.el.parentNode.removeChild(overlay.el);
  694. overlay.el = null;
  695. }
  696. };
  697. this.overlays.push(overlay);
  698. return overlay;
  699. };
  700. GMaps.prototype.removeOverlay = function(overlay) {
  701. for (var i = 0; i < this.overlays.length; i++) {
  702. if (this.overlays[i] === overlay) {
  703. this.overlays[i].setMap(null);
  704. this.overlays.splice(i, 1);
  705. break;
  706. }
  707. }
  708. };
  709. GMaps.prototype.removeOverlays = function() {
  710. for (var i = 0, item; item = this.overlays[i]; i++) {
  711. item.setMap(null);
  712. }
  713. this.overlays = [];
  714. };
  715. GMaps.prototype.drawPolyline = function(options) {
  716. var path = [],
  717. points = options.path;
  718. if (points.length) {
  719. if (points[0][0] === undefined) {
  720. path = points;
  721. }
  722. else {
  723. for (var i=0, latlng; latlng=points[i]; i++) {
  724. path.push(new google.maps.LatLng(latlng[0], latlng[1]));
  725. }
  726. }
  727. }
  728. var polyline_options = {
  729. map: this.map,
  730. path: path,
  731. strokeColor: options.strokeColor,
  732. strokeOpacity: options.strokeOpacity,
  733. strokeWeight: options.strokeWeight,
  734. geodesic: options.geodesic,
  735. clickable: true,
  736. editable: false,
  737. visible: true
  738. };
  739. if (options.hasOwnProperty("clickable")) {
  740. polyline_options.clickable = options.clickable;
  741. }
  742. if (options.hasOwnProperty("editable")) {
  743. polyline_options.editable = options.editable;
  744. }
  745. if (options.hasOwnProperty("icons")) {
  746. polyline_options.icons = options.icons;
  747. }
  748. if (options.hasOwnProperty("zIndex")) {
  749. polyline_options.zIndex = options.zIndex;
  750. }
  751. var polyline = new google.maps.Polyline(polyline_options);
  752. var polyline_events = ['click', 'dblclick', 'mousedown', 'mousemove', 'mouseout', 'mouseover', 'mouseup', 'rightclick'];
  753. for (var ev = 0; ev < polyline_events.length; ev++) {
  754. (function(object, name) {
  755. if (options[name]) {
  756. google.maps.event.addListener(object, name, function(e){
  757. options[name].apply(this, [e]);
  758. });
  759. }
  760. })(polyline, polyline_events[ev]);
  761. }
  762. this.polylines.push(polyline);
  763. GMaps.fire('polyline_added', polyline, this);
  764. return polyline;
  765. };
  766. GMaps.prototype.removePolyline = function(polyline) {
  767. for (var i = 0; i < this.polylines.length; i++) {
  768. if (this.polylines[i] === polyline) {
  769. this.polylines[i].setMap(null);
  770. this.polylines.splice(i, 1);
  771. GMaps.fire('polyline_removed', polyline, this);
  772. break;
  773. }
  774. }
  775. };
  776. GMaps.prototype.removePolylines = function() {
  777. for (var i = 0, item; item = this.polylines[i]; i++) {
  778. item.setMap(null);
  779. }
  780. this.polylines = [];
  781. };
  782. GMaps.prototype.drawCircle = function(options) {
  783. options = extend_object({
  784. map: this.map,
  785. center: new google.maps.LatLng(options.lat, options.lng)
  786. }, options);
  787. delete options.lat;
  788. delete options.lng;
  789. var polygon = new google.maps.Circle(options),
  790. polygon_events = ['click', 'dblclick', 'mousedown', 'mousemove', 'mouseout', 'mouseover', 'mouseup', 'rightclick'];
  791. for (var ev = 0; ev < polygon_events.length; ev++) {
  792. (function(object, name) {
  793. if (options[name]) {
  794. google.maps.event.addListener(object, name, function(e){
  795. options[name].apply(this, [e]);
  796. });
  797. }
  798. })(polygon, polygon_events[ev]);
  799. }
  800. this.polygons.push(polygon);
  801. return polygon;
  802. };
  803. GMaps.prototype.drawRectangle = function(options) {
  804. options = extend_object({
  805. map: this.map
  806. }, options);
  807. var latLngBounds = new google.maps.LatLngBounds(
  808. new google.maps.LatLng(options.bounds[0][0], options.bounds[0][1]),
  809. new google.maps.LatLng(options.bounds[1][0], options.bounds[1][1])
  810. );
  811. options.bounds = latLngBounds;
  812. var polygon = new google.maps.Rectangle(options),
  813. polygon_events = ['click', 'dblclick', 'mousedown', 'mousemove', 'mouseout', 'mouseover', 'mouseup', 'rightclick'];
  814. for (var ev = 0; ev < polygon_events.length; ev++) {
  815. (function(object, name) {
  816. if (options[name]) {
  817. google.maps.event.addListener(object, name, function(e){
  818. options[name].apply(this, [e]);
  819. });
  820. }
  821. })(polygon, polygon_events[ev]);
  822. }
  823. this.polygons.push(polygon);
  824. return polygon;
  825. };
  826. GMaps.prototype.drawPolygon = function(options) {
  827. var useGeoJSON = false;
  828. if(options.hasOwnProperty("useGeoJSON")) {
  829. useGeoJSON = options.useGeoJSON;
  830. }
  831. delete options.useGeoJSON;
  832. options = extend_object({
  833. map: this.map
  834. }, options);
  835. if (useGeoJSON == false) {
  836. options.paths = [options.paths.slice(0)];
  837. }
  838. if (options.paths.length > 0) {
  839. if (options.paths[0].length > 0) {
  840. options.paths = array_flat(array_map(options.paths, arrayToLatLng, useGeoJSON));
  841. }
  842. }
  843. var polygon = new google.maps.Polygon(options),
  844. polygon_events = ['click', 'dblclick', 'mousedown', 'mousemove', 'mouseout', 'mouseover', 'mouseup', 'rightclick'];
  845. for (var ev = 0; ev < polygon_events.length; ev++) {
  846. (function(object, name) {
  847. if (options[name]) {
  848. google.maps.event.addListener(object, name, function(e){
  849. options[name].apply(this, [e]);
  850. });
  851. }
  852. })(polygon, polygon_events[ev]);
  853. }
  854. this.polygons.push(polygon);
  855. GMaps.fire('polygon_added', polygon, this);
  856. return polygon;
  857. };
  858. GMaps.prototype.removePolygon = function(polygon) {
  859. for (var i = 0; i < this.polygons.length; i++) {
  860. if (this.polygons[i] === polygon) {
  861. this.polygons[i].setMap(null);
  862. this.polygons.splice(i, 1);
  863. GMaps.fire('polygon_removed', polygon, this);
  864. break;
  865. }
  866. }
  867. };
  868. GMaps.prototype.removePolygons = function() {
  869. for (var i = 0, item; item = this.polygons[i]; i++) {
  870. item.setMap(null);
  871. }
  872. this.polygons = [];
  873. };
  874. GMaps.prototype.getFromFusionTables = function(options) {
  875. var events = options.events;
  876. delete options.events;
  877. var fusion_tables_options = options,
  878. layer = new google.maps.FusionTablesLayer(fusion_tables_options);
  879. for (var ev in events) {
  880. (function(object, name) {
  881. google.maps.event.addListener(object, name, function(e) {
  882. events[name].apply(this, [e]);
  883. });
  884. })(layer, ev);
  885. }
  886. this.layers.push(layer);
  887. return layer;
  888. };
  889. GMaps.prototype.loadFromFusionTables = function(options) {
  890. var layer = this.getFromFusionTables(options);
  891. layer.setMap(this.map);
  892. return layer;
  893. };
  894. GMaps.prototype.getFromKML = function(options) {
  895. var url = options.url,
  896. events = options.events;
  897. delete options.url;
  898. delete options.events;
  899. var kml_options = options,
  900. layer = new google.maps.KmlLayer(url, kml_options);
  901. for (var ev in events) {
  902. (function(object, name) {
  903. google.maps.event.addListener(object, name, function(e) {
  904. events[name].apply(this, [e]);
  905. });
  906. })(layer, ev);
  907. }
  908. this.layers.push(layer);
  909. return layer;
  910. };
  911. GMaps.prototype.loadFromKML = function(options) {
  912. var layer = this.getFromKML(options);
  913. layer.setMap(this.map);
  914. return layer;
  915. };
  916. GMaps.prototype.addLayer = function(layerName, options) {
  917. //var default_layers = ['weather', 'clouds', 'traffic', 'transit', 'bicycling', 'panoramio', 'places'];
  918. options = options || {};
  919. var layer;
  920. switch(layerName) {
  921. case 'weather': this.singleLayers.weather = layer = new google.maps.weather.WeatherLayer();
  922. break;
  923. case 'clouds': this.singleLayers.clouds = layer = new google.maps.weather.CloudLayer();
  924. break;
  925. case 'traffic': this.singleLayers.traffic = layer = new google.maps.TrafficLayer();
  926. break;
  927. case 'transit': this.singleLayers.transit = layer = new google.maps.TransitLayer();
  928. break;
  929. case 'bicycling': this.singleLayers.bicycling = layer = new google.maps.BicyclingLayer();
  930. break;
  931. case 'panoramio':
  932. this.singleLayers.panoramio = layer = new google.maps.panoramio.PanoramioLayer();
  933. layer.setTag(options.filter);
  934. delete options.filter;
  935. //click event
  936. if (options.click) {
  937. google.maps.event.addListener(layer, 'click', function(event) {
  938. options.click(event);
  939. delete options.click;
  940. });
  941. }
  942. break;
  943. case 'places':
  944. this.singleLayers.places = layer = new google.maps.places.PlacesService(this.map);
  945. //search, nearbySearch, radarSearch callback, Both are the same
  946. if (options.search || options.nearbySearch || options.radarSearch) {
  947. var placeSearchRequest = {
  948. bounds : options.bounds || null,
  949. keyword : options.keyword || null,
  950. location : options.location || null,
  951. name : options.name || null,
  952. radius : options.radius || null,
  953. rankBy : options.rankBy || null,
  954. types : options.types || null
  955. };
  956. if (options.radarSearch) {
  957. layer.radarSearch(placeSearchRequest, options.radarSearch);
  958. }
  959. if (options.search) {
  960. layer.search(placeSearchRequest, options.search);
  961. }
  962. if (options.nearbySearch) {
  963. layer.nearbySearch(placeSearchRequest, options.nearbySearch);
  964. }
  965. }
  966. //textSearch callback
  967. if (options.textSearch) {
  968. var textSearchRequest = {
  969. bounds : options.bounds || null,
  970. location : options.location || null,
  971. query : options.query || null,
  972. radius : options.radius || null
  973. };
  974. layer.textSearch(textSearchRequest, options.textSearch);
  975. }
  976. break;
  977. }
  978. if (layer !== undefined) {
  979. if (typeof layer.setOptions == 'function') {
  980. layer.setOptions(options);
  981. }
  982. if (typeof layer.setMap == 'function') {
  983. layer.setMap(this.map);
  984. }
  985. return layer;
  986. }
  987. };
  988. GMaps.prototype.removeLayer = function(layer) {
  989. if (typeof(layer) == "string" && this.singleLayers[layer] !== undefined) {
  990. this.singleLayers[layer].setMap(null);
  991. delete this.singleLayers[layer];
  992. }
  993. else {
  994. for (var i = 0; i < this.layers.length; i++) {
  995. if (this.layers[i] === layer) {
  996. this.layers[i].setMap(null);
  997. this.layers.splice(i, 1);
  998. break;
  999. }
  1000. }
  1001. }
  1002. };
  1003. var travelMode, unitSystem;
  1004. GMaps.prototype.getRoutes = function(options) {
  1005. switch (options.travelMode) {
  1006. case 'bicycling':
  1007. travelMode = google.maps.TravelMode.BICYCLING;
  1008. break;
  1009. case 'transit':
  1010. travelMode = google.maps.TravelMode.TRANSIT;
  1011. break;
  1012. case 'driving':
  1013. travelMode = google.maps.TravelMode.DRIVING;
  1014. break;
  1015. default:
  1016. travelMode = google.maps.TravelMode.WALKING;
  1017. break;
  1018. }
  1019. if (options.unitSystem === 'imperial') {
  1020. unitSystem = google.maps.UnitSystem.IMPERIAL;
  1021. }
  1022. else {
  1023. unitSystem = google.maps.UnitSystem.METRIC;
  1024. }
  1025. var base_options = {
  1026. avoidHighways: false,
  1027. avoidTolls: false,
  1028. optimizeWaypoints: false,
  1029. waypoints: []
  1030. },
  1031. request_options = extend_object(base_options, options);
  1032. request_options.origin = /string/.test(typeof options.origin) ? options.origin : new google.maps.LatLng(options.origin[0], options.origin[1]);
  1033. request_options.destination = /string/.test(typeof options.destination) ? options.destination : new google.maps.LatLng(options.destination[0], options.destination[1]);
  1034. request_options.travelMode = travelMode;
  1035. request_options.unitSystem = unitSystem;
  1036. delete request_options.callback;
  1037. delete request_options.error;
  1038. var self = this,
  1039. service = new google.maps.DirectionsService();
  1040. service.route(request_options, function(result, status) {
  1041. if (status === google.maps.DirectionsStatus.OK) {
  1042. for (var r in result.routes) {
  1043. if (result.routes.hasOwnProperty(r)) {
  1044. self.routes.push(result.routes[r]);
  1045. }
  1046. }
  1047. if (options.callback) {
  1048. options.callback(self.routes);
  1049. }
  1050. }
  1051. else {
  1052. if (options.error) {
  1053. options.error(result, status);
  1054. }
  1055. }
  1056. });
  1057. };
  1058. GMaps.prototype.removeRoutes = function() {
  1059. this.routes = [];
  1060. };
  1061. GMaps.prototype.getElevations = function(options) {
  1062. options = extend_object({
  1063. locations: [],
  1064. path : false,
  1065. samples : 256
  1066. }, options);
  1067. if (options.locations.length > 0) {
  1068. if (options.locations[0].length > 0) {
  1069. options.locations = array_flat(array_map([options.locations], arrayToLatLng, false));
  1070. }
  1071. }
  1072. var callback = options.callback;
  1073. delete options.callback;
  1074. var service = new google.maps.ElevationService();
  1075. //location request
  1076. if (!options.path) {
  1077. delete options.path;
  1078. delete options.samples;
  1079. service.getElevationForLocations(options, function(result, status) {
  1080. if (callback && typeof(callback) === "function") {
  1081. callback(result, status);
  1082. }
  1083. });
  1084. //path request
  1085. } else {
  1086. var pathRequest = {
  1087. path : options.locations,
  1088. samples : options.samples
  1089. };
  1090. service.getElevationAlongPath(pathRequest, function(result, status) {
  1091. if (callback && typeof(callback) === "function") {
  1092. callback(result, status);
  1093. }
  1094. });
  1095. }
  1096. };
  1097. GMaps.prototype.cleanRoute = GMaps.prototype.removePolylines;
  1098. GMaps.prototype.drawRoute = function(options) {
  1099. var self = this;
  1100. this.getRoutes({
  1101. origin: options.origin,
  1102. destination: options.destination,
  1103. travelMode: options.travelMode,
  1104. waypoints: options.waypoints,
  1105. unitSystem: options.unitSystem,
  1106. error: options.error,
  1107. callback: function(e) {
  1108. if (e.length > 0) {
  1109. self.drawPolyline({
  1110. path: e[e.length - 1].overview_path,
  1111. strokeColor: options.strokeColor,
  1112. strokeOpacity: options.strokeOpacity,
  1113. strokeWeight: options.strokeWeight
  1114. });
  1115. if (options.callback) {
  1116. options.callback(e[e.length - 1]);
  1117. }
  1118. }
  1119. }
  1120. });
  1121. };
  1122. GMaps.prototype.travelRoute = function(options) {
  1123. if (options.origin && options.destination) {
  1124. this.getRoutes({
  1125. origin: options.origin,
  1126. destination: options.destination,
  1127. travelMode: options.travelMode,
  1128. waypoints : options.waypoints,
  1129. unitSystem: options.unitSystem,
  1130. error: options.error,
  1131. callback: function(e) {
  1132. //start callback
  1133. if (e.length > 0 && options.start) {
  1134. options.start(e[e.length - 1]);
  1135. }
  1136. //step callback
  1137. if (e.length > 0 && options.step) {
  1138. var route = e[e.length - 1];
  1139. if (route.legs.length > 0) {
  1140. var steps = route.legs[0].steps;
  1141. for (var i=0, step; step=steps[i]; i++) {
  1142. step.step_number = i;
  1143. options.step(step, (route.legs[0].steps.length - 1));
  1144. }
  1145. }
  1146. }
  1147. //end callback
  1148. if (e.length > 0 && options.end) {
  1149. options.end(e[e.length - 1]);
  1150. }
  1151. }
  1152. });
  1153. }
  1154. else if (options.route) {
  1155. if (options.route.legs.length > 0) {
  1156. var steps = options.route.legs[0].steps;
  1157. for (var i=0, step; step=steps[i]; i++) {
  1158. step.step_number = i;
  1159. options.step(step);
  1160. }
  1161. }
  1162. }
  1163. };
  1164. GMaps.prototype.drawSteppedRoute = function(options) {
  1165. var self = this;
  1166. if (options.origin && options.destination) {
  1167. this.getRoutes({
  1168. origin: options.origin,
  1169. destination: options.destination,
  1170. travelMode: options.travelMode,
  1171. waypoints : options.waypoints,
  1172. error: options.error,
  1173. callback: function(e) {
  1174. //start callback
  1175. if (e.length > 0 && options.start) {
  1176. options.start(e[e.length - 1]);
  1177. }
  1178. //step callback
  1179. if (e.length > 0 && options.step) {
  1180. var route = e[e.length - 1];
  1181. if (route.legs.length > 0) {
  1182. var steps = route.legs[0].steps;
  1183. for (var i=0, step; step=steps[i]; i++) {
  1184. step.step_number = i;
  1185. self.drawPolyline({
  1186. path: step.path,
  1187. strokeColor: options.strokeColor,
  1188. strokeOpacity: options.strokeOpacity,
  1189. strokeWeight: options.strokeWeight
  1190. });
  1191. options.step(step, (route.legs[0].steps.length - 1));
  1192. }
  1193. }
  1194. }
  1195. //end callback
  1196. if (e.length > 0 && options.end) {
  1197. options.end(e[e.length - 1]);
  1198. }
  1199. }
  1200. });
  1201. }
  1202. else if (options.route) {
  1203. if (options.route.legs.length > 0) {
  1204. var steps = options.route.legs[0].steps;
  1205. for (var i=0, step; step=steps[i]; i++) {
  1206. step.step_number = i;
  1207. self.drawPolyline({
  1208. path: step.path,
  1209. strokeColor: options.strokeColor,
  1210. strokeOpacity: options.strokeOpacity,
  1211. strokeWeight: options.strokeWeight
  1212. });
  1213. options.step(step);
  1214. }
  1215. }
  1216. }
  1217. };
  1218. GMaps.Route = function(options) {
  1219. this.origin = options.origin;
  1220. this.destination = options.destination;
  1221. this.waypoints = options.waypoints;
  1222. this.map = options.map;
  1223. this.route = options.route;
  1224. this.step_count = 0;
  1225. this.steps = this.route.legs[0].steps;
  1226. this.steps_length = this.steps.length;
  1227. this.polyline = this.map.drawPolyline({
  1228. path: new google.maps.MVCArray(),
  1229. strokeColor: options.strokeColor,
  1230. strokeOpacity: options.strokeOpacity,
  1231. strokeWeight: options.strokeWeight
  1232. }).getPath();
  1233. };
  1234. GMaps.Route.prototype.getRoute = function(options) {
  1235. var self = this;
  1236. this.map.getRoutes({
  1237. origin : this.origin,
  1238. destination : this.destination,
  1239. travelMode : options.travelMode,
  1240. waypoints : this.waypoints || [],
  1241. error: options.error,
  1242. callback : function() {
  1243. self.route = e[0];
  1244. if (options.callback) {
  1245. options.callback.call(self);
  1246. }
  1247. }
  1248. });
  1249. };
  1250. GMaps.Route.prototype.back = function() {
  1251. if (this.step_count > 0) {
  1252. this.step_count--;
  1253. var path = this.route.legs[0].steps[this.step_count].path;
  1254. for (var p in path){
  1255. if (path.hasOwnProperty(p)){
  1256. this.polyline.pop();
  1257. }
  1258. }
  1259. }
  1260. };
  1261. GMaps.Route.prototype.forward = function() {
  1262. if (this.step_count < this.steps_length) {
  1263. var path = this.route.legs[0].steps[this.step_count].path;
  1264. for (var p in path){
  1265. if (path.hasOwnProperty(p)){
  1266. this.polyline.push(path[p]);
  1267. }
  1268. }
  1269. this.step_count++;
  1270. }
  1271. };
  1272. GMaps.prototype.checkGeofence = function(lat, lng, fence) {
  1273. return fence.containsLatLng(new google.maps.LatLng(lat, lng));
  1274. };
  1275. GMaps.prototype.checkMarkerGeofence = function(marker, outside_callback) {
  1276. if (marker.fences) {
  1277. for (var i = 0, fence; fence = marker.fences[i]; i++) {
  1278. var pos = marker.getPosition();
  1279. if (!this.checkGeofence(pos.lat(), pos.lng(), fence)) {
  1280. outside_callback(marker, fence);
  1281. }
  1282. }
  1283. }
  1284. };
  1285. GMaps.prototype.toImage = function(options) {
  1286. var options = options || {},
  1287. static_map_options = {};
  1288. static_map_options['size'] = options['size'] || [this.el.clientWidth, this.el.clientHeight];
  1289. static_map_options['lat'] = this.getCenter().lat();
  1290. static_map_options['lng'] = this.getCenter().lng();
  1291. if (this.markers.length > 0) {
  1292. static_map_options['markers'] = [];
  1293. for (var i = 0; i < this.markers.length; i++) {
  1294. static_map_options['markers'].push({
  1295. lat: this.markers[i].getPosition().lat(),
  1296. lng: this.markers[i].getPosition().lng()
  1297. });
  1298. }
  1299. }
  1300. if (this.polylines.length > 0) {
  1301. var polyline = this.polylines[0];
  1302. static_map_options['polyline'] = {};
  1303. static_map_options['polyline']['path'] = google.maps.geometry.encoding.encodePath(polyline.getPath());
  1304. static_map_options['polyline']['strokeColor'] = polyline.strokeColor
  1305. static_map_options['polyline']['strokeOpacity'] = polyline.strokeOpacity
  1306. static_map_options['polyline']['strokeWeight'] = polyline.strokeWeight
  1307. }
  1308. return GMaps.staticMapURL(static_map_options);
  1309. };
  1310. GMaps.staticMapURL = function(options){
  1311. var parameters = [],
  1312. data,
  1313. static_root = 'http://maps.googleapis.com/maps/api/staticmap';
  1314. if (options.url) {
  1315. static_root = options.url;
  1316. delete options.url;
  1317. }
  1318. static_root += '?';
  1319. var markers = options.markers;
  1320. delete options.markers;
  1321. if (!markers && options.marker) {
  1322. markers = [options.marker];
  1323. delete options.marker;
  1324. }
  1325. var styles = options.styles;
  1326. delete options.styles;
  1327. var polyline = options.polyline;
  1328. delete options.polyline;
  1329. /** Map options **/
  1330. if (options.center) {
  1331. parameters.push('center=' + options.center);
  1332. delete options.center;
  1333. }
  1334. else if (options.address) {
  1335. parameters.push('center=' + options.address);
  1336. delete options.address;
  1337. }
  1338. else if (options.lat) {
  1339. parameters.push(['center=', options.lat, ',', options.lng].join(''));
  1340. delete options.lat;
  1341. delete options.lng;
  1342. }
  1343. else if (options.visible) {
  1344. var visible = encodeURI(options.visible.join('|'));
  1345. parameters.push('visible=' + visible);
  1346. }
  1347. var size = options.size;
  1348. if (size) {
  1349. if (size.join) {
  1350. size = size.join('x');
  1351. }
  1352. delete options.size;
  1353. }
  1354. else {
  1355. size = '630x300';
  1356. }
  1357. parameters.push('size=' + size);
  1358. if (!options.zoom && options.zoom !== false) {
  1359. options.zoom = 15;
  1360. }
  1361. var sensor = options.hasOwnProperty('sensor') ? !!options.sensor : true;
  1362. delete options.sensor;
  1363. parameters.push('sensor=' + sensor);
  1364. for (var param in options) {
  1365. if (options.hasOwnProperty(param)) {
  1366. parameters.push(param + '=' + options[param]);
  1367. }
  1368. }
  1369. /** Markers **/
  1370. if (markers) {
  1371. var marker, loc;
  1372. for (var i=0; data=markers[i]; i++) {
  1373. marker = [];
  1374. if (data.size && data.size !== 'normal') {
  1375. marker.push('size:' + data.size);
  1376. delete data.size;
  1377. }
  1378. else if (data.icon) {
  1379. marker.push('icon:' + encodeURI(data.icon));
  1380. delete data.icon;
  1381. }
  1382. if (data.color) {
  1383. marker.push('color:' + data.color.replace('#', '0x'));
  1384. delete data.color;
  1385. }
  1386. if (data.label) {
  1387. marker.push('label:' + data.label[0].toUpperCase());
  1388. delete data.label;
  1389. }
  1390. loc = (data.address ? data.address : data.lat + ',' + data.lng);
  1391. delete data.address;
  1392. delete data.lat;
  1393. delete data.lng;
  1394. for(var param in data){
  1395. if (data.hasOwnProperty(param)) {
  1396. marker.push(param + ':' + data[param]);
  1397. }
  1398. }
  1399. if (marker.length || i === 0) {
  1400. marker.push(loc);
  1401. marker = marker.join('|');
  1402. parameters.push('markers=' + encodeURI(marker));
  1403. }
  1404. // New marker without styles
  1405. else {
  1406. marker = parameters.pop() + encodeURI('|' + loc);
  1407. parameters.push(marker);
  1408. }
  1409. }
  1410. }
  1411. /** Map Styles **/
  1412. if (styles) {
  1413. for (var i = 0; i < styles.length; i++) {
  1414. var styleRule = [];
  1415. if (styles[i].featureType){
  1416. styleRule.push('feature:' + styles[i].featureType.toLowerCase());
  1417. }
  1418. if (styles[i].elementType) {
  1419. styleRule.push('element:' + styles[i].elementType.toLowerCase());
  1420. }
  1421. for (var j = 0; j < styles[i].stylers.length; j++) {
  1422. for (var p in styles[i].stylers[j]) {
  1423. var ruleArg = styles[i].stylers[j][p];
  1424. if (p == 'hue' || p == 'color') {
  1425. ruleArg = '0x' + ruleArg.substring(1);
  1426. }
  1427. styleRule.push(p + ':' + ruleArg);
  1428. }
  1429. }
  1430. var rule = styleRule.join('|');
  1431. if (rule != '') {
  1432. parameters.push('style=' + rule);
  1433. }
  1434. }
  1435. }
  1436. /** Polylines **/
  1437. function parseColor(color, opacity) {
  1438. if (color[0] === '#'){
  1439. color = color.replace('#', '0x');
  1440. if (opacity) {
  1441. opacity = parseFloat(opacity);
  1442. opacity = Math.min(1, Math.max(opacity, 0));
  1443. if (opacity === 0) {
  1444. return '0x00000000';
  1445. }
  1446. opacity = (opacity * 255).toString(16);
  1447. if (opacity.length === 1) {
  1448. opacity += opacity;
  1449. }
  1450. color = color.slice(0,8) + opacity;
  1451. }
  1452. }
  1453. return color;
  1454. }
  1455. if (polyline) {
  1456. data = polyline;
  1457. polyline = [];
  1458. if (data.strokeWeight) {
  1459. polyline.push('weight:' + parseInt(data.strokeWeight, 10));
  1460. }
  1461. if (data.strokeColor) {
  1462. var color = parseColor(data.strokeColor, data.strokeOpacity);
  1463. polyline.push('color:' + color);
  1464. }
  1465. if (data.fillColor) {
  1466. var fillcolor = parseColor(data.fillColor, data.fillOpacity);
  1467. polyline.push('fillcolor:' + fillcolor);
  1468. }
  1469. var path = data.path;
  1470. if (path.join) {
  1471. for (var j=0, pos; pos=path[j]; j++) {
  1472. polyline.push(pos.join(','));
  1473. }
  1474. }
  1475. else {
  1476. polyline.push('enc:' + path);
  1477. }
  1478. polyline = polyline.join('|');
  1479. parameters.push('path=' + encodeURI(polyline));
  1480. }
  1481. /** Retina support **/
  1482. var dpi = window.devicePixelRatio || 1;
  1483. parameters.push('scale=' + dpi);
  1484. parameters = parameters.join('&');
  1485. return static_root + parameters;
  1486. };
  1487. GMaps.prototype.addMapType = function(mapTypeId, options) {
  1488. if (options.hasOwnProperty("getTileUrl") && typeof(options["getTileUrl"]) == "function") {
  1489. options.tileSize = options.tileSize || new google.maps.Size(256, 256);
  1490. var mapType = new google.maps.ImageMapType(options);
  1491. this.map.mapTypes.set(mapTypeId, mapType);
  1492. }
  1493. else {
  1494. throw "'getTileUrl' function required.";
  1495. }
  1496. };
  1497. GMaps.prototype.addOverlayMapType = function(options) {
  1498. if (options.hasOwnProperty("getTile") && typeof(options["getTile"]) == "function") {
  1499. var overlayMapTypeIndex = options.index;
  1500. delete options.index;
  1501. this.map.overlayMapTypes.insertAt(overlayMapTypeIndex, options);
  1502. }
  1503. else {
  1504. throw "'getTile' function required.";
  1505. }
  1506. };
  1507. GMaps.prototype.removeOverlayMapType = function(overlayMapTypeIndex) {
  1508. this.map.overlayMapTypes.removeAt(overlayMapTypeIndex);
  1509. };
  1510. GMaps.prototype.addStyle = function(options) {
  1511. var styledMapType = new google.maps.StyledMapType(options.styles, { name: options.styledMapName });
  1512. this.map.mapTypes.set(options.mapTypeId, styledMapType);
  1513. };
  1514. GMaps.prototype.setStyle = function(mapTypeId) {
  1515. this.map.setMapTypeId(mapTypeId);
  1516. };
  1517. GMaps.prototype.createPanorama = function(streetview_options) {
  1518. if (!streetview_options.hasOwnProperty('lat') || !streetview_options.hasOwnProperty('lng')) {
  1519. streetview_options.lat = this.getCenter().lat();
  1520. streetview_options.lng = this.getCenter().lng();
  1521. }
  1522. this.panorama = GMaps.createPanorama(streetview_options);
  1523. this.map.setStreetView(this.panorama);
  1524. return this.panorama;
  1525. };
  1526. GMaps.createPanorama = function(options) {
  1527. var el = getElementById(options.el, options.context);
  1528. options.position = new google.maps.LatLng(options.lat, options.lng);
  1529. delete options.el;
  1530. delete options.context;
  1531. delete options.lat;
  1532. delete options.lng;
  1533. var streetview_events = ['closeclick', 'links_changed', 'pano_changed', 'position_changed', 'pov_changed', 'resize', 'visible_changed'],
  1534. streetview_options = extend_object({visible : true}, options);
  1535. for (var i = 0; i < streetview_events.length; i++) {
  1536. delete streetview_options[streetview_events[i]];
  1537. }
  1538. var panorama = new google.maps.StreetViewPanorama(el, streetview_options);
  1539. for (var i = 0; i < streetview_events.length; i++) {
  1540. (function(object, name) {
  1541. if (options[name]) {
  1542. google.maps.event.addListener(object, name, function(){
  1543. options[name].apply(this);
  1544. });
  1545. }
  1546. })(panorama, streetview_events[i]);
  1547. }
  1548. return panorama;
  1549. };
  1550. GMaps.prototype.on = function(event_name, handler) {
  1551. return GMaps.on(event_name, this, handler);
  1552. };
  1553. GMaps.prototype.off = function(event_name) {
  1554. GMaps.off(event_name, this);
  1555. };
  1556. GMaps.custom_events = ['marker_added', 'marker_removed', 'polyline_added', 'polyline_removed', 'polygon_added', 'polygon_removed', 'geolocated', 'geolocation_failed'];
  1557. GMaps.on = function(event_name, object, handler) {
  1558. if (GMaps.custom_events.indexOf(event_name) == -1) {
  1559. if(object instanceof GMaps) object = object.map;
  1560. return google.maps.event.addListener(object, event_name, handler);
  1561. }
  1562. else {
  1563. var registered_event = {
  1564. handler : handler,
  1565. eventName : event_name
  1566. };
  1567. object.registered_events[event_name] = object.registered_events[event_name] || [];
  1568. object.registered_events[event_name].push(registered_event);
  1569. return registered_event;
  1570. }
  1571. };
  1572. GMaps.off = function(event_name, object) {
  1573. if (GMaps.custom_events.indexOf(event_name) == -1) {
  1574. if(object instanceof GMaps) object = object.map;
  1575. google.maps.event.clearListeners(object, event_name);
  1576. }
  1577. else {
  1578. object.registered_events[event_name] = [];
  1579. }
  1580. };
  1581. GMaps.fire = function(event_name, object, scope) {
  1582. if (GMaps.custom_events.indexOf(event_name) == -1) {
  1583. google.maps.event.trigger(object, event_name, Array.prototype.slice.apply(arguments).slice(2));
  1584. }
  1585. else {
  1586. if(event_name in scope.registered_events) {
  1587. var firing_events = scope.registered_events[event_name];
  1588. for(var i = 0; i < firing_events.length; i++) {
  1589. (function(handler, scope, object) {
  1590. handler.apply(scope, [object]);
  1591. })(firing_events[i]['handler'], scope, object);
  1592. }
  1593. }
  1594. }
  1595. };
  1596. GMaps.geolocate = function(options) {
  1597. var complete_callback = options.always || options.complete;
  1598. if (navigator.geolocation) {
  1599. navigator.geolocation.getCurrentPosition(function(position) {
  1600. options.success(position);
  1601. if (complete_callback) {
  1602. complete_callback();
  1603. }
  1604. }, function(error) {
  1605. options.error(error);
  1606. if (complete_callback) {
  1607. complete_callback();
  1608. }
  1609. }, options.options);
  1610. }
  1611. else {
  1612. options.not_supported();
  1613. if (complete_callback) {
  1614. complete_callback();
  1615. }
  1616. }
  1617. };
  1618. GMaps.geocode = function(options) {
  1619. this.geocoder = new google.maps.Geocoder();
  1620. var callback = options.callback;
  1621. if (options.hasOwnProperty('lat') && options.hasOwnProperty('lng')) {
  1622. options.latLng = new google.maps.LatLng(options.lat, options.lng);
  1623. }
  1624. delete options.lat;
  1625. delete options.lng;
  1626. delete options.callback;
  1627. this.geocoder.geocode(options, function(results, status) {
  1628. callback(results, status);
  1629. });
  1630. };
  1631. //==========================
  1632. // Polygon containsLatLng
  1633. // https://github.com/tparkin/Google-Maps-Point-in-Polygon
  1634. // Poygon getBounds extension - google-maps-extensions
  1635. // http://code.google.com/p/google-maps-extensions/source/browse/google.maps.Polygon.getBounds.js
  1636. if (!google.maps.Polygon.prototype.getBounds) {
  1637. google.maps.Polygon.prototype.getBounds = function(latLng) {
  1638. var bounds = new google.maps.LatLngBounds();
  1639. var paths = this.getPaths();
  1640. var path;
  1641. for (var p = 0; p < paths.getLength(); p++) {
  1642. path = paths.getAt(p);
  1643. for (var i = 0; i < path.getLength(); i++) {
  1644. bounds.extend(path.getAt(i));
  1645. }
  1646. }
  1647. return bounds;
  1648. };
  1649. }
  1650. if (!google.maps.Polygon.prototype.containsLatLng) {
  1651. // Polygon containsLatLng - method to determine if a latLng is within a polygon
  1652. google.maps.Polygon.prototype.containsLatLng = function(latLng) {
  1653. // Exclude points outside of bounds as there is no way they are in the poly
  1654. var bounds = this.getBounds();
  1655. if (bounds !== null && !bounds.contains(latLng)) {
  1656. return false;
  1657. }
  1658. // Raycast point in polygon method
  1659. var inPoly = false;
  1660. var numPaths = this.getPaths().getLength();
  1661. for (var p = 0; p < numPaths; p++) {
  1662. var path = this.getPaths().getAt(p);
  1663. var numPoints = path.getLength();
  1664. var j = numPoints - 1;
  1665. for (var i = 0; i < numPoints; i++) {
  1666. var vertex1 = path.getAt(i);
  1667. var vertex2 = path.getAt(j);
  1668. if (vertex1.lng() < latLng.lng() && vertex2.lng() >= latLng.lng() || vertex2.lng() < latLng.lng() && vertex1.lng() >= latLng.lng()) {
  1669. if (vertex1.lat() + (latLng.lng() - vertex1.lng()) / (vertex2.lng() - vertex1.lng()) * (vertex2.lat() - vertex1.lat()) < latLng.lat()) {
  1670. inPoly = !inPoly;
  1671. }
  1672. }
  1673. j = i;
  1674. }
  1675. }
  1676. return inPoly;
  1677. };
  1678. }
  1679. if (!google.maps.Circle.prototype.containsLatLng) {
  1680. google.maps.Circle.prototype.containsLatLng = function(latLng) {
  1681. if (google.maps.geometry) {
  1682. return google.maps.geometry.spherical.computeDistanceBetween(this.getCenter(), latLng) <= this.getRadius();
  1683. }
  1684. else {
  1685. return true;
  1686. }
  1687. };
  1688. }
  1689. google.maps.LatLngBounds.prototype.containsLatLng = function(latLng) {
  1690. return this.contains(latLng);
  1691. };
  1692. google.maps.Marker.prototype.setFences = function(fences) {
  1693. this.fences = fences;
  1694. };
  1695. google.maps.Marker.prototype.addFence = function(fence) {
  1696. this.fences.push(fence);
  1697. };
  1698. google.maps.Marker.prototype.getId = function() {
  1699. return this['__gm_id'];
  1700. };
  1701. //==========================
  1702. // Array indexOf
  1703. // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/indexOf
  1704. if (!Array.prototype.indexOf) {
  1705. Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
  1706. "use strict";
  1707. if (this == null) {
  1708. throw new TypeError();
  1709. }
  1710. var t = Object(this);
  1711. var len = t.length >>> 0;
  1712. if (len === 0) {
  1713. return -1;
  1714. }
  1715. var n = 0;
  1716. if (arguments.length > 1) {
  1717. n = Number(arguments[1]);
  1718. if (n != n) { // shortcut for verifying if it's NaN
  1719. n = 0;
  1720. } else if (n != 0 && n != Infinity && n != -Infinity) {
  1721. n = (n > 0 || -1) * Math.floor(Math.abs(n));
  1722. }
  1723. }
  1724. if (n >= len) {
  1725. return -1;
  1726. }
  1727. var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
  1728. for (; k < len; k++) {
  1729. if (k in t && t[k] === searchElement) {
  1730. return k;
  1731. }
  1732. }
  1733. return -1;
  1734. }
  1735. }
  1736. return GMaps;
  1737. }));