简体   繁体   English

javascript 将 json 数据迭代到 geojson 中以获得 leaflet

[英]javascript iterate json data into geojson for leaflet

I have some json data that I want to use in a leaflet map.我有一些 json 数据想在 leaflet map 中使用。 I need to convert the json format into the geojson format used by leaflet.我需要将 json 格式转换为 leaflet 使用的 geojson 格式。 I also want to sort the data into Friends and Family so I can identify them separately on the map.我还想将数据分类为朋友和家人,以便我可以在 map 上分别识别它们。

I have succesfuly fetch(ed) the data file and used a switch staement to sort the data into Friends case and Family case.我已成功获取(编辑)数据文件并使用开关状态将数据分类为朋友案例和家庭案例。 Inside each case I handle converting the data into geojson format.在每种情况下,我都会将数据转换为 geojson 格式。

My problem is that I have not been able to figure out how to use the variables myFriends and myFamily outside of the function getData so that I can plot the points on the map.我的问题是我无法弄清楚如何在 function getData 之外使用变量 myFriends 和 myFamily,以便我可以 plot Z1D78DC8ED51214E518B5114FEZ 上的点。

This is all just fake data that I am using to learn how to use leaflet.这只是我用来学习如何使用 leaflet 的假数据。 Ultimately I hope to get this data through a stream to track up to the moment position data.最终,我希望通过 stream 获取这些数据,以跟踪当前的 position 数据。

In my code I am able to output the data to the console by moving the console.log(myFriends);在我的代码中,我可以通过移动console.log(myFriends); inside the function. function 内部。

What I am trying to get 2 variables myFriends and myFamily each containing all of the iterated data.我试图获得 2 个变量 myFriends 和 myFamily ,每个变量都包含所有迭代数据。

Original JSON Data原装JSON资料

[
    {
"time" : 0.00, 
"ID" : 51, 
"name" : "John",
"relationship" : "Friend",
"Lat" : 56.166609, 
"Long" : 27.157364
},
{
    "time" : 0.00, 
    "ID" : 52, 
    "name" : "Sally",
    "relationship" : "Friend",
    "Lat" : 55.895501, 
    "Long" : 26.753631
    },
    {
        "time" : 0.00, 
        "ID" : 50, 
        "name" : "Tom",
        "relationship" : "Family",
        "Lat" : 56.236112,
        "Long" : 26.168675
        }
]

var myFriends = var myFriends =

{
    "type": "FeatureCollection",
    "features": [
        {
            "geometry": {
                "type": "Point",
                "coordinates": [
                   56.166609, 27.157364
                ]
            },
            "type": "Feature",
            "properties": {
                "popupContent": "John"
            },
            "id": 51
        },
        {
            "geometry": {
                "type": "Point",
                "coordinates": [
                   55.895501, 26.753631
                ]
            },
            "type": "Feature",
            "properties": {
                "popupContent": "Sally"
            },
            "id": 52
        },
        
    ]
};

var myFamily = var myFamily =

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {
                "popupContent": "Tom"
            },
            "geometry": {
                "type": "Point",
                "coordinates": [56.236112, 26.168675]
            }
        },
       
    ]
};

Program Code程序代码

async function getData() {

const response = await fetch('data.json');
var data = await response.json();
//console.log(data);
for (var i = 0; i < data.length; i++){
    var unit = data[i];

var relationship = unit.relationship;

switch (relationship) {
  case "Friends":
    var myFriends = {
        "type": "FeatureCollection",
        "features": [
            {
                "geometry": {
                "type": "Point",
                "coordinates": [
                unit.Lat + " , " + unit.Long
                ]
                            },
                "type": "Feature",
                "properties": {
                "popupContent": unit.name
                            },
                "id": unit.ID
            },     
                ]
                    };

    break;
  case "Family":
    console.log("Family");
    console.log(unit);  

}

    }
}

getData();


console.log(myFriends);

The scope of your var myFriends is limited to the function getData().您的 var myFriends的 scope 仅限于 function getData()。 To access it outside of getData() you need to return it at the end.要在 getData() 之外访问它,您需要在最后返回它。

Your function is an asynchronous function, which means it returns a promise.您的 function 是异步 function,这意味着它返回 promise。 Therefore, when you call your function you need to wait for its result using await.因此,当您调用 function 时,您需要使用 await 等待其结果。 To make it work you need to为了使它工作,你需要

  1. modify your function to return the data you want.修改您的 function 以返回您想要的数据。

  2. use await where you call your getData function在调用 getData function 的地方使用 await

    var result = await getData(); var 结果 = 等待 getData();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM