-
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from tomickigrzegorz:new-examples
New examples, refactoring
- Loading branch information
Showing
11 changed files
with
244 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Add rectangle over click tiles</title> | ||
<link | ||
rel="stylesheet" | ||
href="https://unpkg.com/[email protected]/dist/leaflet.css" | ||
/> | ||
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script> | ||
<link rel="stylesheet" href="style.css" /> | ||
</head> | ||
|
||
<body> | ||
<div id="map"></div> | ||
<script src="script.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* eslint-disable no-undef */ | ||
/** | ||
* add rectangle over click tiles | ||
*/ | ||
|
||
// config map | ||
let config = { | ||
minZoom: 7, | ||
maxZoom: 18, | ||
}; | ||
// magnification with which the map will start | ||
const zoom = 18; | ||
// co-ordinates | ||
const lat = 52.22977; | ||
const lng = 21.01178; | ||
|
||
// calling map | ||
const map = L.map("map", config).setView([lat, lng], zoom); | ||
|
||
// Used to load and display tile layers on the map | ||
// Most tile servers require attribution, which you can set under `Layer` | ||
L.tileLayer("https://tile.openstreetmap.org/{z}/{x}/{y}.png", { | ||
attribution: | ||
'© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors', | ||
}).addTo(map); | ||
|
||
// ---------------------------------------------- | ||
// convert lat lng and zoom on rectangle when click on map | ||
|
||
map.on("click", (e) => { | ||
const { lat, lng } = e.latlng; | ||
const zoom = map.getZoom(); | ||
|
||
const { x, y } = getTileNumber(lat, lng, zoom); | ||
|
||
const rectangle = L.rectangle( | ||
[ | ||
[tile2lat(y, zoom), tile2lon(x, zoom)], | ||
[tile2lat(parseInt(y) + 1, zoom), tile2lon(parseInt(x) + 1, zoom)], | ||
], | ||
{ | ||
color: "#ff7800", | ||
weight: 1, | ||
} | ||
).addTo(map); | ||
}); | ||
|
||
// convert lat lng to tile lon | ||
function tile2lon(x, z) { | ||
return (x / Math.pow(2, z)) * 360 - 180; | ||
} | ||
|
||
// convert lat lng to tile lat | ||
function tile2lat(y, z) { | ||
const n = Math.PI - (2 * Math.PI * y) / Math.pow(2, z); | ||
return (180 / Math.PI) * Math.atan(0.5 * (Math.exp(n) - Math.exp(-n))); | ||
} | ||
|
||
// generate tile lat, lng | ||
function getTileNumber(lat, lng, zoom) { | ||
const xtile = parseInt(Math.floor(((lng + 180) / 360) * (1 << zoom))); | ||
const ytile = parseInt( | ||
Math.floor( | ||
((1 - | ||
Math.log( | ||
Math.tan((lat * Math.PI) / 180) + 1 / Math.cos((lat * Math.PI) / 180) | ||
) / | ||
Math.PI) / | ||
2) * | ||
(1 << zoom) | ||
) | ||
); | ||
return { x: xtile, y: ytile }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
*, | ||
:after, | ||
:before { | ||
box-sizing: border-box; | ||
padding: 0; | ||
margin: 0; | ||
} | ||
|
||
html { | ||
height: 100%; | ||
} | ||
|
||
body, | ||
html, | ||
#map { | ||
width: 100%; | ||
height: 100%; | ||
} | ||
|
||
body { | ||
position: relative; | ||
min-height: 100%; | ||
margin: 0; | ||
padding: 0; | ||
background-color: #f1f1f1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>change tile style when click</title> | ||
<link | ||
rel="stylesheet" | ||
href="https://unpkg.com/[email protected]/dist/leaflet.css" | ||
/> | ||
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script> | ||
<link rel="stylesheet" href="style.css" /> | ||
</head> | ||
|
||
<body> | ||
<div id="map"></div> | ||
<script src="script.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* eslint-disable no-undef */ | ||
/** | ||
* change tile style when click | ||
*/ | ||
|
||
// config map | ||
let config = { | ||
minZoom: 7, | ||
maxZoom: 18, | ||
}; | ||
// magnification with which the map will start | ||
const zoom = 18; | ||
// co-ordinates | ||
const lat = 52.22977; | ||
const lng = 21.01178; | ||
|
||
// calling map | ||
const map = L.map("map", config).setView([lat, lng], zoom); | ||
|
||
// Used to load and display tile layers on the map | ||
// Most tile servers require attribution, which you can set under `Layer` | ||
L.tileLayer("https://tile.openstreetmap.org/{z}/{x}/{y}.png", { | ||
attribution: | ||
'© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors', | ||
}).addTo(map); | ||
|
||
// ------------------------------------------------------ | ||
// click on map | ||
|
||
map.on("click", (e) => { | ||
const { lat, lng } = e.latlng; | ||
|
||
// find image with specific src | ||
const img = document.querySelector( | ||
`img[src="https://tile.openstreetmap.org/${getTileNumber(lat, lng)}.png"]` | ||
); | ||
|
||
img.style.filter = "grayscale(100%)"; | ||
img.style.border = "1px solid #000"; | ||
img.style.borderRadius = "10px"; | ||
img.style.padding = "5px"; | ||
img.style.backgroundColor = "white"; | ||
}); | ||
|
||
// convert lat, lng to tile number | ||
function getTileNumber(lat, lng) { | ||
const currentZoom = map.getZoom(); | ||
|
||
const xtile = parseInt(Math.floor(((lng + 180) / 360) * (1 << currentZoom))); | ||
const ytile = parseInt( | ||
Math.floor( | ||
((1 - | ||
Math.log( | ||
Math.tan((lat * Math.PI) / 180) + 1 / Math.cos((lat * Math.PI) / 180) | ||
) / | ||
Math.PI) / | ||
2) * | ||
(1 << currentZoom) | ||
) | ||
); | ||
return `${currentZoom}/${xtile}/${ytile}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
*, | ||
:after, | ||
:before { | ||
box-sizing: border-box; | ||
padding: 0; | ||
margin: 0; | ||
} | ||
|
||
html { | ||
height: 100%; | ||
} | ||
|
||
body, | ||
html, | ||
#map { | ||
width: 100%; | ||
height: 100%; | ||
} | ||
|
||
body { | ||
position: relative; | ||
min-height: 100%; | ||
margin: 0; | ||
padding: 0; | ||
background-color: #f1f1f1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters