[GIS] Different icon of the nearest marker to current location in Leaflet

javascriptleaflet

I'm working with Leaflet and JavaScript for the first time, so I feel a bit lost here. I have an app that displays user's location and a couple of points of interest from a GeoJSON on a map. The points have a popup bound to them that displays information when clicked.

Now I would like the marker for the closest POI to the user be different than the other markers. I have also managed to find the closest point using L.GeometryUtil.

//GEOJSON LAYER
//----------
function onEachFeature(feature, layer){
    if (feature.properties && feature.properties.name) {
        layer.bindPopup(feature.properties.name);
    }
}

var geojsonLayer = L.geoJSON(pivovary, {
    pointToLayer: function (feature, latlng) {
        return L.marker(latlng, {icon: beerMarker})},
    onEachFeature: onEachFeature
}).addTo(map);

//USER LOCATION
//----------
var closestpt = null
map.locate({setView: true, maxZoom: 16});
function onLocationFound(e){
    L.marker(e.latlng, {icon: youAreHereMarker}).addTo(map),
    closestpt = L.GeometryUtil.closestLayer(map, geojsonLayer.getLayers(), e.latlng).latlng
}

map.on('locationfound', onLocationFound)

I tried to add a new marker at the closestpt coordinates in onLocationFound, but then I am just placing a new marker on top of one that is already there and I cannot use the popup that is bound to my geojsonLayer anyway.

Is there a way to render the markers after I find the closest point?

Best Answer

You could do it by adding markers to layer group and later looking for closest marker in the group, something like this:

var markerGroupLayer = L.layerGroup();

var geojsonLayer = L.geoJSON(pivovary, {
    pointToLayer: function (feature, latlng) {
        var marker = L.marker(latlng, {icon: beerMarker});
        markerGroupLayer.addLayer(marker);
        return marker;},
    onEachFeature: onEachFeature
}).addTo(map);

var closestMarker = null;
map.locate({setView: true, maxZoom: 16});
function onLocationFound(e){
    L.marker(e.latlng, {icon: youAreHereMarker}).addTo(map),
    closestMarker = L.GeometryUtil.closestLayer(map, markerGroupLayer.getLayers(), e.latlng);
    closestMarker.setIcon(closestMarkerIcon);
}

I haven't tried it, but it should work. What is still missing is logic of changing icon of previous closest marker to default icon and handling of case where several markers are at equal closest distance.