Skip to content

Instantly share code, notes, and snippets.

@codefist
Last active June 20, 2019 12:23
Show Gist options
  • Save codefist/222d34636b06b4453e238e86cd9fa4ca to your computer and use it in GitHub Desktop.
Save codefist/222d34636b06b4453e238e86cd9fa4ca to your computer and use it in GitHub Desktop.
Leaflet implementation example
import '../css/leaflet.css';
import * as L from './leaflet-src.esm.js';
import axios from 'axios';
window.addEventListener('load', () => {
const maxTheMap = () => {
let map = document.getElementById("map");
let header_height = document.getElementById("site-header").clientHeight;
// map.style.width = "";
map.style.height = (window.innerHeight - header_height) + "px";
}
maxTheMap();
const map = L.map('map', {
preferCanvas: true,
center: [43.0389,-87.90647],
zoom: 13
});
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', {
maxZoom: 18,
id: 'YOUR_MAPBOX_ID',
accessToken: 'YOUR_MAPBOX_ACCESS_TOKEN'
}).addTo(map);
const markerLayer = L.layerGroup();
markerLayer.addTo(map);
const updateMarkers = (map) => {
axios({
method: 'get',
url: '/api/markers/in_bounds',
params: {
nw_lat: map.getBounds().getNorthWest().lat,
nw_lng: map.getBounds().getNorthWest().lng,
se_lat: map.getBounds().getSouthEast().lat,
se_lng: map.getBounds().getSouthEast().lng
}
})
.then( response => {
// remove out of bounds markers
markerLayer.getLayers().forEach( m => {
if ( m.getLatLng().lat > map.getBounds().getNorthWest().lat
|| m.getLatLng().lat < map.getBounds().getSouthEast().lat
|| m.getLatLng().lng < map.getBounds().getNorthWest().lng
|| m.getLatLng().lng > map.getBounds().getSouthEast().lng
) {
markerLayer.removeLayer(m);
}
});
// iterate over new markers for current bounds
// for each new marker, iterate over all existing
// add if not already in markerLayer, do nothing if found
response.data.markers.forEach( responseMarker => {
// iterate over each existing Markers
// only add if not found in markerLayer
let alreadyFound = markerLayer.getLayers().reduce( (bool_acc, existingMarker) => {
return bool_acc || ( responseMarker.id == existingMarker.options.id );
}, false);
if ( !alreadyFound ) {
L.marker([responseMarker.lat, responseMarker.lng], {id: responseMarker.id}).addTo(markerLayer);
}
});
});
}
map.addEventListener('moveend', (e) => {
updateMarkers(e.sourceTarget);
});
window.addEventListener('resize', maxTheMap);
updateMarkers(map);
})
@codefist
Copy link
Author

the main thing I wanted to share here was the handling of duplicate map markers returned by the api.
the api endpoint is returning all markers within the given map bounds every time the map stops at a new position.
we only want to render a marker on the map one time...
basically I maintain a "layerGroup" (line 29) and add or remove markers to that group intelligently.

also, note that I am using the modular (esm) version of the leaflet library. (import * as L from './leaflet-src.esm.js';)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment