map_controller.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * @ngdoc directive
  3. * @name MapController
  4. * @requires $scope
  5. * @property {Hash} controls collection of Controls initiated within `map` directive
  6. * @property {Hash} markersi collection of Markers initiated within `map` directive
  7. * @property {Hash} shapes collection of shapes initiated within `map` directive
  8. * @property {MarkerClusterer} markerClusterer MarkerClusterer initiated within `map` directive
  9. */
  10. /*jshint -W089*/
  11. ngMap.MapController = function() {
  12. this.map = null;
  13. this._objects = [];
  14. /**
  15. * Add a marker to map and $scope.markers
  16. * @memberof MapController
  17. * @name addMarker
  18. * @param {Marker} marker google map marker
  19. */
  20. this.addMarker = function(marker) {
  21. /**
  22. * marker and shape are initialized before map is initialized
  23. * so, collect _objects then will init. those when map is initialized
  24. * However the case as in ng-repeat, we can directly add to map
  25. */
  26. if (this.map) {
  27. this.map.markers = this.map.markers || {};
  28. marker.setMap(this.map);
  29. if (marker.centered) {
  30. this.map.setCenter(marker.position);
  31. }
  32. var len = Object.keys(this.map.markers).length;
  33. this.map.markers[marker.id || len] = marker;
  34. } else {
  35. this._objects.push(marker);
  36. }
  37. };
  38. /**
  39. * Add a shape to map and $scope.shapes
  40. * @memberof MapController
  41. * @name addShape
  42. * @param {Shape} shape google map shape
  43. */
  44. this.addShape = function(shape) {
  45. if (this.map) {
  46. this.map.shapes = this.map.shapes || {};
  47. shape.setMap(this.map);
  48. var len = Object.keys(this.map.shapes).length;
  49. this.map.shapes[shape.id || len] = shape;
  50. } else {
  51. this._objects.push(shape);
  52. }
  53. };
  54. this.addObject = function(groupName, obj) {
  55. if (this.map) {
  56. this.map[groupName] = this.map[groupName] || {};
  57. var len = Object.keys(this.map[groupName]).length;
  58. this.map[groupName][obj.id || len] = obj;
  59. if (groupName != "infoWindows" && obj.setMap) { //infoWindow.setMap works like infoWindow.open
  60. obj.setMap(this.map);
  61. }
  62. } else {
  63. obj.groupName = groupName;
  64. this._objects.push(obj);
  65. }
  66. }
  67. /**
  68. * Add a shape to map and $scope.shapes
  69. * @memberof MapController
  70. * @name addShape
  71. * @param {Shape} shape google map shape
  72. */
  73. this.addObjects = function(objects) {
  74. for (var i=0; i<objects.length; i++) {
  75. var obj=objects[i];
  76. if (obj instanceof google.maps.Marker) {
  77. this.addMarker(obj);
  78. } else if (obj instanceof google.maps.Circle ||
  79. obj instanceof google.maps.Polygon ||
  80. obj instanceof google.maps.Polyline ||
  81. obj instanceof google.maps.Rectangle ||
  82. obj instanceof google.maps.GroundOverlay) {
  83. this.addShape(obj);
  84. } else {
  85. this.addObject(obj.groupName, obj);
  86. }
  87. }
  88. };
  89. };