Javascript OSM API路径节点排序不正确
所以我的目标是能够从OSM中提取一条给定的路径,并显示在传单地图上。然而,当我尝试拉一个给定的方式时,节点在响应中的顺序似乎不正确Javascript OSM API路径节点排序不正确,javascript,leaflet,openstreetmap,Javascript,Leaflet,Openstreetmap,所以我的目标是能够从OSM中提取一条给定的路径,并显示在传单地图上。然而,当我尝试拉一个给定的方式时,节点在响应中的顺序似乎不正确 从“axios”导入axios 从“xml2js”导入xml2js 让parser=new xml2js.parser() 导出默认值{ 异步getStpPolygon(){ 让xml=wait axios.get('https://www.openstreetmap.org/api/0.6/way/39394541/full') 返回解析节点(xml) }, 异
从“axios”导入axios
从“xml2js”导入xml2js
让parser=new xml2js.parser()
导出默认值{
异步getStpPolygon(){
让xml=wait axios.get('https://www.openstreetmap.org/api/0.6/way/39394541/full')
返回解析节点(xml)
},
异步getMplsPolygon(){
让xml=wait axios.get('https://www.openstreetmap.org/api/0.6/way/93481561/full')
返回解析节点(xml)
}
}
异步函数解析节点(xml){
返回新承诺((解决、拒绝)=>{
parser.parseString(xml.data,(err,data)=>{
如果(错误)拒绝(错误)
让输出=data.osm.node.map((node)=>{
返回[
parseFloat(节点$.lat),
parseFloat(节点$.lon)
]
})
解析(输出)
})
})
}
以下是XML的外观:
<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.6" generator="CGImap 0.6.1 (18903 thorn-01.openstreetmap.org)" copyright="OpenStreetMap and contributors" attribution="http://www.openstreetmap.org/copyright" license="http://opendatacommons.org/licenses/odbl/1-0/">
<node id="1083701880" visible="true" version="1" changeset="6873749" timestamp="2011-01-05T16:51:40Z" user="neuhausr" uid="16591" lat="44.9751170" lon="-93.2758411"/>
<node id="1083701882" visible="true" version="1" changeset="6873749" timestamp="2011-01-05T16:51:40Z" user="neuhausr" uid="16591" lat="44.9746502" lon="-93.2772842"/>
<node id="1083701938" visible="true" version="1" changeset="6873749" timestamp="2011-01-05T16:51:41Z" user="neuhausr" uid="16591" lat="44.9727679" lon="-93.2778367"/>
<node id="1083701987" visible="true" version="1" changeset="6873749" timestamp="2011-01-05T16:51:42Z" user="neuhausr" uid="16591" lat="44.9730222" lon="-93.2787594"/>
<node id="1083701993" visible="true" version="1" changeset="6873749" timestamp="2011-01-05T16:51:42Z" user="neuhausr" uid="16591" lat="44.9737736" lon="-93.2793709"/>
<node id="1083702026" visible="true" version="1" changeset="6873749" timestamp="2011-01-05T16:51:43Z" user="neuhausr" uid="16591" lat="44.9754130" lon="-93.2765707"/>
<way id="93481561" visible="true" version="1" changeset="6873749" timestamp="2011-01-05T16:51:43Z" user="neuhausr" uid="16591">
<nd ref="1083701993"/>
<nd ref="1083701987"/>
<nd ref="1083701938"/>
<nd ref="1083701880"/>
<nd ref="1083702026"/>
<nd ref="1083701882"/>
<nd ref="1083701993"/>
<tag k="amenity" v="university"/>
<tag k="name" v="University of St. Thomas"/>
</way>
</osm>
还有一个演示
异步函数getStpPolygon(){
const resp=等待axios.get('https://www.openstreetmap.org/api/0.6/way/93481561/full')
constjson=xml2js(resp.data,{compact:true});
常量refs={};
json.osm.node.forEach((node)=>{
const attrs=节点。\u属性;
参考[attrs.id]=[+attrs.lat,+attrs.lon];
});
返回json.osm.way.nd.map((nd)=>{
常数id=nd.\u attributes.ref;
返回引用[id];
});
};
var map=L.map('map').setView([44.97386,-93.27569],15);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'{
属性:“©;贡献者”
}).addTo(地图);
getStpPolygon()。然后((点)=>{
L.多段线(点)。添加到(地图);
});代码>
#地图{宽度:100%;高度:200px}
Ah,因此OSM提供了路径中的顺序,并允许用户将节点映射到路径中的引用。非常感谢您的帮助,修复效果非常好!是的,因为多种方式可以共享相同的节点。文件中
项的顺序不相关。
项目的顺序很重要。
async function parseNodes (xml) {
return new Promise((resolve, reject) => {
parser.parseString(xml.data, (err, data) => {
if (err) reject(err)
//map node ids to their coordinates
const refs = {};
data.osm.node.forEach((node) => {
const attrs = node.$;
refs[attrs.id] = [+attrs.lat, +attrs.lon];
});
// return the coordinates in the correct order
const output = data.osm.way.nd.map((nd) => {
const id = nd.$.ref;
return refs[id];
});
resolve(output)
})
})
}