如何使用 Leaflet 为 Leaflet Realtime 插件设置自定义图标?

How to set custom icon for Leaflet Realtime plugin with Leaflet?

我是 Leaflet JS 的新手。我正在尝试找出一种方法来更改 Leaflet Realtime 插件中使用的 L.Geojson 标记的默认样式。 我不知道要更改什么 属性 才能更改标记的样式。

到目前为止,这是我的代码:

    var map = L.map('map', {center: [46.4337, 23.4532], zoom: 8}),
    realtime = L.realtime({
        url: 'get_points.php',
        crossOrigin: true,
        type: 'json'
    }, {
        interval: 500
    }).addTo(map);
var osm = new L.TileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png');
map.addLayer(osm);

function update(e) {
    realtime.update(JSON.parse(e.data));
}

function remove(e) {
    realtime.remove(JSON.parse(e.data));
}
realtime.on('update', function(e) {
        popupContent = function(fId) {
            var feature = e.features[fId],
                my_number = feature.properties.number;
                mystatus = feature.properties.mystatus;
            return ('My number is: '+ my_number + '<br />' + 'Status: ' + mystatus) ;
        },
        bindFeaturePopup =  function(fId) {
            realtime.getLayer(fId).bindPopup(popupContent(fId));
        },
        updateFeaturePopup = function(fId) {
            realtime.getLayer(fId).getPopup().setContent(popupContent(fId));
        };
        


    Object.keys(e.enter).forEach(bindFeaturePopup);
    Object.keys(e.update).forEach(updateFeaturePopup);
});

我试过使用自定义图标标记设置 pointToLayer 函数,但没有成功。
谢谢

使用插件页面上给出的示例,我成功地使用 pointToLayer 设置了标记的样式。您只需在包含间隔选项的括号内添加函数。

var realtime = L.realtime({
    url: 'https://wanderdrone.appspot.com/',
    crossOrigin: true,
    type: 'json'
}, {
    interval: 3 * 1000,
    pointToLayer: function (feature, latlng) {
    return L.circleMarker(latlng, geojsonMarkerOptions)
    }
}).addTo(map);

这是一个关于 JSFiddle 的工作示例: http://jsfiddle.net/4usvq7ky/

pointToLayer 函数有效,就像您可以与 L.GeoJSON 一起使用的所有其他选项一样:

You can basically do anything you can do with L.GeoJSON with L.Realtime - styling, onEachFeature, gettings bounds, etc.

如果你在选项对象中使用 pointToLayer 方法(我猜你试图在源对象中使用它或者犯了一个错误),你可以 return a L.Marker 自定义 L.Icon:

var realtime = L.realtime({
    url: 'https://wanderdrone.appspot.com/',
    crossOrigin: true,
    type: 'json'
}, {
    interval: 3 * 1000,
    pointToLayer: function (feature, latlng) {
        return L.marker(latlng, {
            'icon': L.icon({
                iconUrl: '//leafletjs.com/docs/images/leaf-green.png',
                shadowUrl: '//leafletjs.com/docs/images/leaf-shadow.png',
                iconSize:     [38, 95], // size of the icon
                shadowSize:   [50, 64], // size of the shadow
                iconAnchor:   [22, 94], // point of the icon which will correspond to marker's location
                shadowAnchor: [4, 62],  // the same for the shadow
                popupAnchor:  [-3, -76] // point from which the popup should open relative to the iconAnchor
            })
        });
    }
}).addTo(map);

这是一个关于 Plunker 的工作示例:http://plnkr.co/edit/NmtcUa?p=preview

教程:http://leafletjs.com/examples/custom-icons.html

pointToLayer参考:http://leafletjs.com/reference.html#geojson-pointtolayer

L.Icon参考:http://leafletjs.com/reference.html#icon