Skip to content

Instantly share code, notes, and snippets.

@codefist
Created June 19, 2019 17:18
Show Gist options
  • Save codefist/b683fe7c59f9697e00cbd5af7717c499 to your computer and use it in GitHub Desktop.
Save codefist/b683fe7c59f9697e00cbd5af7717c499 to your computer and use it in GitHub Desktop.
Leaflet implementation example
import css from "../css/app.css"
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: 'elouisyoung.0f89goo2',
accessToken: 'pk.eyJ1IjoiZWxvdWlzeW91bmciLCJhIjoiY2lwcGxlaWVpMDQzb2Z3bmN1dmhuMXQxMyJ9.YcVQVtBpfDfKI2KXfil6NA'
}).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);
}
});
});
}
const moveEnd = (e) => {
updateMarkers(e.sourceTarget);
}
map.addEventListener('moveend', moveEnd);
updateMarkers(map);
window.addEventListener('resize', maxTheMap);
})
@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 30) and add or remove markers to that group intelligently

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