首页 > 解决方案 > 如何将坐标传递给 Leaflet 中的 LatLng 函数

问题描述

我试图弄清楚如何将坐标传递给传单中的 L.LatLng 函数,以便它可以映射坐标。

我可以成功加载数据,它看起来像这样:

//This is the structure of the geojson data, as an example
var SanFranciscoData = {"type":"FeatureCollection", "features": [
{"type":"Feature","geometry":{"type":"Point","coordinates":[-96.97593699999999,32.889954000000046]}

//Load in the geojson
d3.json("data/dataPoints.json", function(SFData) {
        var SFData = SanFranciscoData.features
        })

    //pass in coordinates to the L.LatLng leaflet function
    SFData.forEach(function(d) {
        d.latLong = new L.LatLng(d.features.geometry.coordinates[1],
                                d.features.geometry.coordinates[0]);
         })

正如我在示例中看到的那样,我也尝试过:

var coords = SFData.feature.geometry.coordinates;

上述两种方法都给出相同的结果:坐标未定义。我究竟做错了什么?我不确定如何使用对象表示法访问坐标数组以访问纬度/经度。

标签: javascriptarraysjsonleafletgeojson

解决方案


好的..这有效:

SFData.forEach(function(d) {
        var coords = d.geometry.coordinates
        console.log(coords)
        d.latLong = new L.LatLng(coords[1],
                                coords[0]);

    })

似乎需要使用 .forEach 函数定义坐标!


推荐阅读