gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 21:51:32 25 4
gpt4 key购买 nike

我是 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 函数有效,就像您可以与 L.GeoJSON 一起使用的所有其他选项一样:

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

如果您在选项对象中使用pointToLayer 方法(我猜您试图在源对象中使用它或犯了一个错误),您可以返回一个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

关于javascript - 如何使用 Leaflet 为 Leaflet Realtime 插件设置自定义图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28899176/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com