gmaps.js 69 KB

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