get X Y Z coordinates of tile by click on Leaflet map - Javascript Leaflet

Javascript examples for Leaflet:Map

Description

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

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.js"></script> 
      <script type="text/javascript" src="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.js"></script> 
      <link rel="stylesheet" type="text/css" href="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.css"> 
      <style id="compiled-css" type="text/css">

body, html {
   width: 100%;
   height: 100%;
}
#map {//from  www  . j  a v  a 2  s .  c o  m
   width: 100%;
   height: 100%;
}


      </style> 
      <script type="text/javascript">
    $(window).load(function(){
$(document).ready(function(){
    url = "http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
   var map = L.map('map').setView([0, 0], 0);
   if (typeof(Number.prototype.toRad) === "undefined") {
     Number.prototype.toRad = function() {
       return this * Math.PI / 180;
     }
   }
   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;
   }
   L.tileLayer(url, {
       maxZoom: 18
   }).addTo(map);
   map.on('click', function (e) {
        url = getTileURL(e.latlng.lat, e.latlng.lng, map.getZoom())
      console.log(url);
        console.log(url);
   });
});
    });

      </script> 
   </head> 
   <body> 
      <div id="map"></div>  
   </body>
</html>

Related Tutorials