map-projection-simple.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <!DOCTYPE html>
  2. <html ng-app="myApp">
  3. <head>
  4. <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
  5. <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
  6. <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.js"></script>
  7. <!-- build:js scripts/ng-map.min.js -->
  8. <script src="../app/scripts/app.js"></script>
  9. <script src="../app/scripts/directives/map_controller.js"></script>
  10. <script src="../app/scripts/directives/map.js"></script>
  11. <script src="../app/scripts/directives/marker.js"></script>
  12. <script src="../app/scripts/directives/shape.js"></script>
  13. <script src="../app/scripts/directives/map-type.js"></script>
  14. <script src="../app/scripts/services/geo_coder.js"></script>
  15. <script src="../app/scripts/services/navigator_geolocation.js"></script>
  16. <script src="../app/scripts/services/attr2_options.js"></script>
  17. <!-- endbuild -->
  18. <script>
  19. // Note: this value is exact as the map projects a full 360 degrees of longitude
  20. var GALL_PETERS_RANGE_X = 800;
  21. // Note: this value is inexact as the map is cut off at ~ +/- 83 degrees.
  22. // However, the polar regions produce very little increase in Y range, so
  23. // we will use the tile size.
  24. var GALL_PETERS_RANGE_Y = 510;
  25. function degreesToRadians(deg) {
  26. return deg * (Math.PI / 180);
  27. }
  28. function radiansToDegrees(rad) {
  29. return rad / (Math.PI / 180);
  30. }
  31. /**
  32. * @constructor
  33. * @implements {google.maps.Projection}
  34. */
  35. function GallPetersProjection() {
  36. // Using the base map tile, denote the lat/lon of the equatorial origin.
  37. this.worldOrigin_ = new google.maps.Point(GALL_PETERS_RANGE_X * 400 / 800,
  38. GALL_PETERS_RANGE_Y / 2);
  39. // This projection has equidistant meridians, so each longitude degree is a linear
  40. // mapping.
  41. this.worldCoordinatePerLonDegree_ = GALL_PETERS_RANGE_X / 360;
  42. // This constant merely reflects that latitudes vary from +90 to -90 degrees.
  43. this.worldCoordinateLatRange = GALL_PETERS_RANGE_Y / 2;
  44. };
  45. GallPetersProjection.prototype.fromLatLngToPoint = function(latLng) {
  46. var origin = this.worldOrigin_;
  47. var x = origin.x + this.worldCoordinatePerLonDegree_ * latLng.lng();
  48. // Note that latitude is measured from the world coordinate origin
  49. // at the top left of the map.
  50. var latRadians = degreesToRadians(latLng.lat());
  51. var y = origin.y - this.worldCoordinateLatRange * Math.sin(latRadians);
  52. return new google.maps.Point(x, y);
  53. };
  54. GallPetersProjection.prototype.fromPointToLatLng = function(point, noWrap) {
  55. var y = point.y;
  56. var x = point.x;
  57. if (y < 0) {
  58. y = 0;
  59. }
  60. if (y >= GALL_PETERS_RANGE_Y) {
  61. y = GALL_PETERS_RANGE_Y;
  62. }
  63. var origin = this.worldOrigin_;
  64. var lng = (x - origin.x) / this.worldCoordinatePerLonDegree_;
  65. var latRadians = Math.asin((origin.y - y) / this.worldCoordinateLatRange);
  66. var lat = radiansToDegrees(latRadians);
  67. return new google.maps.LatLng(lat, lng, noWrap);
  68. };
  69. var gallPetersMap;
  70. var gallPetersMapType = new google.maps.ImageMapType({
  71. getTileUrl: function(coord, zoom) {
  72. var numTiles = 1 << zoom;
  73. // Don't wrap tiles vertically.
  74. if (coord.y < 0 || coord.y >= numTiles) {
  75. return null;
  76. }
  77. // Wrap tiles horizontally.
  78. var x = ((coord.x % numTiles) + numTiles) % numTiles;
  79. // For simplicity, we use a tileset consisting of 1 tile at zoom level 0
  80. // and 4 tiles at zoom level 1. Note that we set the base URL to a
  81. // relative directory.
  82. var baseURL = 'https://google-developers.appspot.com/maps/documentation/javascript/examples/full/images/';
  83. baseURL += 'gall-peters_' + zoom + '_' + x + '_' + coord.y + '.png';
  84. return baseURL;
  85. },
  86. tileSize: new google.maps.Size(800, 512),
  87. isPng: true,
  88. minZoom: 0,
  89. maxZoom: 1,
  90. name: 'Gall-Peters'
  91. });
  92. gallPetersMapType.projection = new GallPetersProjection();
  93. </script>
  94. <script>
  95. var app = angular.module('myApp', ['ngMap']);
  96. app.controller('MapProjectionSimpleCtrl', function($scope, $window) {
  97. $scope.alertLocation = function(event) {
  98. $window.alert('Point.X.Y: ' + event.latLng);
  99. };
  100. });
  101. </script>
  102. </head>
  103. <body>
  104. <div ng-controller="MapProjectionSimpleCtrl">
  105. <map zoom="0" center="0,0"
  106. on-click="alertLocation()"
  107. street-view-control="false"
  108. map-type-id="gallPetersMap"
  109. map-type-control-options="{
  110. mapTypeIds: ['ROADMAP', 'gallPetersMap']
  111. }">
  112. <map-type name="gallPetersMap" object="gallPetersMapType">
  113. </map-type>
  114. </map>
  115. </div>
  116. </body>
  117. </html>