如何在Leaflet中使用Leaflet实时插件设置自定义图标?

5

我是Leaflet JS的新手。我正在尝试找到一种方法来更改Leaflet实时插件中使用的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函数,但没有成功。
谢谢。

2个回答

3
pointToLayer 函数与你可以使用的 L.GeoJSON 的其他选项一样有效:

基本上,你可以像对待 L.GeoJSON 一样对待 L.Realtime - 添加样式、onEachFeature、获取边界等。

如果你在选项对象中使用了 pointToLayer 方法(我猜你尝试在源对象中使用它或犯了一个错误),你可以返回带有自定义 L.IconL.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.htmlpointToLayer参考文献:http://leafletjs.com/reference.html#geojson-pointtolayerL.Icon参考文献:http://leafletjs.com/reference.html#icon

谢谢您详细的回答,我第一次尝试时确实犯了一个错误。现在,我成功地更改了图标,但是当JSON更改时,图标颜色不会根据属性而改变。有什么想法吗?以下是代码:`pointToLayer: function (feature, latlng) {return L.marker(latlng, { 'icon': new L.AwesomeNumberMarkers({ number: feature.properties.number, markerColor: feature.properties.mystatus.toLowerCase() })});` - Flaviu
在页面刷新时,标记颜色会改变,但它不会实时改变。 - Flaviu
不用谢,你随时都受欢迎的。这就是SO存在的意义。但是你可以接受答案,这样其他遇到类似问题的人也能找到一个被接受的解决方案。请参阅:http://stackoverflow.com/help/someone-answers。关于你的新问题,那有点离题,评论并不适合这种情况。如果你发布一个新问题来描述你的问题,我们很乐意帮助你。 - iH8

0

通过使用插件页面上提供的示例,我成功地使用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/


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接