marker.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * @ngdoc directive
  3. * @name marker
  4. * @requires Attr2Options
  5. * @requires NavigatorGeolocation
  6. * @description
  7. * Draw a Google map marker on a map with given options and register events
  8. *
  9. * Requires: map directive
  10. *
  11. * Restrict To: Element
  12. *
  13. * @param {String} position address, 'current', or [latitude, longitude]
  14. * example:
  15. * '1600 Pennsylvania Ave, 20500 Washingtion DC',
  16. * 'current position',
  17. * '[40.74, -74.18]'
  18. * @param {Boolean} centered if set, map will be centered with this marker
  19. * @param {String} <MarkerOption> Any Marker options, https://developers.google.com/maps/documentation/javascript/reference?csw=1#MarkerOptions
  20. * @param {String} <MapEvent> Any Marker events, https://developers.google.com/maps/documentation/javascript/reference
  21. * @example
  22. * Usage:
  23. * <map MAP_ATTRIBUTES>
  24. * <marker ANY_MARKER_OPTIONS ANY_MARKER_EVENTS"></MARKER>
  25. * </map>
  26. *
  27. * Example:
  28. * <map center="[40.74, -74.18]">
  29. * <marker position="[40.74, -74.18]" on-click="myfunc()"></div>
  30. * </map>
  31. *
  32. * <map center="the cn tower">
  33. * <marker position="the cn tower" on-click="myfunc()"></div>
  34. * </map>
  35. */
  36. ngMap.directive('marker', ['Attr2Options', function(Attr2Options) {
  37. var parser = Attr2Options;
  38. var getMarker = function(options, events) {
  39. var marker;
  40. /**
  41. * set options
  42. */
  43. if (options.icon instanceof Object) {
  44. if ((""+options.icon.path).match(/^[A-Z_]+$/)) {
  45. options.icon.path = google.maps.SymbolPath[options.icon.path];
  46. }
  47. for (var key in options.icon) {
  48. var arr = options.icon[key];
  49. if (key == "anchor" || key == "origin") {
  50. options.icon[key] = new google.maps.Point(arr[0], arr[1]);
  51. } else if (key == "size" || key == "scaledSize") {
  52. options.icon[key] = new google.maps.Size(arr[0], arr[1]);
  53. }
  54. }
  55. }
  56. if (!(options.position instanceof google.maps.LatLng)) {
  57. var orgPosition = options.position;
  58. options.position = new google.maps.LatLng(0,0);
  59. marker = new google.maps.Marker(options);
  60. parser.setDelayedGeoLocation(marker, 'setPosition', orgPosition);
  61. } else {
  62. marker = new google.maps.Marker(options);
  63. }
  64. /**
  65. * set events
  66. */
  67. if (Object.keys(events).length > 0) {
  68. console.log("markerEvents", events);
  69. }
  70. for (var eventName in events) {
  71. if (eventName) {
  72. google.maps.event.addListener(marker, eventName, events[eventName]);
  73. }
  74. }
  75. return marker;
  76. };
  77. return {
  78. restrict: 'E',
  79. require: '^map',
  80. link: function(scope, element, attrs, mapController) {
  81. var orgAttrs = parser.orgAttributes(element);
  82. var filtered = parser.filter(attrs);
  83. var markerOptions = parser.getOptions(filtered, scope);
  84. var markerEvents = parser.getEvents(scope, filtered);
  85. console.log('marker options', markerOptions, 'events', markerEvents);
  86. /**
  87. * set event to clean up removed marker
  88. * useful with ng-repeat
  89. */
  90. element.bind('$destroy', function() {
  91. var markers = marker.map.markers;
  92. for (var name in markers) {
  93. if (markers[name] == marker) {
  94. delete markers[name];
  95. }
  96. }
  97. marker.setMap(null);
  98. });
  99. var marker = getMarker(markerOptions, markerEvents);
  100. mapController.addMarker(marker);
  101. /**
  102. * set observers
  103. */
  104. parser.observeAttrSetObj(orgAttrs, attrs, marker); /* observers */
  105. } //link
  106. }; // return
  107. }]);//