如何使用传单为传单实时插件设置自定义图标

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

本文关键字:插件 设置 自定义 图标 何使用 单实时 实时      更新时间:2023-09-26

我是传单JS的新手。我正试图找到一种方法来更改传单实时插件中使用的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函数的工作原理与您可以使用L.GeoJSON:的其他选项一样

你基本上可以用L.GeoJSON做任何你能做的事情。实时样式、onEachFeature、获取边界等。

如果您在options对象中使用pointToLayer方法(我猜您试图在源对象中使用它或犯了错误),您可以使用自己的自定义L.Icon:返回L.Marker

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-点对层

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

使用插件页面上给出的示例,我成功地使用pointToLayer对标记进行了样式化。您只需要在包含interval选项的括号内添加函数即可。

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/