Skip to content

Commit 3a64ccb

Browse files
committed
issue allenhwkim#250, added an event 'objectChanged', and used it to zoom to incluce all markers
1 parent 82b5c10 commit 3a64ccb

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

directives/map.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,12 @@
170170
scope.map.scope = scope;
171171
google.maps.event.addListenerOnce(map, "idle", function() {
172172
scope.$emit('mapInitialized', map);
173-
if (attrs.zoomToIncludeMarkers) {
173+
if (attrs.zoomToIncludeMarkers == 'auto') {
174+
scope.$on('objectChanged', function(evt, msg) {
175+
console.log('objectChanged name', msg);
176+
msg[0] == 'markers' && ctrl.zoomToIncludeMarkers();
177+
});
178+
}else if (attrs.zoomToIncludeMarkers) {
174179
console.log('zoomToIncludeMarkers');
175180
ctrl.zoomToIncludeMarkers();
176181
}

directives/map_controller.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* @property {Hash} markers collection of Markers initiated within `map` directive
1111
* @property {Hash} shapes collection of shapes initiated within `map` directive
1212
*/
13-
var MapController = function($q, NavigatorGeolocation, GeoCoder, Attr2Options) {
13+
var MapController = function($scope, $q, NavigatorGeolocation, GeoCoder, Attr2Options) {
1414
var parser = Attr2Options;
1515
var _this = this;
1616

@@ -62,6 +62,7 @@
6262
if (obj.centered && obj.position) {
6363
this.map.setCenter(obj.position);
6464
}
65+
$scope.$emit('objectChanged', [groupName, this.map[groupName]]);
6566
} else {
6667
obj.groupName = groupName;
6768
this._objects.push(obj);
@@ -85,6 +86,7 @@
8586

8687
/* delete from map */
8788
obj.map && obj.setMap && obj.setMap(null);
89+
$scope.$emit('objectChanged', [groupName, this.map[groupName]]);
8890
}
8991
};
9092

@@ -177,6 +179,6 @@
177179

178180
}; // MapController
179181

180-
MapController.$inject = ['$q', 'NavigatorGeolocation', 'GeoCoder', 'Attr2Options'];
182+
MapController.$inject = ['$scope', '$q', 'NavigatorGeolocation', 'GeoCoder', 'Attr2Options'];
181183
angular.module('ngMap').controller('MapController', MapController);
182184
})();

testapp/map_zoom_to_include_markers.html

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
55
<script src="https://maps.google.com/maps/api/js?sensor=false"></script>
6-
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>
6+
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
77
<!--
88
<script src="../build/scripts/ng-map.min.js"></script>
99
-->
@@ -16,7 +16,7 @@
1616
<script src="../services/navigator_geolocation.js"></script>
1717
<script src="../services/attr2_options.js"></script>
1818
<script>
19-
function MyCtrl($scope) {
19+
angular.module('ngMap').controller('MyCtrl', function($scope) {
2020
$scope.positions =[
2121
{pos:[40.71, -74.21]},
2222
{pos:[41.72, -73.20]},
@@ -26,13 +26,19 @@
2626
{pos:[45.76, -69.16]},
2727
{pos:[46.77, -68.15]}
2828
];
29-
}
29+
$scope.addMarker = function(event) {
30+
var ll = event.latLng;
31+
console.log('ccccccccccccc', $scope.$id, $scope.map.markers, {lat:ll.lat(), lng: ll.lng()});
32+
$scope.positions.push({pos:[ll.lat(), ll.lng()]});
33+
$scope.$apply();
34+
}
35+
});
3036
</script>
3137
</head>
3238

3339
<body>
3440
<div ng-controller="MyCtrl">
35-
<h3>Without zoom-to-include-markers='true'</h3>
41+
<h3>Without zoom-to-include-markers</h3>
3642
<map zoom="11" center="[40.74, -74.18]">
3743
<marker ng-repeat="p in positions" position="{{p.pos}}" title="pos: {{p.pos}}"></marker>
3844
</map>
@@ -41,6 +47,12 @@ <h3>With zoom-to-include-markers='true'</h3>
4147
<map zoom="11" center="[40.74, -74.18]" zoom-to-include-markers="true">
4248
<marker ng-repeat="p in positions" position="{{p.pos}}" title="pos: {{p.pos}}"></marker>
4349
</map>
50+
51+
<h3>With zoom-to-include-markers='auto'</h3>
52+
Please click on map to add more markers
53+
<map zoom="11" center="[40.74, -74.18]" zoom-to-include-markers="auto" on-click="addMarker()">
54+
<marker ng-repeat="p in positions" position="{{p.pos}}" title="pos: {{p.pos}}"></marker>
55+
</map>
4456
</div>
4557
</body>
4658
</html>

0 commit comments

Comments
 (0)