static/pngtile/map.js
changeset 142 9b316e83e9e3
child 143 22efa9fe73c8
equal deleted inserted replaced
141:19a3ed063d18 142:9b316e83e9e3
       
     1 var map_config;
       
     2 var map;
       
     3 
       
     4 function map_init (_config) {
       
     5     map_config = _config;
       
     6 
       
     7     var bounds = L.latLngBounds(
       
     8         L.latLng(-map_config.image_height, 0),
       
     9         L.latLng(0, +map_config.image_width)
       
    10     );
       
    11     
       
    12     map = L.map('map', {
       
    13         crs: L              .CRS.Simple,
       
    14         minZoom:            0,
       
    15         maxZoom:            map_config.tile_zoom,
       
    16         maxBounds:          bounds
       
    17     });
       
    18 
       
    19     map.on('move', map_move);
       
    20 
       
    21     L.tileLayer(map_config.tile_url, {
       
    22         name:               map_config.tile_name,
       
    23         minZoom:            0,
       
    24         maxZoom:            map_config.tile_zoom,
       
    25         tileSize:           map_config.tile_size,
       
    26         continuousWorld:    true,
       
    27         noWrap:             true,
       
    28         zoomReverse:        true,
       
    29         bounds:             bounds
       
    30     }).addTo(map);
       
    31 
       
    32     // set position
       
    33     var x = bounds.getCenter().lng;
       
    34     var y = -bounds.getCenter().lat;
       
    35     var z = 0;
       
    36 
       
    37     if (document.location.hash) {
       
    38         // parse x:y:z tuple
       
    39         var pt = document.location.hash.substr(1).split(":");
       
    40         
       
    41         // unpack    
       
    42         if (pt.length) x = parseInt(pt.shift()) || x;
       
    43         if (pt.length) y = parseInt(pt.shift()) || y;
       
    44         if (pt.length) z = parseInt(pt.shift()) || z;
       
    45     }
       
    46 
       
    47     map_center(x, y, z);
       
    48 }
       
    49 
       
    50 function map_move () {
       
    51     var map_center = map.getCenter();
       
    52     var map_zoom = map.getZoom();
       
    53 
       
    54     var x = (+map_center.lng) << map_config.tile_zoom;
       
    55     var y = (-map_center.lat) << map_config.tile_zoom;
       
    56     var z = map_config.tile_zoom - map_zoom;
       
    57 
       
    58     document.location.hash = x + ":" + y + ":" + z;
       
    59 }
       
    60 
       
    61 /*
       
    62  * Position map based on pngtile coordinates
       
    63  */
       
    64 function map_center (x, y, z) {
       
    65     // translate to lat/lng
       
    66     // leaflet seems to base its latlng coordinates on the max zoom level
       
    67     var map_center = [
       
    68         -(y >> map_config.tile_zoom),
       
    69         +(x >> map_config.tile_zoom),
       
    70     ];
       
    71 
       
    72     // reversed zoom
       
    73     var map_zoom = map_config.tile_zoom - z;
       
    74     
       
    75     map.setView(map_center, map_zoom);
       
    76 }
       
    77