source : shape.js

  1. /**
  2. * @ngdoc directive
  3. * @name shape
  4. * @param Attr2MapOptions {service} convert html attribute to Google map api options
  5. * @description
  6. * Initialize a Google map shape in map with given options and register events
  7. * The shapes are:
  8. * . circle
  9. * . polygon
  10. * . polyline
  11. * . rectangle
  12. * . groundOverlay(or image)
  13. *
  14. * Requires: map directive
  15. *
  16. * Restrict To: Element
  17. *
  18. * @attr {Boolean} centered if set, map will be centered with this marker
  19. * @attr {Expression} geo-callback if shape is a circle and the center is
  20. * an address, the expression is will be performed when geo-lookup
  21. * is successful. e.g., geo-callback="showDetail()"
  22. * @attr {String} <OPTIONS>
  23. * For circle, [any circle options](https://developers.google.com/maps/documentation/javascript/reference#CircleOptions)
  24. * For polygon, [any polygon options](https://developers.google.com/maps/documentation/javascript/reference#PolygonOptions)
  25. * For polyline, [any polyline options](https://developers.google.com/maps/documentation/javascript/reference#PolylineOptions)
  26. * For rectangle, [any rectangle options](https://developers.google.com/maps/documentation/javascript/reference#RectangleOptions)
  27. * For image, [any groundOverlay options](https://developers.google.com/maps/documentation/javascript/reference#GroundOverlayOptions)
  28. * @attr {String} <MapEvent> [Any Shape events](https://developers.google.com/maps/documentation/javascript/reference)
  29. * @example
  30. * Usage:
  31. * <map MAP_ATTRIBUTES>
  32. * <shape name="SHAPE_NAME ANY_SHAPE_OPTIONS ANY_SHAPE_EVENTS"></shape>
  33. * </map>
  34. *
  35. * Example:
  36. *
  37. * <map zoom="11" center="[40.74, -74.18]">
  38. * <shape id="polyline" name="polyline" geodesic="true"
  39. * stroke-color="#FF0000" stroke-opacity="1.0" stroke-weight="2"
  40. * path="[[40.74,-74.18],[40.64,-74.10],[40.54,-74.05],[40.44,-74]]" >
  41. * </shape>
  42. * </map>
  43. *
  44. * <map zoom="11" center="[40.74, -74.18]">
  45. * <shape id="polygon" name="polygon" stroke-color="#FF0000"
  46. * stroke-opacity="1.0" stroke-weight="2"
  47. * paths="[[40.74,-74.18],[40.64,-74.18],[40.84,-74.08],[40.74,-74.18]]" >
  48. * </shape>
  49. * </map>
  50. *
  51. * <map zoom="11" center="[40.74, -74.18]">
  52. * <shape id="rectangle" name="rectangle" stroke-color='#FF0000'
  53. * stroke-opacity="0.8" stroke-weight="2"
  54. * bounds="[[40.74,-74.18], [40.78,-74.14]]" editable="true" >
  55. * </shape>
  56. * </map>
  57. *
  58. * <map zoom="11" center="[40.74, -74.18]">
  59. * <shape id="circle" name="circle" stroke-color='#FF0000'
  60. * stroke-opacity="0.8"stroke-weight="2"
  61. * center="[40.70,-74.14]" radius="4000" editable="true" >
  62. * </shape>
  63. * </map>
  64. *
  65. * <map zoom="11" center="[40.74, -74.18]">
  66. * <shape id="image" name="image"
  67. * url="https://www.lib.utexas.edu/maps/historical/newark_nj_1922.jpg"
  68. * bounds="[[40.71,-74.22],[40.77,-74.12]]" opacity="0.7"
  69. * clickable="true">
  70. * </shape>
  71. * </map>
  72. *
  73. * For full-working example, please visit
  74. * [shape example](https://rawgit.com/allenhwkim/angularjs-google-maps/master/build/shape.html)
  75. */
  76. /* global google */
  77. (function() {
  78. 'use strict';
  79. var getShape = function(options, events) {
  80. var shape;
  81. var shapeName = options.name;
  82. delete options.name; //remove name bcoz it's not for options
  83. console.log("shape", shapeName, "options", options, 'events', events);
  84. /**
  85. * set options
  86. */
  87. switch(shapeName) {
  88. case "circle":
  89. if (!(options.center instanceof google.maps.LatLng)) {
  90. options.center = new google.maps.LatLng(0,0);
  91. }
  92. shape = new google.maps.Circle(options);
  93. break;
  94. case "polygon":
  95. shape = new google.maps.Polygon(options);
  96. break;
  97. case "polyline":
  98. shape = new google.maps.Polyline(options);
  99. break;
  100. case "rectangle":
  101. shape = new google.maps.Rectangle(options);
  102. break;
  103. case "groundOverlay":
  104. case "image":
  105. var url = options.url;
  106. var opts = {opacity: options.opacity, clickable: options.clickable, id:options.id};
  107. shape = new google.maps.GroundOverlay(url, options.bounds, opts);
  108. break;
  109. }
  110. /**
  111. * set events
  112. */
  113. for (var eventName in events) {
  114. if (events[eventName]) {
  115. google.maps.event.addListener(shape, eventName, events[eventName]);
  116. }
  117. }
  118. return shape;
  119. };
  120. var shape = function(Attr2MapOptions, $parse, NgMap) {
  121. var parser = Attr2MapOptions;
  122. var linkFunc = function(scope, element, attrs, mapController) {
  123. mapController = mapController[0]||mapController[1];
  124. var orgAttrs = parser.orgAttributes(element);
  125. var filtered = parser.filter(attrs);
  126. var shapeOptions = parser.getOptions(filtered, {scope: scope});
  127. var shapeEvents = parser.getEvents(scope, filtered);
  128. var address, shapeType;
  129. shapeType = shapeOptions.name;
  130. if (!(shapeOptions.center instanceof google.maps.LatLng)) {
  131. address = shapeOptions.center;
  132. }
  133. var shape = getShape(shapeOptions, shapeEvents);
  134. mapController.addObject('shapes', shape);
  135. if (address && shapeType == 'circle') {
  136. NgMap.getGeoLocation(address).then(function(latlng) {
  137. shape.setCenter(latlng);
  138. shape.centered && shape.map.setCenter(latlng);
  139. var geoCallback = attrs.geoCallback;
  140. geoCallback && $parse(geoCallback)(scope);
  141. });
  142. }
  143. //set observers
  144. mapController.observeAttrSetObj(orgAttrs, attrs, shape);
  145. element.bind('$destroy', function() {
  146. mapController.deleteObject('shapes', shape);
  147. });
  148. };
  149. return {
  150. restrict: 'E',
  151. require: ['?^map','?^ngMap'],
  152. link: linkFunc
  153. }; // return
  154. };
  155. shape.$inject = ['Attr2MapOptions', '$parse', 'NgMap'];
  156. angular.module('ngMap').directive('shape', shape);
  157. })();