Leaflet 带有标记和线条的传单
我使用的是带有geojson的传单JS,但我不能同时绘制带有标记的多段线,所以我的解决方案是先绘制一条多段线,然后添加标记。 我认为这不是一个好办法,那么还有其他解决办法吗 这是我的密码Leaflet 带有标记和线条的传单,leaflet,Leaflet,我使用的是带有geojson的传单JS,但我不能同时绘制带有标记的多段线,所以我的解决方案是先绘制一条多段线,然后添加标记。 我认为这不是一个好办法,那么还有其他解决办法吗 这是我的密码 function DrawLine(mymap,topo){ var line={ "type": "Feature", "geometry": { "type": "LineString", "coordinates" : topo.pointsFor
function DrawLine(mymap,topo){
var line={
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates" : topo.pointsForJson
// topo.pointsForJson is my data source like : [[5.58611,43.296665], [5.614466,43.190604], [5.565922,43.254726], [5.376992,43.302967]]
},
"properties": {
"ID": topo['OMS_IDTOPO'],
"color" : "blue"
}
};
var points=[];
for(var i in topo.pointsForJson){
var point = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates" : topo.pointsForJson[i]
}
};
points.push(point);
}
//add markers
L.geoJSON(points).addTo(mymap);
// add polyline
var polyline = L.geoJSON(line,{
style:function (feature) {
return {color: feature.properties.color}
}
}).bindPopup(function (layer) {
return layer.feature.properties.ID;
}).addTo(mymap);
mymap.fitBounds(polyline.getBounds());
}
非常感谢您真的不需要在运行时首先构建GeoJSON对象来在传单地图上显示某些内容 只需在坐标中循环,并在每一对上建立一个标记 然后从坐标数组中构建一条多段线 您需要在该过程中恢复坐标,因为它们被记录为经度/纬度(符合GeoJSON格式),而传单在直接构建标记和多段线时需要经度/纬度(而不是使用
L.GeoJSON
factory)
var pointsForJson=[
[5.58611, 43.296665],
[5.614466, 43.190604],
[5.565922, 43.254726],
[5.376992, 43.302967]
];
var map=L.map('map');
pointsForJson.forEach(函数(lngLat){
L.标记(lngLatToLatLng(lngLat)).添加(地图);
});
var polyline=L.多段线(lngLatArrayToLatLng(pointsForJson)).addTo(map);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'{
属性:“©;贡献者”
}).addTo(地图);
fitBounds(polyline.getBounds());
函数lngLatArrayToLatLng(lngLatArray){
返回lngLatArray.map(lnglatalatlng);
}
功能lngLatToLatLng(lngLat){
返回[lngLat[1],lngLat[0]];
}
感谢您的回复,所以您认为我仍然需要分别添加标记和行?为什么您认为不是这样?任何自动为您执行此操作的库(如果有的话)都会将它们单独添加到引擎盖下。