Skip to content

Instantly share code, notes, and snippets.

@yuskesuzki
Last active December 13, 2015 01:42
Show Gist options
  • Save yuskesuzki/5185ebbf7d5f48e806f0 to your computer and use it in GitHub Desktop.
Save yuskesuzki/5185ebbf7d5f48e806f0 to your computer and use it in GitHub Desktop.
Leaflet.js imageoverlay sample no2
<html>
<head>
<style>
html,body{
margin: 0px;
padding: 0px;
}
#map {
width: 100%;
height: 100%;
}
</style>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.js"></script>
</head>
<body>
<div id="map"></div>
<script>
var map = L.map('map', {
maxZoom: 24,
minZoom: 1,
crs: L.CRS.Simple
}).setView([0, 0], 1);
var imageUrl = 'https://farm6.staticflickr.com/5495/9188725689_ac415ed0b5_h.jpg';
var h = 768;
var w = 1280;
var southWest = [0, w / 5];
var northEast = [h / 5, 0];
var imageBounds = new L.LatLngBounds(southWest, northEast);
map.setMaxBounds(new L.LatLngBounds([0, w / 5], [h / 5, 0])); //表示可能範囲
var attributionStr = '';
var radar = L.imageOverlay(
imageUrl,
imageBounds, {
attribution: attributionStr
}).addTo(map);
map.on('zoomend', changeImg);
/**
* ズーム倍率によって L.imageOverlay で表示してる画像を切替える
*/
function changeImg() {
// map.removeLayer(radar);
var img = '';
if(map.getZoom() >= 4) {
img = 'https://farm9.staticflickr.com/8030/8056224185_abae8e0c15_h.jpg';
} else {
img = 'https://farm6.staticflickr.com/5495/9188725689_ac415ed0b5_h.jpg';
}
console.log(map.getZoom());
radar.setUrl(img);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment