Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Leaflet : remove layer if zoom level greater than 9

Tags:

leaflet

I built a heatmap on Leaflet.
My first goal is to see the heatmap when you open the map. The second goal is not to view the heatmap if the zoom level is greater than 9.

I tried this :

if (map.getZoom() >9 {
    map.removeLayer (heatmapLayer);     
};

But it did not work.
Would you have any suggestions ?
Thanks !


Here is the code :

<!DOCTYPE html>
<html lang="en">
<head>
<title>Application - version 1.0</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">

<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.6.3/leaflet.css" />
<link rel="stylesheet" href="style_p.css" />

<style type="text/css"> 
html, body, #map {
margin: 0;
margin-top: 0%;
width: 100%;
height: 100%;
};

</style>

</head>

<!-- Favicon -->
<link rel="icon" href="california.ico" />


<body>

<div id="map"></div>

<script src="http://cdn.leafletjs.com/leaflet-0.6.3/leaflet-src.js"></script>
<script src="http://maps.google.com/maps/api/js?v=3.2&sensor=false"></script>
<script src="jquery.js"></script>
<script type="text/javascript" src="heatmap.js"></script>
<script type="text/javascript" src="heatmap-leaflet.js"></script>
<script type="text/javascript" src="sloopjohnb.js"></script>
<script src="google.js"></script>
<script src="leaflet_search.js"></script>

<script type="text/javascript" charset="utf-8">  

$(function() {


var osm = new L.TileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png');
var base = new L.TileLayer('http://129.206.74.245:8001/tms_r.ashx?x={x}&y={y}&z={z}');
var ggl2 = new L.Google('SATELLITE');

var heatmapLayer;
heatmapLayer = L.TileLayer.heatMap({
radius: 10,
opacity: 0.8,
gradient: {
0.45: "rgb(0,0,255)",
0.55: "rgb(0,255,255)",
0.65: "rgb(0,255,0)",
0.95: "yellow",
1.0: "rgb(255,0,0)"
}
});


var Data1={
max: 1,
data: sloopjohnb 
};
heatmapLayer.addData(Data1.data);

var baseMaps = {
"Fond OSM": osm,
"Fond de carte de base": base,
"Photo. aérienne Google" : ggl2

};

var overlayMaps = {
'Heatmap': heatmapLayer
};

map = new L.Map('map', {
minZoom : 1,
maxZoom : 11,
boxZoom : true,
layers: [base, heatmapLayer]
});

var controls = L.control.layers(baseMaps, overlayMaps, {position: 'bottomright'});
controls.addTo(map);

map.addControl(L.control.search());
L.control.scale().addTo(map);

map.attributionControl.addAttribution('<a href="http://www.patrick wied.at/static/heatmapjs/">Heatmap.js</a>');
map.setView(new L.LatLng(39.291,-5.9765),2);


// Disparition de la heatmap en fct du zoom
map.on('zoomend', function () {
if (map.getZoom() > 9) {
map.removeLayer(heatmapLayer);
}
}); 

});

</script>

</body>
</html>
like image 376
Julien Avatar asked Apr 11 '14 15:04

Julien


People also ask

How to change zoom level in Leaflet?

A leaflet map has several ways to control the zoom level shown, but the most obvious one is setZoom() . For example, map. setZoom(0); will set the zoom level of map to 0 .

How do you remove zoom control in Leaflet?

Zoom − By default, this control exists at the top left corner of the map. It has two buttons "+" and "–", using which you can zoom-in or zoom-out the map. You can remove the default zoom control by setting the zoomControl option of the map options to false.

What are layers in Leaflet?

In Leaflet anything that can be added to the map is a layer. So polygons, circles, markers, popups, tiles are all layers. You can combine layers in a L. LayerGroup (or FeatureGroup), if you for example want to treat a set of polygons as a single layer.


2 Answers

Are you sure you are creating the listener correctly?

For example, this seems like it should be called when the user zooms. So something like:

Edited

map.on('zoomend', function () {
    if (map.getZoom() > 9 && map.hasLayer(heatmapLayer)) {
        map.removeLayer(heatmapLayer);
    }
    if (map.getZoom() < 9 && map.hasLayer(heatmapLayer) == false)
    {
        map.addLayer(heatmapLayer);
    }   
});


If you ONLY want the layer added/removed based on zoom, don't add it to your layer control here:

var controls = L.control.layers(baseMaps, overlayMaps, {position: 'bottomright'});

controls.addTo(map);
You will just have to make sure you are doing your adding/removing when it is necessary. Try messing with this set up a little and see where you get. Also, the Documentation is well written regarding the use of L.tileLayer

like image 129
Ju66ernaut Avatar answered Jan 04 '23 04:01

Ju66ernaut


You can use the leaflet-zoom-show-hide library to do it. This will automatically show/hide your layers as you zoom.

zsh = new ZoomShowHide(map);
var marker1 = L.marker([50.096, 14.425]);
//                    minzoom, maxzoom
zsh.addLayer(marker1, 10, 13);
var marker2 = L.marker([50.076, 14.425]);
zsh.addLayer(marker2, 8, null);
like image 38
timthelion Avatar answered Jan 04 '23 04:01

timthelion