perliedman / leaflet-realtime

Put realtime data on a Leaflet map
ISC License
743 stars 167 forks source link

Ho to use existing marker? #166

Closed VladimirKalachikhin closed 4 years ago

VladimirKalachikhin commented 4 years ago

I want to use existing L.marker by some reasons. My code:

var cursor = L.marker(startCenter, {
    'icon': GpsCursor,
});

var realtime = L.realtime(gpsAndDataServerURI, {
    interval: 1 * 1000,
    updateFeature:  function(feature,olgLayer,newLayer) {
        newLayer = cursor;   
                newLayer.setLatLng( L.latLng(feature.geometry.coordinates[1], feature.geometry.coordinates[0]));
    },
}).addTo(map);

It works, but marker icon are default, not me custom. This code:

var realtime = L.realtime(gpsanddataServerURI, {
    interval: 1 * 1000,
    pointToLayer: function (feature, latlng) {
        return cursor.setLatLng(latlng);
    }        
}).addTo(map);

work fine.

What am I doing wrong?

perliedman commented 4 years ago

Hi, it seems you have misunderstood the updateFeature option, it does not take a third argument, it is just called with two arguments. Also, the function should return the layer to be used, either oldLayer or a new instance that you create in updateLayer.