source : ng-map.js

  1. /**
  2. * @ngdoc provider
  3. * @name NgMap
  4. * @description
  5. * common utility service for ng-map
  6. */
  7. (function() {
  8. 'use strict';
  9. var $window, $document, $q;
  10. var NavigatorGeolocation, Attr2MapOptions, GeoCoder, camelCaseFilter, NgMapPool;
  11. var mapControllers = {};
  12. var getStyle = function(el, styleProp) {
  13. var y;
  14. if (el.currentStyle) {
  15. y = el.currentStyle[styleProp];
  16. } else if ($window.getComputedStyle) {
  17. y = $document.defaultView.
  18. getComputedStyle(el, null).
  19. getPropertyValue(styleProp);
  20. }
  21. return y;
  22. };
  23. /**
  24. * @memberof NgMap
  25. * @function initMap
  26. * @param id optional, id of the map. default 0
  27. */
  28. var initMap = function(id) {
  29. var ctrl = mapControllers[id || 0];
  30. if (!(ctrl.map instanceof google.maps.Map)) {
  31. ctrl.initializeMap();
  32. return ctrl.map;
  33. } else {
  34. console.error('map is already instialized');
  35. }
  36. };
  37. /**
  38. * @memberof NgMap
  39. * @function getMap
  40. * @param {String} optional, id e.g., 'foo'
  41. * @returns promise
  42. */
  43. var getMap = function(id, options) {
  44. options = options || {};
  45. id = typeof id === 'object' ? id.id : id;
  46. var deferred = $q.defer();
  47. var timeout = options.timeout || 10000;
  48. function waitForMap(timeElapsed){
  49. var keys = Object.keys(mapControllers);
  50. var theFirstController = mapControllers[keys[0]];
  51. if(id && mapControllers[id]){
  52. deferred.resolve(mapControllers[id].map);
  53. } else if (!id && theFirstController && theFirstController.map) {
  54. deferred.resolve(theFirstController.map);
  55. } else if (timeElapsed > timeout) {
  56. deferred.reject('could not find map');
  57. } else {
  58. $window.setTimeout( function(){
  59. waitForMap(timeElapsed+100);
  60. }, 100);
  61. }
  62. }
  63. waitForMap(0);
  64. return deferred.promise;
  65. };
  66. /**
  67. * @memberof NgMap
  68. * @function addMap
  69. * @param mapController {__MapContoller} a map controller
  70. * @returns promise
  71. */
  72. var addMap = function(mapCtrl) {
  73. if (mapCtrl.map) {
  74. var len = Object.keys(mapControllers).length;
  75. mapControllers[mapCtrl.map.id || len] = mapCtrl;
  76. }
  77. };
  78. /**
  79. * @memberof NgMap
  80. * @function deleteMap
  81. * @param mapController {__MapContoller} a map controller
  82. */
  83. var deleteMap = function(mapCtrl) {
  84. var len = Object.keys(mapControllers).length - 1;
  85. var mapId = mapCtrl.map.id || len;
  86. if (mapCtrl.map) {
  87. for (var eventName in mapCtrl.eventListeners) {
  88. console.log('clearing map events', eventName);
  89. var listener = mapCtrl.eventListeners[eventName];
  90. google.maps.event.removeListener(listener);
  91. }
  92. if (mapCtrl.map.controls) {
  93. mapCtrl.map.controls.forEach(function(ctrl) {
  94. ctrl.clear();
  95. });
  96. }
  97. }
  98. //Remove Heatmap Layers
  99. if (mapCtrl.map.heatmapLayers) {
  100. Object.keys(mapCtrl.map.heatmapLayers).forEach(function (layer) {
  101. mapCtrl.deleteObject('heatmapLayers', mapCtrl.map.heatmapLayers[layer]);
  102. });
  103. }
  104. NgMapPool.deleteMapInstance(mapId);
  105. delete mapControllers[mapId];
  106. };
  107. /**
  108. * @memberof NgMap
  109. * @function getGeoLocation
  110. * @param {String} address
  111. * @param {Hash} options geo options
  112. * @returns promise
  113. */
  114. var getGeoLocation = function(string, options) {
  115. var deferred = $q.defer();
  116. if (!string || string.match(/^current/i)) { // current location
  117. NavigatorGeolocation.getCurrentPosition(options).then(
  118. function(position) {
  119. var lat = position.coords.latitude;
  120. var lng = position.coords.longitude;
  121. var latLng = new google.maps.LatLng(lat,lng);
  122. deferred.resolve(latLng);
  123. },
  124. function(error) {
  125. deferred.reject(error);
  126. }
  127. );
  128. } else {
  129. GeoCoder.geocode({address: string}).then(
  130. function(results) {
  131. deferred.resolve(results[0].geometry.location);
  132. },
  133. function(error) {
  134. deferred.reject(error);
  135. }
  136. );
  137. // var geocoder = new google.maps.Geocoder();
  138. // geocoder.geocode(options, function (results, status) {
  139. // if (status == google.maps.GeocoderStatus.OK) {
  140. // deferred.resolve(results);
  141. // } else {
  142. // deferred.reject(status);
  143. // }
  144. // });
  145. }
  146. return deferred.promise;
  147. };
  148. /**
  149. * @memberof NgMap
  150. * @function observeAndSet
  151. * @param {String} attrName attribute name
  152. * @param {Object} object A Google maps object to be changed
  153. * @returns attribue observe function
  154. */
  155. var observeAndSet = function(attrName, object) {
  156. console.log('observing', attrName, 'on object', object);
  157. return function(val) {
  158. if (val) {
  159. var setMethod = camelCaseFilter('set-'+attrName);
  160. var optionValue = Attr2MapOptions.toOptionValue(val, {key: attrName});
  161. if (object[setMethod]) { //if set method does exist
  162. console.log('observing', attrName, 'and setting', optionValue);
  163. /* if an location is being observed */
  164. if (attrName.match(/center|position/) &&
  165. typeof optionValue == 'string') {
  166. getGeoLocation(optionValue).then(function(latlng) {
  167. object[setMethod](latlng);
  168. });
  169. } else {
  170. object[setMethod](optionValue);
  171. }
  172. }
  173. }
  174. };
  175. };
  176. /**
  177. * @memberof NgMap
  178. * @function setStyle
  179. * @param {HtmlElement} map contriner element
  180. * @desc set display, width, height of map container element
  181. */
  182. var setStyle = function(el) {
  183. //if style is not given to the map element, set display and height
  184. var defaultStyle = el.getAttribute('default-style');
  185. if (defaultStyle == "true") {
  186. el.style.display = 'block';
  187. el.style.height = '300px';
  188. } else {
  189. if (getStyle(el, 'display') != "block") {
  190. el.style.display = 'block';
  191. }
  192. if (getStyle(el, 'height').match(/^(0|auto)/)) {
  193. el.style.height = '300px';
  194. }
  195. }
  196. };
  197. angular.module('ngMap').provider('NgMap', function() {
  198. var defaultOptions = {};
  199. /**
  200. * @memberof NgMap
  201. * @function setDefaultOptions
  202. * @param {Hash} options
  203. * @example
  204. * app.config(function(NgMapProvider) {
  205. * NgMapProvider.setDefaultOptions({
  206. * marker: {
  207. * optimized: false
  208. * }
  209. * });
  210. * });
  211. */
  212. this.setDefaultOptions = function(options) {
  213. defaultOptions = options;
  214. };
  215. var NgMap = function(
  216. _$window_, _$document_, _$q_,
  217. _NavigatorGeolocation_, _Attr2MapOptions_,
  218. _GeoCoder_, _camelCaseFilter_, _NgMapPool_
  219. ) {
  220. $window = _$window_;
  221. $document = _$document_[0];
  222. $q = _$q_;
  223. NavigatorGeolocation = _NavigatorGeolocation_;
  224. Attr2MapOptions = _Attr2MapOptions_;
  225. GeoCoder = _GeoCoder_;
  226. camelCaseFilter = _camelCaseFilter_;
  227. NgMapPool = _NgMapPool_;
  228. return {
  229. defaultOptions: defaultOptions,
  230. addMap: addMap,
  231. deleteMap: deleteMap,
  232. getMap: getMap,
  233. initMap: initMap,
  234. setStyle: setStyle,
  235. getGeoLocation: getGeoLocation,
  236. observeAndSet: observeAndSet
  237. };
  238. };
  239. NgMap.$inject = [
  240. '$window', '$document', '$q',
  241. 'NavigatorGeolocation', 'Attr2MapOptions',
  242. 'GeoCoder', 'camelCaseFilter', 'NgMapPool'
  243. ];
  244. this.$get = NgMap;
  245. });
  246. })();