source : ng-map-pool.js

  1. /**
  2. * @ngdoc factory
  3. * @name NgMapPool
  4. * @description
  5. * Provide map instance to avoid memory leak
  6. */
  7. (function() {
  8. 'use strict';
  9. /**
  10. * @memberof NgMapPool
  11. * @desc map instance pool
  12. */
  13. var mapInstances = [];
  14. var $window, $document, $timeout;
  15. var add = function(el) {
  16. var mapDiv = $document.createElement("div");
  17. mapDiv.style.width = "100%";
  18. mapDiv.style.height = "100%";
  19. el.appendChild(mapDiv);
  20. var map = new $window.google.maps.Map(mapDiv, {});
  21. mapInstances.push(map);
  22. return map;
  23. };
  24. var findById = function(el, id) {
  25. var notInUseMap;
  26. for (var i=0; i<mapInstances.length; i++) {
  27. var map = mapInstances[i];
  28. if (map.id == id && !map.inUse) {
  29. var mapDiv = map.getDiv();
  30. el.appendChild(mapDiv);
  31. notInUseMap = map;
  32. break;
  33. }
  34. }
  35. return notInUseMap;
  36. };
  37. var findUnused = function(el) { //jshint ignore:line
  38. var notInUseMap;
  39. for (var i=0; i<mapInstances.length; i++) {
  40. var map = mapInstances[i];
  41. if (map.id) {
  42. continue;
  43. }
  44. if (!map.inUse) {
  45. var mapDiv = map.getDiv();
  46. el.appendChild(mapDiv);
  47. notInUseMap = map;
  48. break;
  49. }
  50. }
  51. return notInUseMap;
  52. };
  53. /**
  54. * @memberof NgMapPool
  55. * @function getMapInstance
  56. * @param {HtmlElement} el map container element
  57. * @return map instance for the given element
  58. */
  59. var getMapInstance = function(el) {
  60. var map = findById(el, el.id) || findUnused(el);
  61. if (!map) {
  62. map = add(el);
  63. } else {
  64. /* firing map idle event, which is used by map controller */
  65. $timeout(function() {
  66. google.maps.event.trigger(map, 'idle');
  67. }, 100);
  68. }
  69. map.inUse = true;
  70. return map;
  71. };
  72. /**
  73. * @memberof NgMapPool
  74. * @function returnMapInstance
  75. * @param {Map} an instance of google.maps.Map
  76. * @desc sets the flag inUse of the given map instance to false, so that it
  77. * can be reused later
  78. */
  79. var returnMapInstance = function(map) {
  80. map.inUse = false;
  81. };
  82. /**
  83. * @memberof NgMapPool
  84. * @function resetMapInstances
  85. * @desc resets mapInstance array
  86. */
  87. var resetMapInstances = function() {
  88. for(var i = 0;i < mapInstances.length;i++) {
  89. mapInstances[i] = null;
  90. }
  91. mapInstances = [];
  92. };
  93. /**
  94. * @memberof NgMapPool
  95. * @function deleteMapInstance
  96. * @desc delete a mapInstance
  97. */
  98. var deleteMapInstance= function(mapId) {
  99. for( var i=0; i<mapInstances.length; i++ ) {
  100. if( (mapInstances[i] !== null) && (mapInstances[i].id == mapId)) {
  101. mapInstances[i]= null;
  102. mapInstances.splice( i, 1 );
  103. }
  104. }
  105. };
  106. var NgMapPool = function(_$document_, _$window_, _$timeout_) {
  107. $document = _$document_[0], $window = _$window_, $timeout = _$timeout_;
  108. return {
  109. mapInstances: mapInstances,
  110. resetMapInstances: resetMapInstances,
  111. getMapInstance: getMapInstance,
  112. returnMapInstance: returnMapInstance,
  113. deleteMapInstance: deleteMapInstance
  114. };
  115. };
  116. NgMapPool.$inject = [ '$document', '$window', '$timeout'];
  117. angular.module('ngMap').factory('NgMapPool', NgMapPool);
  118. })();