map-coordinates.html
changeset 0 5ecce0e1ef52
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/map-coordinates.html	Wed Apr 09 12:36:39 2014 +0200
     1.3 @@ -0,0 +1,117 @@
     1.4 +
     1.5 +
     1.6 +<!DOCTYPE html>
     1.7 +<html>
     1.8 +  <head>
     1.9 +    <title>Showing pixel and tile coordinates</title>
    1.10 +    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    1.11 +    <meta charset="utf-8">
    1.12 +    <link href="/maps/documentation/javascript/examples/default.css" rel="stylesheet">
    1.13 +    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
    1.14 +    <script>
    1.15 +var map;
    1.16 +var TILE_SIZE = 256;
    1.17 +var chicago = new google.maps.LatLng(41.850033,-87.6500523);
    1.18 +
    1.19 +function bound(value, opt_min, opt_max) {
    1.20 +  if (opt_min != null) value = Math.max(value, opt_min);
    1.21 +  if (opt_max != null) value = Math.min(value, opt_max);
    1.22 +  return value;
    1.23 +}
    1.24 +
    1.25 +function degreesToRadians(deg) {
    1.26 +  return deg * (Math.PI / 180);
    1.27 +}
    1.28 +
    1.29 +function radiansToDegrees(rad) {
    1.30 +  return rad / (Math.PI / 180);
    1.31 +}
    1.32 +
    1.33 +/** @constructor */
    1.34 +function MercatorProjection() {
    1.35 +  this.pixelOrigin_ = new google.maps.Point(TILE_SIZE / 2,
    1.36 +      TILE_SIZE / 2);
    1.37 +  this.pixelsPerLonDegree_ = TILE_SIZE / 360;
    1.38 +  this.pixelsPerLonRadian_ = TILE_SIZE / (2 * Math.PI);
    1.39 +}
    1.40 +
    1.41 +MercatorProjection.prototype.fromLatLngToPoint = function(latLng,
    1.42 +    opt_point) {
    1.43 +  var me = this;
    1.44 +  var point = opt_point || new google.maps.Point(0, 0);
    1.45 +  var origin = me.pixelOrigin_;
    1.46 +
    1.47 +  point.x = origin.x + latLng.lng() * me.pixelsPerLonDegree_;
    1.48 +
    1.49 +  // Truncating to 0.9999 effectively limits latitude to 89.189. This is
    1.50 +  // about a third of a tile past the edge of the world tile.
    1.51 +  var siny = bound(Math.sin(degreesToRadians(latLng.lat())), -0.9999,
    1.52 +      0.9999);
    1.53 +  point.y = origin.y + 0.5 * Math.log((1 + siny) / (1 - siny)) *
    1.54 +      -me.pixelsPerLonRadian_;
    1.55 +  return point;
    1.56 +};
    1.57 +
    1.58 +MercatorProjection.prototype.fromPointToLatLng = function(point) {
    1.59 +  var me = this;
    1.60 +  var origin = me.pixelOrigin_;
    1.61 +  var lng = (point.x - origin.x) / me.pixelsPerLonDegree_;
    1.62 +  var latRadians = (point.y - origin.y) / -me.pixelsPerLonRadian_;
    1.63 +  var lat = radiansToDegrees(2 * Math.atan(Math.exp(latRadians)) -
    1.64 +      Math.PI / 2);
    1.65 +  return new google.maps.LatLng(lat, lng);
    1.66 +};
    1.67 +
    1.68 +function createInfoWindowContent() {
    1.69 +  var numTiles = 1 << map.getZoom();
    1.70 +  var projection = new MercatorProjection();
    1.71 +  var worldCoordinate = projection.fromLatLngToPoint(chicago);
    1.72 +  var pixelCoordinate = new google.maps.Point(
    1.73 +      worldCoordinate.x * numTiles,
    1.74 +      worldCoordinate.y * numTiles);
    1.75 +  var tileCoordinate = new google.maps.Point(
    1.76 +      Math.floor(pixelCoordinate.x / TILE_SIZE),
    1.77 +      Math.floor(pixelCoordinate.y / TILE_SIZE));
    1.78 +
    1.79 +  return [
    1.80 +    'Chicago, IL',
    1.81 +    'LatLng: ' + chicago.lat() + ' , ' + chicago.lng(),
    1.82 +    'World Coordinate: ' + worldCoordinate.x + ' , ' +
    1.83 +      worldCoordinate.y,
    1.84 +    'Pixel Coordinate: ' + Math.floor(pixelCoordinate.x) + ' , ' +
    1.85 +      Math.floor(pixelCoordinate.y),
    1.86 +    'Tile Coordinate: ' + tileCoordinate.x + ' , ' +
    1.87 +      tileCoordinate.y + ' at Zoom Level: ' + map.getZoom()
    1.88 +  ].join('<br>');
    1.89 +}
    1.90 +
    1.91 +function initialize() {
    1.92 +  var mapOptions = {
    1.93 +    zoom: 3,
    1.94 +    center: chicago,
    1.95 +    mapTypeId: google.maps.MapTypeId.ROADMAP
    1.96 +  };
    1.97 +
    1.98 +  map = new google.maps.Map(document.getElementById('map-canvas'),
    1.99 +      mapOptions);
   1.100 +
   1.101 +  var coordInfoWindow = new google.maps.InfoWindow();
   1.102 +  coordInfoWindow.setContent(createInfoWindowContent());
   1.103 +  coordInfoWindow.setPosition(chicago);
   1.104 +  coordInfoWindow.open(map);
   1.105 +
   1.106 +  google.maps.event.addListener(map, 'zoom_changed', function() {
   1.107 +    coordInfoWindow.setContent(createInfoWindowContent());
   1.108 +    coordInfoWindow.open(map);
   1.109 +  });
   1.110 +}
   1.111 +
   1.112 +google.maps.event.addDomListener(window, 'load', initialize);
   1.113 +
   1.114 +    </script>
   1.115 +  </head>
   1.116 +  <body>
   1.117 +    <div id="map-canvas"></div>
   1.118 +  </body>
   1.119 +</html>
   1.120 +