【问题标题】:Put coordinates in correct format以正确的格式放置坐标
【发布时间】:2018-12-10 17:09:31
【问题描述】:

我正在尝试将来自后端的位置数据放入 react-map-gl,但我遇到了一个小障碍。

    addLines = () => {
    const geo = this.state.geo;
    const insideGeo = geo.map(dataItem => {
        const json = new Buffer(dataItem.payload, "hex").toString();
        if (json !== '')
            return JSON.parse(json);
        return json;
    }).filter(item => (item !== ''));
    const lat = insideGeo.map(item => item.eventData.location.latitude);
    const lon = insideGeo.map(item => item.eventData.location.longitude);
    console.log('Lat:', lat[4]);
    console.log('Lon:', lon[4]);
    const map = this.refs.map.getMap()
    map.addLayer({
        "id": "route",
        "type": "line",
        "source": {
            "type": "geojson",
            "data": {
                "type": "Feature",
                "properties": {},
                "geometry": {
                    "type": "LineString",
                    "coordinates": [
                        [
                            lon[4], lat[4]
                        ],
                        [
                            lon[5], lat[5]
                        ],
                    ]
                }
            }
        },
        "layout": {
            "line-join": "round",
            "line-cap": "round"
        },
        "paint": {
            "line-color": "#18c3ea",
            "line-width": 3
        }
    });
}

Its screen of console log lon and lat 我想把整个坐标像这样:{lon} {lat}

It working if I will put only together specific number.

如何通过?

谢谢

【问题讨论】:

  • 你想要一个每个元素都是 {Lat: 00.000, Lon: 00.000} 的数组,对吗?
  • 需要放{lon}和{lat},不带具体数字。

标签: javascript html react-map-gl


【解决方案1】:

根据您的代码,它似乎可以工作。

数组方法:

替换以下行:

const lat = insideGeo.map(item => item.eventData.location.latitude);
const lon = insideGeo.map(item => item.eventData.location.longitude);

与:

const coords = insideGeo.map(item => [item.eventData.location.latitude, item.eventData.location.longitude]);

您可以像这样访问数字:

const lat = coords[index][0]
const lon = coords[index][1]

对象方法:

替换以下行:

const lat = insideGeo.map(item => item.eventData.location.latitude);
const lon = insideGeo.map(item => item.eventData.location.longitude);

与:

const coords = insideGeo.map(item => item.eventData.location);

您可以像这样访问数字:

const lat = coords[index].latitude
const lon = coords[index].longitude

【讨论】:

  • 它还给定高度 0:{纬度:19.43075,经度:-98.0992,高度:9} “坐标”:[ [ lon[4], lat[4] ], ] 如果我们可以工作将放置至少两个这样的坐标: 47.836119, 28.992924 “坐标”: [ [ 47.836119, 28.992924 ], [ 44.836119, 21.992924 ], ]
  • 然后它会在这两个坐标之间给我们一条线。我也想做同样的事情并在 57 个坐标之间划线。
  • 我编辑了插入数组的方法,这是你想要的吗?
猜你喜欢
  • 1970-01-01
  • 2022-07-29
  • 2012-06-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多