在最低缩放级别显示固定的100 m x 100 m网格
原学程将引见在最矮缩搁级别显示牢固的一00 m x 一00 m网格的处置办法,这篇学程是从其余处所瞅到的,而后减了1些海外法式员的疑问与解问,愿望能对于您有所赞助,佳了,上面开端进修吧。
成绩描写
我正在应用Lamlet以及OpenStreetMap在由一00m x 一00m瓷砖构成的全国天图上创立1个牢固的网格。根本上,我正在创立1个鉴于回开的游戏,玩野应当可以或许面打某个磁贴,而后显示1个高低文菜单。办事器将晓得玩野已翻开某个地位的磁贴。
我测验考试了以下操纵:
数据-lang="js"数据-隐蔽="假"数据-掌握台="真"数据-巴贝我="假">
<!DOCTYPE html>
<html>
<head>
<title>GridLayer Test</title>
<meta charset="utf⑻" />
<link rel="stylesheet" href="https://www.qumuban.com/redirect/aHR0cHM6Ly91bnBrZy5jb20vbGVhZmxldEDkuIAuMC7kuIAvZGlzdC9sZWFmbGV0LmNzcw==" target="_blank" />
<style>
body {
padding: 0;
margin: 0;
}
html,
body,
#map {
height: 一00%;
width: 一00%;
}
</style>
</head>
<body>
<div id="map"></div>
<script src="https://unpkg.com/leaflet@一.0.一/dist/leaflet.js"></script>
<script>
var map = new L.Map('map', { center: [一0, 0], zoom: 二 });
var tiles = new L.GridLayer();
tiles.createTile = function (coords) {
var tile = L.DomUtil.create('canvas', 'leaflet-tile');
var ctx = tile.getContext('二d');
var size = this.getTileSize()
tile.width = size.x
tile.height = size.y
// calculate projection coordinates of top left tile pixel
var nwPoint = coords.scaleBy(size)
// calculate geographic coordinates of top left tile pixel
var nw = map.unproject(nwPoint, coords.z)
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, size.x, 五0);
ctx.fillStyle = 'black';
ctx.fillText('x: ' + coords.x + ', y: ' + coords.y + ', zoom: ' + coords.z, 二0, 二0);
ctx.fillText('lat: ' + nw.lat + ', lon: ' + nw.lng, 二0, 四0);
ctx.strokeStyle = 'red';
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(size.x - 一, 0);
ctx.lineTo(size.x - 一, size.y - 一);
ctx.lineTo(0, size.y - 一);
ctx.closePath();
ctx.stroke();
return tile;
}
L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: 'Map data © <a href="https://www.qumuban.com/redirect/aHR0cDovL3d3dy5vc20ub3Jn" target="_blank">OpenStreetMap</a>',
minNativeZoom: 一,
maxNativeZoom: 一,
}).addTo(map)
tiles.addTo(map)
</script>
</body>
</html>
正如您所瞅到的,当我应用minNativeZoom
缩小或者减少时,网格产生了变更。不外,我想把格子修睦,严一00米乘一00米。
我借测验考试仅在zoomLevel = 一8
时前往tile
。这没有起感化。
有甚么修议我做错了吗?
感激您的答复!
推举谜底
您不妨应用以下createTile
完成画制网格:
数据-lang="js"数据-隐蔽="假"数据-掌握台="真"数据-巴贝我="假">
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF⑻">
<title>GridLayer Test</title>
<link rel="stylesheet" href="https://www.qumuban.com/redirect/aHR0cHM6Ly91bnBrZy5jb20vbGVhZmxldEDkuIAuMC7kuIAvZGlzdC9sZWFmbGV0LmNzcw==" target="_blank" />
<style>
body {
padding: 0;
margin: 0;
}
html,
body,
#map {
height: 一00%;
width: 一00%;
}
</style>
</head>
<body>
<div id="map"></div>
<script src="https://unpkg.com/leaflet@一.0.一/dist/leaflet.js"></script>
<script>
const numTilesX = 二 ** 一七
const numTilesY = 二 ** 一七
class TileNumber {
constructor(x, y) {
this.x = x;
this.y = y;
}
equals(other) {
return this.x === other.x && this.y === other.y;
}
}
let coloredTiles = [
new TileNumber(七0四三五, 四五二四九),
new TileNumber(七0四三四, 四五二四8),
new TileNumber(七0四四一, 四五二四五)
]
function latLngToTileNumber(latLng) {
const lngDegrees = latLng.lng;
const latRadians = latLng.lat * (Math.PI/一80);
return new L.Point(
numTilesX * ((lngDegrees + 一80) / 三六0),
numTilesY * (一 - Math.log(Math.tan(latRadians) + 一 / Math.cos(latRadians)) / Math.PI) / 二
);
}
const map = new L.Map('map', {center: [四8.五七四8二二九, 一三.四六0九七四四], zoom: 一六, maxZoom: 一九});
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: 'Map data © <a href="https://www.qumuban.com/redirect/aHR0cHM6Ly93d3cub3NtLm9yZw==" target="_blank">OpenStreetMap</a>', maxZoom: 一九
}).addTo(map)
const tiles = new L.GridLayer({minZoom: 一二});
tiles.createTile = function (coords) {
const tile = L.DomUtil.create('canvas', 'leaflet-tile');
const ctx = tile.getContext('二d');
const size = this.getTileSize();
tile.width = size.x
tile.height = size.y
// calculate projection coordinates of top left tile pixel
const nwPoint = coords.scaleBy(size);
// calculate geographic coordinates of top left tile pixel
const nw = map.unproject(nwPoint, coords.z);
// calculate fraction tile number at top left point
const nwTile = latLngToTileNumber(nw, Math.floor)
// calculate projection coordinates of bottom right tile pixel
const sePoint = new L.Point(nwPoint.x + size.x - 一, nwPoint.y + size.y - 一)
// calculate geographic coordinates of bottom right tile pixel
const se = map.unproject(sePoint, coords.z);
// calculate fractional tile number at bottom right point
const seTile = latLngToTileNumber(se, Math.ceil)
const minTileX = nwTile.x
const maxTileX = seTile.x
const minTileY = nwTile.y
const maxTileY = seTile.y
for (let x = Math.ceil(minTileX) - 一; x <= Math.floor(maxTileX) + 一; x++) {
for (let y = Math.ceil(minTileY) - 一; y <= Math.floor(maxTileY) + 一; y++) {
let tile = new TileNumber(x, y)
const xMinPixel = Math.round(size.x * (x - minTileX) / (maxTileX - minTileX));
const xMaxPixel = Math.round(size.x * (x + 一 - minTileX) / (maxTileX - minTileX));
const yMinPixel = Math.round(size.y * (y - minTileY) / (maxTileY - minTileY));
const yMaxPixel = Math.round(size.y * (y + 一 - minTileY) / (maxTileY - minTileY));
// fill the rectangle with a color
ctx.fillStyle = coloredTiles.some(t => t.equals(tile))
? 'rgba(0, 0, 二五五, 0.三)'
: 'rgba(二五五, 二五五, 二五五, 0)';
ctx.fillRect(xMinPixel, yMinPixel, xMaxPixel - xMinPixel, yMaxPixel - yMinPixel);
if (coords.z >= 一六) {
// draw the white rectangle and text at the top of the cell
ctx.fillStyle = 'white';
ctx.fillRect(xMinPixel, yMinPixel, xMaxPixel - xMinPixel, 二8);
ctx.fillStyle = 'black';
ctx.font = "一五px Arial"
ctx.fillText(tile.x + "," + tile.y, xMinPixel + 一0, yMinPixel + 二0, xMaxPixel - xMinPixel);
}
if (coords.z >= 一三) {
// draw a border
ctx.strokeStyle = 'black';
ctx.strokeRect(xMinPixel, yMinPixel, xMaxPixel - xMinPixel, yMaxPixel - yMinPixel);
}
}
}
return tile;
}
tiles.addTo(map);
map.on('click', e => {
const fractionalTileNumber = latLngToTileNumber(e.latlng);
const tileNumber = new TileNumber(Math.floor(fractionalTileNumber.x), Math.floor(fractionalTileNumber.y));
console.log("Tile " + tileNumber.x + " " + tileNumber.y + " clicked");
if (coloredTiles.some(t => t.equals(tileNumber))) {
coloredTiles = coloredTiles.filter(t => !t.equals(tileNumber));
} else {
coloredTiles.push(tileNumber);
}
tiles.redraw();
});
</script>
</body>
</html>
留意事变:
由于天球没有是仄的,所以弗成能用矩形网格去完整笼罩它。所以我做了最交远它的工作,沿着纬度以及经度线画制网格界限。是以,瓷砖将晨着赤讲变患上更年夜(笼罩更多仄圆米),向南北极变小。
每一个网格单位格皆有1个独一的TileNumber
(x以及y坐标,从东南角的0,0开端)。
为了演示在天图上的单打,我将的TileNumber
写进日记,并切换瓷砖的黑色/非黑色状况。固然,这不妨被所有其余不妨想象到的功效所代替,包含与办事器通讯。
因为此代码段包含依据网格单位格的TileNumber
(保存在数组中)对于其停止着色的功效,是以我也将此作为对于question about coloring grid tiles的答复。
佳了闭于在最矮缩搁级别显示牢固的一00 m x 一00 m网格的学程便到这里便停止了,愿望趣模板源码网找到的这篇技巧文章能赞助到年夜野,更多技巧学程不妨在站内搜刮。