I've added a polygon layer and a point layer to my leaflet map.
I would like the points to appear after a certain zoom level or when a "click" event occurs e.g. (polygons.on("click", function....)
I have looked at different questions here and also this example
But I am still unable to understand what to do in my code.
Here is my code:
<script type="text/javascript">
var mapboxTiles = L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '<a href="http://www.mapbox.com/about/maps/" target="_blank">Terms & Feedback</a>'
});
var map = L.map('map')
.addLayer(mapboxTiles)
.setView([42.444508, -76.499491], 4);
var polygons = new L.geoJson(null, {
pointToLayer: function (feature, latlng) {
return L.marker(latlng, {});
}
});
var points = new L.geoJson(null, {
pointToLayer: function (feature, latlng) {
return L.marker(latlng, {});
}
});
points.bindPopup("popup.").openPopup();
polygons.on("click", function (event) {
map.fitBounds(event.layer.getBounds());
});
polygons.addTo(map);
points.addTo(map);
$.getJSON('polygons.php', function (data) {
polygons.addData(data)
});
$.getJSON('points.php', function (data) {
points.addData(data)
});
</script>