custom-control.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*jshint -W030*/
  2. /**
  3. * @ngdoc directive
  4. * @name custom-control
  5. * @requires Attr2Options
  6. * @requires $compile
  7. * @description
  8. * Build custom control and set to the map with position
  9. *
  10. * Requires: map directive
  11. *
  12. * Restrict To: Element
  13. *
  14. * @param {String} position position of this control
  15. * i.e. TOP_RIGHT
  16. * @param {Number} index index of the control
  17. * @example
  18. *
  19. * Example:
  20. * <map center="41.850033,-87.6500523" zoom="3">
  21. * <custom-control id="home" position="TOP_LEFT" index="1">
  22. * <div style="background-color: white;">
  23. * <b>Home</b>
  24. * </div>
  25. * </custom-control>
  26. * </map>
  27. *
  28. */
  29. /*jshint -W089*/
  30. ngMap.directive('customControl', ['Attr2Options', '$compile', function(Attr2Options, $compile) {
  31. var parser = Attr2Options;
  32. return {
  33. restrict: 'E',
  34. require: '^map',
  35. link: function(scope, element, attrs, mapController) {
  36. element.css('display','none');
  37. var orgAttrs = parser.orgAttributes(element);
  38. var filtered = parser.filter(attrs);
  39. var options = parser.getOptions(filtered, scope);
  40. var events = parser.getEvents(scope, filtered);
  41. console.log("custom-control options", options, "events", events);
  42. /**
  43. * build a custom control element
  44. */
  45. var compiled = $compile(element.html().trim())(scope);
  46. var customControlEl = compiled[0];
  47. /**
  48. * set events
  49. */
  50. for (var eventName in events) {
  51. google.maps.event.addDomListener(customControlEl, eventName, events[eventName]);
  52. }
  53. mapController.addObject('customControls', customControlEl);
  54. scope.$on('mapInitialized', function(evt, map) {
  55. var position = options.position;
  56. map.controls[google.maps.ControlPosition[position]].push(customControlEl);
  57. });
  58. } //link
  59. }; // return
  60. }]);// function