function newTilesSelector({ container, onTileSelect = (()=>{}), ...mapConfig }) {
const map = newLeafletMap(container, mapConfig);
const MyGridLayer = L.GridLayer.extend({
_selectTile(info) {
if (this._selection) {
this._selection.remove();
this._selection = null;
}
info = info || this._selectedTileInfo;
if (!info) return ;
const { x, y, z } = info || {};
this._selectedTileInfo = { x, y, z };
const bounds = L.latLngBounds([
[ tileUtils.tile2lat(y, z), tileUtils.tile2long(x+1, z)],
[ tileUtils.tile2lat(y+1, z), tileUtils.tile2long(x, z)]
]);
this._selection = L.rectangle(bounds, {color: "#ff7800", weight: 1}).addTo(this._map);
},
getEvents: function (...args) {
const events = L.GridLayer.prototype.getEvents.apply(this, args);
return {
...events,
click(e){
const size = this.getTileSize();
const map = this._map;
const zoom = map.getZoom();
const pixelPoint = map.project(e.latlng, zoom).floor();
const coords = pixelPoint.unscaleBy(size).floor();
const tileInfo = { ...coords, z : zoom };
this._selectTile(tileInfo);
this.fire('selecttile', tileInfo);
}
};
},
createTile: function(coords){
const zoom = map.getZoom();
const size = this.getTileSize();
const style = {
width : `${size.x}px`,
height : `${size.y}px`,
border : `2px solid rgba(255, 0, 0, 0.3)`,
'font-size' : '1.5em',
}
const tile = L.DomUtil.create('div');
for (const [key, value] of Object.entries(style)) {
tile.style[key] = value;
}
tile.innerHTML = `[${zoom} : ${coords.x} : ${coords.y}]`;
return tile;
}
});
const grid = new MyGridLayer({});
grid
.addTo(map)
.on('selecttile', ({x,y,z}) => onTileSelect({ x, y, z }))
.bringToFront();
return map;
}