【问题标题】:How to the Leaflet CoordsToLatLngs to draw a polyline onto a map?如何使用 Leaflet CoordsToLatLngs 在地图上绘制折线?
【发布时间】:2021-08-11 12:30:13
【问题描述】:

我有一个 geoJson 文件,其中包含一组坐标,我正在尝试使用传单在地图上制作多段线。

我的代码最初可以工作,但后来我意识到我的 geoJson 文件中的经度和纬度是相反的,所以这将我带到了不同的坐标。

我尝试使用 coordsToLatLngs() 方法来反转这些,但一直收到一个错误,即该方法未定义为静态:

Uncaught ReferenceError: coordsToLatLngs is not defined

[这是本节传单文档的链接]1https://leafletjs.com/reference-0.7.7.html#geojson

关于如何使用此方法以便我可以切换经纬度的任何指导?

本节我的代码:

var latLngCoords = coordsToLatLngs(value.coords, 0, false);
var polyline = L.polyline(latLngCoords, {color: 'red'}).addTo(mymap);
mymap.fitBounds(polyline.getBounds());
                            ```

【问题讨论】:

  • v0.7.7 好像8岁了;您是否使用不同版本的库及其文档?考虑改用 v1.7.1。
  • 我使用的是 1.7.1,只是在这里使用了一个旧链接 - 使用给出的答案设法修复它!谢谢

标签: javascript leaflet maps static-methods polyline


【解决方案1】:

coordsToLatLngs 未导入全局范围

试着这样跑

L.coordsToLatLngs(value.coords, 0, false)

L.GeoJSON.coordsToLatLngs(value.coords, 0, false)

或者,有 turf.js 库https://turfjs.org/docs/#flip 带有翻转功能

let coords = turf.lineString(latLngCoords)
let newCoords = turf.flip(coords)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-23
    • 2020-03-05
    • 1970-01-01
    • 1970-01-01
    • 2016-01-05
    • 2023-04-02
    • 2012-11-22
    • 1970-01-01
    相关资源
    最近更新 更多