How to get X Y Z coordinates of tile by click on Leaflet map

后端 未结 3 509
说谎
说谎 2020-12-15 10:46

I want to ask for help to deal with the possible use of non-standard coordinates on the map Leaflet.

I want to use Leaflet to display custom maps with my own tile g

相关标签:
3条回答
  • 2020-12-15 11:31

    Here is a working example for getting Zoom, X, and Y coordinates of clicked tile (using openstreet map): http://jsfiddle.net/84P9r/

    function getTileURL(lat, lon, zoom) {
        var xtile = parseInt(Math.floor( (lon + 180) / 360 * (1<<zoom) ));
        var ytile = parseInt(Math.floor( (1 - Math.log(Math.tan(lat.toRad()) + 1 / Math.cos(lat.toRad())) / Math.PI) / 2 * (1<<zoom) ));
        return "" + zoom + "/" + xtile + "/" + ytile;
    }
    

    based on an answer https://stackoverflow.com/a/19197572

    0 讨论(0)
  • 2020-12-15 11:31

    You can use the mouseEventToLayerPoint and mouseEventToContainerPoint methods in the Leaflet API to convert pixels onscreen to pixels relative to the top-left of the map, and then using a little math, you can derive the location within a tile.

    0 讨论(0)
  • 2020-12-15 11:41

    This is what Leaflet does internally:

    var tileSize = [256, 256]
    var pixelPoint = map.project(e.latlng, map.getZoom()).floor()
    var coords = pixelPoint.unscaleBy(tileSize).floor()
    coords.z = map.getZoom() // { x: 212, y: 387, z: 10 }
    
    0 讨论(0)
提交回复
热议问题