如何使用传单为传单实时插件设置自定义图标
How to set custom icon for Leaflet Realtime plugin with Leaflet?
我是传单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/
相关文章:
- 如何使用传单为传单实时插件设置自定义图标
- 无法调用方法'应用'关于为jquery谷歌地图插件设置方向的未定义
- 在jQuery UI选项卡中显示WordPress插件设置
- FullCalendar jQuery 插件 - 设置今天的日期
- JavaScript 插件设置优化
- 如何使用 eclipse-mars IDE 将 jquery 插件设置到我的 codeigniter 项目中
- 使用名为虔诚的jQuery图表插件设置多个if条件
- 如何使用angularjs中的自定义指令,使用Unform jQuery插件设置浏览器默认复选框的样式
- 如何在babel 6中定义插件设置
- jQuery XPath插件设置属性
- bootstrap TimePicker插件:设置完整的小时/分/秒
- jQuery cookie插件-设置和检索加载
- Jquery插件/设置间隔和范围问题
- 使用Iframe -resizer插件设置高度为0的Iframe
- 在jQuery插件设置中使用特殊字符
- 使用jquery cookie插件设置body类
- 为YUI日历插件设置时区
- 将jquery自动完成插件设置为自动提交
- jQuery selectionBox插件-设置默认文本选择选项
- jSlider jQuery插件:设置最小范围,并格式化最小值