admin管理员组文章数量:1391951
For canvas layers, how can I access the clicked pixel of a specific tile? Given a LatLng like { lat: 37.68816, lng: -119.76196 }
, how can I: #1, retrieve the correct tile clicked, and #2, the pixel coordinates within the tile? Both of these should consider maxNativeZoom
.
For canvas layers, how can I access the clicked pixel of a specific tile? Given a LatLng like { lat: 37.68816, lng: -119.76196 }
, how can I: #1, retrieve the correct tile clicked, and #2, the pixel coordinates within the tile? Both of these should consider maxNativeZoom
.
1 Answer
Reset to default 8A CRS like L.CRS.EPSG3857
is required. The map's CRS is accessible by map.options.crs
. The true zoom, tile size (like 256, but could be 512 or higher about maxNativeZoom
) and a pixel origin like map.getPixelOrigin()
are required:
const latlngToTilePixel = (latlng, crs, zoom, tileSize, pixelOrigin) => {
const layerPoint = crs.latLngToPoint(latlng, zoom).floor()
const tile = layerPoint.divideBy(tileSize).floor()
const tileCorner = tile.multiplyBy(tileSize).subtract(pixelOrigin)
const tilePixel = layerPoint.subtract(pixelOrigin).subtract(tileCorner)
return [tile, tilePixel]
}
First, convert the latlng
to a layer point. Now all units are in pixels.
Second, divide by tileSize
and round down. This gives the tile "slippy" coordinates.
Third, multiply back by tileSize
to get the pixel coordinates of the tile corner, adjusted for pixelOrigin
.
Finally, to get the tile pixels, subtract the layer point from origin and tile corner.
本文标签: javascriptProject Leaflet LatLng to tile pixel coordinatesStack Overflow
版权声明:本文标题:javascript - Project Leaflet LatLng to tile pixel coordinates - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744635536a2616801.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论