diff --git a/code/map_data_calc_tools.js b/code/map_data_calc_tools.js index 276e38f0..80bb5e0f 100644 --- a/code/map_data_calc_tools.js +++ b/code/map_data_calc_tools.js @@ -10,34 +10,42 @@ // http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames -window.zoomToTilesPerEdge = function(zoom) { -// var LEVEL_TO_TILES_PER_EDGE = [65536, 65536, 16384, 16384, 4096, 1536, 1024, 256, 32]; -// return LEVEL_TO_TILES_PER_EDGE[level]; +window.getMapZoomTileParameters = function(zoom) { + // these arrays/constants are based on those in the stock intel site. it's essential we keep them in sync with their code + // (it may be worth reading the values from their code rather than using our own copies? it's a case of either + // breaking if they rename their variables if we do, or breaking if they change the values if we don't) var ZOOM_TO_TILES_PER_EDGE = [32, 32, 32, 32, 256, 256, 256, 1024, 1024, 1536, 4096, 4096, 16384, 16384, 16384]; - return ZOOM_TO_TILES_PER_EDGE[zoom] || 65536; + var MAX_TILES_PER_EDGE = 65536; + var ZOOM_TO_LEVEL = [8, 8, 8, 8, 7, 7, 7, 6, 6, 5, 4, 4, 3, 2, 2, 1, 1]; + + return { + level: ZOOM_TO_LEVEL[zoom] || 0, // default to level 0 (all portals) if not in array + tilesPerEdge: ZOOM_TO_TILES_PER_EDGE[zoom] || MAX_TILES_PER_EDGE, + zoom: zoom // include the zoom level, for reference + }; } -window.lngToTile = function(lng, zoom) { - return Math.floor((lng + 180) / 360 * zoomToTilesPerEdge(zoom)); +window.lngToTile = function(lng, params) { + return Math.floor((lng + 180) / 360 * params.tilesPerEdge); } -window.latToTile = function(lat, zoom) { +window.latToTile = function(lat, params) { return Math.floor((1 - Math.log(Math.tan(lat * Math.PI / 180) + - 1 / Math.cos(lat * Math.PI / 180)) / Math.PI) / 2 * zoomToTilesPerEdge(zoom)); + 1 / Math.cos(lat * Math.PI / 180)) / Math.PI) / 2 * params.tilesPerEdge); } -window.tileToLng = function(x, zoom) { - return x / zoomToTilesPerEdge(zoom) * 360 - 180; +window.tileToLng = function(x, params) { + return x / params.tilesPerEdge * 360 - 180; } -window.tileToLat = function(y, zoom) { - var n = Math.PI - 2 * Math.PI * y / zoomToTilesPerEdge(zoom); +window.tileToLat = function(y, params) { + var n = Math.PI - 2 * Math.PI * y / params.tilesPerEdge; return 180 / Math.PI * Math.atan(0.5 * (Math.exp(n) - Math.exp(-n))); } -window.pointToTileId = function(zoom, x, y) { - return zoom + "_" + x + "_" + y; +window.pointToTileId = function(params, x, y) { + return params.zoom + "_" + x + "_" + y; } diff --git a/code/map_data_request.js b/code/map_data_request.js index ecb35e59..56165568 100644 --- a/code/map_data_request.js +++ b/code/map_data_request.js @@ -180,8 +180,8 @@ window.MapDataRequest.prototype.refresh = function() { var bounds = clampLatLngBounds(map.getBounds()); var zoom = map.getZoom(); -//TODO: fix this: the API now takes zoom (again) rather than portal level, so we need to go back to more like the old API - var minPortalLevel = getMinPortalLevelForZoom(zoom); + + var tileParams = getMapZoomTileParameters(zoom); //DEBUG: resize the bounds so we only retrieve some data //bounds = bounds.pad(-0.4); @@ -189,36 +189,34 @@ window.MapDataRequest.prototype.refresh = function() { //var debugrect = L.rectangle(bounds,{color: 'red', fill: false, weight: 4, opacity: 0.8}).addTo(map); //setTimeout (function(){ map.removeLayer(debugrect); }, 10*1000); - var x1 = lngToTile(bounds.getWest(), zoom); - var x2 = lngToTile(bounds.getEast(), zoom); - var y1 = latToTile(bounds.getNorth(), zoom); - var y2 = latToTile(bounds.getSouth(), zoom); + var x1 = lngToTile(bounds.getWest(), tileParams); + var x2 = lngToTile(bounds.getEast(), tileParams); + var y1 = latToTile(bounds.getNorth(), tileParams); + var y2 = latToTile(bounds.getSouth(), tileParams); // calculate the full bounds for the data - including the part of the tiles off the screen edge var dataBounds = L.latLngBounds([ - [tileToLat(y2+1,zoom), tileToLng(x1,zoom)], - [tileToLat(y1,zoom), tileToLng(x2+1,zoom)] + [tileToLat(y2+1,tileParams), tileToLng(x1,tileParams)], + [tileToLat(y1,tileParams), tileToLng(x2+1,tileParams)] ]); //var debugrect2 = L.rectangle(dataBounds,{color: 'magenta', fill: false, weight: 4, opacity: 0.8}).addTo(map); //setTimeout (function(){ map.removeLayer(debugrect2); }, 10*1000); // store the parameters used for fetching the data. used to prevent unneeded refreshes after move/zoom - this.fetchedDataParams = { bounds: dataBounds, mapZoom: map.getZoom(), minPortalLevel: minPortalLevel, zoom: zoom }; + this.fetchedDataParams = { bounds: dataBounds, mapZoom: map.getZoom(), zoom: zoom }; - window.runHooks ('mapDataRefreshStart', {bounds: bounds, zoom: zoom, minPortalLevel: minPortalLevel, tileBounds: dataBounds}); + window.runHooks ('mapDataRefreshStart', {bounds: bounds, zoom: zoom, minPortalLevel: tileParams.level, tileBounds: dataBounds}); this.render.startRenderPass(); - this.render.clearPortalsBelowLevel(minPortalLevel); - - + this.render.clearPortalsBelowLevel(tileParams.level); this.render.clearEntitiesOutsideBounds(dataBounds); this.render.updateEntityVisibility(); this.render.processGameEntities(artifact.getArtifactEntities()); - console.log('requesting data tiles at zoom '+zoom+' (L'+minPortalLevel+'+ portals), map zoom is '+map.getZoom()); + console.log('requesting data tiles at zoom '+zoom+' (L'+tileParams.level+'+ portals, '+tileParams.tilesPerEdge+' tiles per global edge), map zoom is '+map.getZoom()); this.cachedTileCount = 0; @@ -236,11 +234,11 @@ window.MapDataRequest.prototype.refresh = function() { for (var y = y1; y <= y2; y++) { // x goes from bottom to top(?) for (var x = x1; x <= x2; x++) { - var tile_id = pointToTileId(zoom, x, y); - var latNorth = tileToLat(y,zoom); - var latSouth = tileToLat(y+1,zoom); - var lngWest = tileToLng(x,zoom); - var lngEast = tileToLng(x+1,zoom); + var tile_id = pointToTileId(tileParams, x, y); + var latNorth = tileToLat(y,tileParams); + var latSouth = tileToLat(y+1,tileParams); + var lngWest = tileToLng(x,tileParams); + var lngEast = tileToLng(x+1,tileParams); this.debugTiles.create(tile_id,[[latSouth,lngWest],[latNorth,lngEast]]); diff --git a/code/utils_misc.js b/code/utils_misc.js index 185cac86..9293dc8c 100644 --- a/code/utils_misc.js +++ b/code/utils_misc.js @@ -279,16 +279,10 @@ window.androidPermalink = function() { } -window.getMinPortalLevelForZoom = function(z) { - var ZOOM_TO_LEVEL = [8, 8, 8, 8, 7, 7, 7, 6, 6, 5, 4, 4, 3, 2, 2, 1, 1]; - - return ZOOM_TO_LEVEL[z] || 0; -} - window.getMinPortalLevel = function() { var z = map.getZoom(); - return getMinPortalLevelForZoom(z); + return getMapZoomTileParameters(z).level; } // returns number of pixels left to scroll down before reaching the