map_fit_bounds.html 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. <!doctype html>
  2. <html ng-app="myapp">
  3. <head>
  4. <script src="https://maps.googleapis.com/maps/api/js"></script>
  5. <script src="http://code.angularjs.org/1.2.25/angular.js"></script>
  6. <script src="https://rawgit.com/allenhwkim/angularjs-google-maps/master/build/scripts/ng-map.js"></script>
  7. <script>
  8. var app = angular.module('myapp', ['ngMap']);
  9. app.controller('MyCtrl', function($scope) {
  10. $scope.positions = [ [-24,132] ,[-25,131] ,[-26,130] ];
  11. var bounds = new google.maps.LatLngBounds();
  12. for (var i=0; i<$scope.positions.length; i++) {
  13. var latlng = new google.maps.LatLng($scope.positions[i][0], $scope.positions[i][1]);
  14. bounds.extend(latlng);
  15. }
  16. $scope.$on('mapInitialized', function(event, map) {
  17. map.setCenter(bounds.getCenter());
  18. map.fitBounds(bounds);
  19. });
  20. });
  21. </script>
  22. </head>
  23. <body ng-controller="MyCtrl">
  24. <map center="-25,131">
  25. <marker ng-repeat="pos in positions" position="{{pos}}"></marker>
  26. </map>
  27. </body>
  28. </html>