map-coordinates.html
author sl
Wed, 09 Apr 2014 12:36:39 +0200
changeset 0 5ecce0e1ef52
permissions -rw-r--r--
Various gmaps HTML pages and scripts.
sl@0
     1
sl@0
     2
sl@0
     3
<!DOCTYPE html>
sl@0
     4
<html>
sl@0
     5
  <head>
sl@0
     6
    <title>Showing pixel and tile coordinates</title>
sl@0
     7
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
sl@0
     8
    <meta charset="utf-8">
sl@0
     9
    <link href="/maps/documentation/javascript/examples/default.css" rel="stylesheet">
sl@0
    10
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
sl@0
    11
    <script>
sl@0
    12
var map;
sl@0
    13
var TILE_SIZE = 256;
sl@0
    14
var chicago = new google.maps.LatLng(41.850033,-87.6500523);
sl@0
    15
sl@0
    16
function bound(value, opt_min, opt_max) {
sl@0
    17
  if (opt_min != null) value = Math.max(value, opt_min);
sl@0
    18
  if (opt_max != null) value = Math.min(value, opt_max);
sl@0
    19
  return value;
sl@0
    20
}
sl@0
    21
sl@0
    22
function degreesToRadians(deg) {
sl@0
    23
  return deg * (Math.PI / 180);
sl@0
    24
}
sl@0
    25
sl@0
    26
function radiansToDegrees(rad) {
sl@0
    27
  return rad / (Math.PI / 180);
sl@0
    28
}
sl@0
    29
sl@0
    30
/** @constructor */
sl@0
    31
function MercatorProjection() {
sl@0
    32
  this.pixelOrigin_ = new google.maps.Point(TILE_SIZE / 2,
sl@0
    33
      TILE_SIZE / 2);
sl@0
    34
  this.pixelsPerLonDegree_ = TILE_SIZE / 360;
sl@0
    35
  this.pixelsPerLonRadian_ = TILE_SIZE / (2 * Math.PI);
sl@0
    36
}
sl@0
    37
sl@0
    38
MercatorProjection.prototype.fromLatLngToPoint = function(latLng,
sl@0
    39
    opt_point) {
sl@0
    40
  var me = this;
sl@0
    41
  var point = opt_point || new google.maps.Point(0, 0);
sl@0
    42
  var origin = me.pixelOrigin_;
sl@0
    43
sl@0
    44
  point.x = origin.x + latLng.lng() * me.pixelsPerLonDegree_;
sl@0
    45
sl@0
    46
  // Truncating to 0.9999 effectively limits latitude to 89.189. This is
sl@0
    47
  // about a third of a tile past the edge of the world tile.
sl@0
    48
  var siny = bound(Math.sin(degreesToRadians(latLng.lat())), -0.9999,
sl@0
    49
      0.9999);
sl@0
    50
  point.y = origin.y + 0.5 * Math.log((1 + siny) / (1 - siny)) *
sl@0
    51
      -me.pixelsPerLonRadian_;
sl@0
    52
  return point;
sl@0
    53
};
sl@0
    54
sl@0
    55
MercatorProjection.prototype.fromPointToLatLng = function(point) {
sl@0
    56
  var me = this;
sl@0
    57
  var origin = me.pixelOrigin_;
sl@0
    58
  var lng = (point.x - origin.x) / me.pixelsPerLonDegree_;
sl@0
    59
  var latRadians = (point.y - origin.y) / -me.pixelsPerLonRadian_;
sl@0
    60
  var lat = radiansToDegrees(2 * Math.atan(Math.exp(latRadians)) -
sl@0
    61
      Math.PI / 2);
sl@0
    62
  return new google.maps.LatLng(lat, lng);
sl@0
    63
};
sl@0
    64
sl@0
    65
function createInfoWindowContent() {
sl@0
    66
  var numTiles = 1 << map.getZoom();
sl@0
    67
  var projection = new MercatorProjection();
sl@0
    68
  var worldCoordinate = projection.fromLatLngToPoint(chicago);
sl@0
    69
  var pixelCoordinate = new google.maps.Point(
sl@0
    70
      worldCoordinate.x * numTiles,
sl@0
    71
      worldCoordinate.y * numTiles);
sl@0
    72
  var tileCoordinate = new google.maps.Point(
sl@0
    73
      Math.floor(pixelCoordinate.x / TILE_SIZE),
sl@0
    74
      Math.floor(pixelCoordinate.y / TILE_SIZE));
sl@0
    75
sl@0
    76
  return [
sl@0
    77
    'Chicago, IL',
sl@0
    78
    'LatLng: ' + chicago.lat() + ' , ' + chicago.lng(),
sl@0
    79
    'World Coordinate: ' + worldCoordinate.x + ' , ' +
sl@0
    80
      worldCoordinate.y,
sl@0
    81
    'Pixel Coordinate: ' + Math.floor(pixelCoordinate.x) + ' , ' +
sl@0
    82
      Math.floor(pixelCoordinate.y),
sl@0
    83
    'Tile Coordinate: ' + tileCoordinate.x + ' , ' +
sl@0
    84
      tileCoordinate.y + ' at Zoom Level: ' + map.getZoom()
sl@0
    85
  ].join('<br>');
sl@0
    86
}
sl@0
    87
sl@0
    88
function initialize() {
sl@0
    89
  var mapOptions = {
sl@0
    90
    zoom: 3,
sl@0
    91
    center: chicago,
sl@0
    92
    mapTypeId: google.maps.MapTypeId.ROADMAP
sl@0
    93
  };
sl@0
    94
sl@0
    95
  map = new google.maps.Map(document.getElementById('map-canvas'),
sl@0
    96
      mapOptions);
sl@0
    97
sl@0
    98
  var coordInfoWindow = new google.maps.InfoWindow();
sl@0
    99
  coordInfoWindow.setContent(createInfoWindowContent());
sl@0
   100
  coordInfoWindow.setPosition(chicago);
sl@0
   101
  coordInfoWindow.open(map);
sl@0
   102
sl@0
   103
  google.maps.event.addListener(map, 'zoom_changed', function() {
sl@0
   104
    coordInfoWindow.setContent(createInfoWindowContent());
sl@0
   105
    coordInfoWindow.open(map);
sl@0
   106
  });
sl@0
   107
}
sl@0
   108
sl@0
   109
google.maps.event.addDomListener(window, 'load', initialize);
sl@0
   110
sl@0
   111
    </script>
sl@0
   112
  </head>
sl@0
   113
  <body>
sl@0
   114
    <div id="map-canvas"></div>
sl@0
   115
  </body>
sl@0
   116
</html>
sl@0
   117