mapsCtrl.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. 'use strict';
  2. /**
  3. * controllers for GoogleMap
  4. * AngularJS Directive
  5. */
  6. app.controller('MapCoordinatesCtrl', ["$scope", "$compile", function ($scope, $compile) {
  7. var TILE_SIZE = 256;
  8. function bound(value, opt_min, opt_max) {
  9. if (opt_min != null)
  10. value = Math.max(value, opt_min);
  11. if (opt_max != null)
  12. value = Math.min(value, opt_max);
  13. return value;
  14. }
  15. function degreesToRadians(deg) {
  16. return deg * (Math.PI / 180);
  17. }
  18. function radiansToDegrees(rad) {
  19. return rad / (Math.PI / 180);
  20. }
  21. function MercatorProjection() {
  22. this.pixelOrigin_ = new google.maps.Point(TILE_SIZE / 2, TILE_SIZE / 2);
  23. this.pixelsPerLonDegree_ = TILE_SIZE / 360;
  24. this.pixelsPerLonRadian_ = TILE_SIZE / (2 * Math.PI);
  25. }
  26. MercatorProjection.prototype.fromLatLngToPoint = function (latLng, opt_point) {
  27. var me = this;
  28. var point = opt_point || new google.maps.Point(0, 0);
  29. var origin = me.pixelOrigin_;
  30. point.x = origin.x + latLng.lng() * me.pixelsPerLonDegree_;
  31. // Truncating to 0.9999 effectively limits latitude to 89.189. This is
  32. // about a third of a tile past the edge of the world tile.
  33. var siny = bound(Math.sin(degreesToRadians(latLng.lat())), -0.9999, 0.9999);
  34. point.y = origin.y + 0.5 * Math.log((1 + siny) / (1 - siny)) * -me.pixelsPerLonRadian_;
  35. return point;
  36. };
  37. MercatorProjection.prototype.fromPointToLatLng = function (point) {
  38. var me = this;
  39. var origin = me.pixelOrigin_;
  40. var lng = (point.x - origin.x) / me.pixelsPerLonDegree_;
  41. var latRadians = (point.y - origin.y) / -me.pixelsPerLonRadian_;
  42. var lat = radiansToDegrees(2 * Math.atan(Math.exp(latRadians)) - Math.PI / 2);
  43. return new google.maps.LatLng(lat, lng);
  44. };
  45. $scope.$on('mapInitialized', function (event, map) {
  46. var numTiles = 1 << map.getZoom();
  47. var projection = new MercatorProjection();
  48. $scope.chicago = map.getCenter();
  49. $scope.worldCoordinate = projection.fromLatLngToPoint($scope.chicago);
  50. $scope.pixelCoordinate = new google.maps.Point($scope.worldCoordinate.x * numTiles, $scope.worldCoordinate.y * numTiles);
  51. $scope.tileCoordinate = new google.maps.Point(Math.floor($scope.pixelCoordinate.x / TILE_SIZE), Math.floor($scope.pixelCoordinate.y / TILE_SIZE));
  52. });
  53. }]);
  54. app.controller('EventSimpleCtrl', ['$scope', '$timeout',
  55. function ($scope, $timeout) {
  56. var marker, map;
  57. $scope.$on('mapInitialized', function (evt, evtMap) {
  58. map = evtMap;
  59. marker = map.markers[0];
  60. });
  61. $scope.centerChanged = function (event) {
  62. $timeout(function () {
  63. map.panTo(marker.getPosition());
  64. }, 3000);
  65. }
  66. $scope.click = function (event) {
  67. map.setZoom(8);
  68. map.setCenter(marker.getPosition());
  69. }
  70. }]);
  71. app.controller('EventPropertiesCtrl', ["$scope", function ($scope) {
  72. $scope.$on('mapInitialized', function (evt, map) {
  73. var infoWindow = map.infoWindows[1];
  74. $scope.zoomChanged = function (e) {
  75. infoWindow.setContent('Zoom: ' + map.getZoom());
  76. map.setCenter(infoWindow.getPosition());
  77. }
  78. });
  79. }]);
  80. app.controller('ControlCustomStateCtrl', ["$scope", function ($scope) {
  81. $scope.home = new google.maps.LatLng(41.850033, -87.6500523);
  82. $scope.goHome = function () {
  83. $scope.map.setCenter($scope.home);
  84. }
  85. $scope.setHome = function () {
  86. $scope.home = $scope.map.getCenter();
  87. }
  88. }]);