<!DOCTYPE html>
<html ng-app="ngMap">
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<script src="https://maps.google.com/maps/api/js?libraries=placeses,visualization,drawing,geometry,places"></script>
<script src="https://code.angularjs.org/1.3.15/angular.js"></script>
<script src="https://rawgit.com/allenhwkim/angularjs-google-maps/master/build/scripts/ng-map.js"></script>
<script>
angular.module('ngMap').controller('MyCtrl', function(NgMap, $scope) {
var vm = this;
vm.positions = [{
pos: [40.71, -74.15]
}, {
pos: [40.72, -74.20]
}, {
pos: [40.73, -74.18]
}, {
pos: [40.77, -74.19]
}, {
pos: [40.75, -74.17]
}, {
pos: [40.76, -74.16]
}, {
pos: [40.74, -74.20]
}];
vm.positionsWithinBounds = angular.copy(vm.positions);
var analyzeViewableMarkers = function(map) {
var ne = map.getBounds().getNorthEast();
var sw = map.getBounds().getSouthWest();
var latBounds = {
top: ne.lat(),
bottom: sw.lat()
};
var lonBounds = {
left: sw.lng(),
right: ne.lng()
};
vm.positionsWithinBounds = angular.copy(
vm.positions.filter(function(pos) {
var liesWithinLatBounds = latBounds.top >= pos.pos[0] && latBounds.bottom <= pos.pos[0];
var liesWithinLonBounds = lonBounds.left <= pos.pos[1] && lonBounds.right >= pos.pos[1];
return liesWithinLatBounds && liesWithinLonBounds;
})
);
// for some reason the $digest cycle doesn't catch changes to vm.viewablePositions
// trigger it manually
$scope.$apply();
};
NgMap.getMap().then(function(map) {
vm.map = map;
google.maps.event.addListener(map, 'bounds_changed', function() {
analyzeViewableMarkers(map);
});
});
});
</script>
</head>
<body>
<div ng-controller="MyCtrl as vm">
<ng-map zoom="11" center="[40.74, -74.18]" trigger-resize="true">
<marker ng-repeat="p in vm.positions" position="{{p.pos}}" title="pos: {{p.pos}}"></marker>
</ng-map>
<div>
<div ng-repeat="p in vm.positionsWithinBounds">{{p.pos}}</div>
</div>
</div>
</body>
</html>