map.js.html 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: directives/map.js</title>
  6. <script src="scripts/prettify/prettify.js"> </script>
  7. <script src="scripts/prettify/lang-css.js"> </script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  13. </head>
  14. <body>
  15. <div id="main">
  16. <h1 class="page-title">Source: directives/map.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>/**
  20. * @ngdoc directive
  21. * @name map
  22. * @requires Attr2Options
  23. * @description
  24. * Implementation of {@link MapController}
  25. * Initialize a Google map within a `&lt;div>` tag with given options and register events
  26. * It accepts children directives; marker, shape, or marker-clusterer
  27. *
  28. * It initialize map, children tags, then emits message as soon as the action is done
  29. * The message emitted from this directive is;
  30. * . mapInitialized
  31. *
  32. * Restrict To:
  33. * Element
  34. *
  35. * @param {Array} geo-fallback-center
  36. * The center of map incase geolocation failed. i.e. [0,0]
  37. * @param {String} init-event The name of event to initialize this map.
  38. * If this option is given, the map won't be initialized until the event is received.
  39. * To invoke the event, use $scope.$emit or $scope.$broacast.
  40. * i.e. &lt;map init-event="init-map" ng-click="$emit('init-map')" center=... >&lt;/map>
  41. * @param {String} &amp;lt;MapOption> Any Google map options,
  42. * https://developers.google.com/maps/documentation/javascript/reference?csw=1#MapOptions
  43. * @param {String} &amp;lt;MapEvent> Any Google map events,
  44. * https://rawgit.com/allenhwkim/angularjs-google-maps/master/build/map_events.html
  45. * @example
  46. * Usage:
  47. * &lt;map MAP_OPTIONS_OR_MAP_EVENTS ..>
  48. * ... Any children directives
  49. * &lt;/map>
  50. *
  51. * Example:
  52. * &lt;map center="[40.74, -74.18]" on-click="doThat()">
  53. * &lt;/map>
  54. *
  55. * &lt;map geo-fallback-center="[40.74, -74.18]">
  56. * &lt;/map>
  57. */
  58. /*jshint -W030*/
  59. ngMap.directive('map', ['Attr2Options', '$timeout', function(Attr2Options, $timeout) {
  60. var parser = Attr2Options;
  61. function getStyle(el,styleProp)
  62. {
  63. if (el.currentStyle) {
  64. var y = el.currentStyle[styleProp];
  65. } else if (window.getComputedStyle) {
  66. var y = document.defaultView.getComputedStyle(el,null).getPropertyValue(styleProp);
  67. }
  68. return y;
  69. }
  70. return {
  71. restrict: 'AE',
  72. controller: ngMap.MapController,
  73. /**
  74. * Initialize map and events
  75. * @memberof map
  76. * @param {$scope} scope
  77. * @param {angular.element} element
  78. * @param {Hash} attrs
  79. * @ctrl {MapController} ctrl
  80. */
  81. link: function (scope, element, attrs, ctrl) {
  82. var orgAttrs = parser.orgAttributes(element);
  83. scope.google = google; //used by $scope.eval in Attr2Options to avoid eval()
  84. /**
  85. * create a new `div` inside map tag, so that it does not touch map element
  86. * http://stackoverflow.com/questions/20955356
  87. */
  88. var el = document.createElement("div");
  89. el.style.width = "100%";
  90. el.style.height = "100%";
  91. element.prepend(el);
  92. /**
  93. * if style is not given to the map element, set display and height
  94. */
  95. if (getStyle(element[0], 'display') != "block") {
  96. element.css('display','block');
  97. }
  98. if (getStyle(element[0], 'height').match(/^(0|auto)/)) {
  99. element.css('height','300px');
  100. }
  101. /**
  102. * initialize function
  103. */
  104. var initializeMap = function(mapOptions, mapEvents) {
  105. var map = new google.maps.Map(el, {});
  106. map.markers = {};
  107. map.shapes = {};
  108. /**
  109. * resize the map to prevent showing partially, in case intialized too early
  110. */
  111. $timeout(function() {
  112. google.maps.event.trigger(map, "resize");
  113. });
  114. /**
  115. * set options
  116. */
  117. mapOptions.zoom = mapOptions.zoom || 15;
  118. var center = mapOptions.center;
  119. if (!center) {
  120. mapOptions.center = new google.maps.LatLng(0,0);
  121. } else if (!(center instanceof google.maps.LatLng)) {
  122. delete mapOptions.center;
  123. Attr2Options.setDelayedGeoLocation(map, 'setCenter',
  124. center, {fallbackLocation: options.geoFallbackCenter});
  125. }
  126. map.setOptions(mapOptions);
  127. /**
  128. * set events
  129. */
  130. for (var eventName in mapEvents) {
  131. if (eventName) {
  132. google.maps.event.addListener(map, eventName, mapEvents[eventName]);
  133. }
  134. }
  135. /**
  136. * set observers
  137. */
  138. parser.observeAttrSetObj(orgAttrs, attrs, map);
  139. /**
  140. * set controller and set objects
  141. * so that map can be used by other directives; marker or shape
  142. * ctrl._objects are gathered when marker and shape are initialized before map is set
  143. */
  144. ctrl.map = map; /* so that map can be used by other directives; marker or shape */
  145. ctrl.addObjects(ctrl._objects);
  146. // /* providing method to add a marker used by user scope */
  147. // map.addMarker = ctrl.addMarker;
  148. /**
  149. * set map for scope and controller and broadcast map event
  150. * scope.map will be overwritten if user have multiple maps in a scope,
  151. * thus the last map will be set as scope.map.
  152. * however an `mapInitialized` event will be emitted every time.
  153. */
  154. scope.map = map;
  155. scope.map.scope = scope;
  156. //google.maps.event.addListenerOnce(map, "idle", function() {
  157. scope.$emit('mapInitialized', map);
  158. //});
  159. // the following lines will be deprecated on behalf of mapInitialized
  160. // to collect maps, we should use scope.maps in your own controller, i.e. MyCtrl
  161. scope.maps = scope.maps || {};
  162. scope.maps[options.id||Object.keys(scope.maps).length] = map;
  163. scope.$emit('mapsInitialized', scope.maps);
  164. }; // function initializeMap()
  165. /**
  166. * get map options and events
  167. */
  168. var filtered = parser.filter(attrs);
  169. var options = parser.getOptions(filtered, scope);
  170. var controlOptions = parser.getControlOptions(filtered);
  171. var mapOptions = angular.extend(options, controlOptions);
  172. var mapEvents = parser.getEvents(scope, filtered);
  173. console.log("filtered", filtered, "mapOptions", mapOptions, 'mapEvents', mapEvents);
  174. if (attrs.initEvent) { // allows controlled initialization
  175. scope.$on(attrs.initEvent, function() {
  176. !ctrl.map &amp;&amp; initializeMap(mapOptions, mapEvents); // init if not done
  177. });
  178. } else {
  179. initializeMap(mapOptions, mapEvents);
  180. } // if
  181. }
  182. };
  183. }]);
  184. </code></pre>
  185. </article>
  186. </section>
  187. </div>
  188. <nav>
  189. <h2><a href="index.html">Index</a></h2><h3>service</h3><ul><li><a href="Attr2Options.html">Attr2Options</a></li><li><a href="GeoCoder.html">GeoCoder</a></li><li><a href="NavigatorGeolocation.html">NavigatorGeolocation</a></li><li><a href="StreetView.html">StreetView</a></li></ul><h3>directive</h3><ul><li><a href="bicycling-layer.html">bicycling-layer</a></li><li><a href="cloud-layer.html">cloud-layer</a></li><li><a href="custom-control.html">custom-control</a></li><li><a href="drawing-manager.html">drawing-manager</a></li><li><a href="dynamic-maps-engine-layer.html">dynamic-maps-engine-layer</a></li><li><a href="fusion-tables-layer.html">fusion-tables-layer</a></li><li><a href="heatmap-layer.html">heatmap-layer</a></li><li><a href="info-window.html">info-window</a></li><li><a href="kml-layer.html">kml-layer</a></li><li><a href="lazy-load.html">lazy-load</a></li><li><a href="map.html">map</a></li><li><a href="map-data.html">map-data</a></li><li><a href="map-type.html">map-type</a></li><li><a href="MapController.html">MapController</a></li><li><a href="maps-engine-layer.html">maps-engine-layer</a></li><li><a href="marker.html">marker</a></li><li><a href="overlay-map-type.html">overlay-map-type</a></li><li><a href="shape.html">shape</a></li><li><a href="traffic-layer.html">traffic-layer</a></li><li><a href="transit-layer.html">transit-layer</a></li><li><a href="weather-layer.html">weather-layer</a></li></ul>
  190. </nav>
  191. <br clear="both">
  192. <footer>
  193. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-alpha9</a>
  194. and <a href="https://github.com/allenhwkim/angular-jsdoc">angular-jsdoc</a>
  195. </footer>
  196. <script> prettyPrint(); </script>
  197. <script src="scripts/linenumber.js"> </script>
  198. <script>
  199. var href=window.location.href.match(/\/([^\/]+$)/)[1];
  200. document.querySelector("nav a[href='"+href+"']").scrollIntoView(true);
  201. if (window.location.hash == "")
  202. document.querySelector("body").scrollIntoView(true);
  203. </script>
  204. </body>
  205. </html>