将JSON对象转换为geojson数组

时间:2017-04-25 08:09:43

标签: javascript json firebase geojson

我正在尝试从JSON对象(最初是Firebase)在网页地图上显示标记。 这是我的Javascript代码,如何管理将JSON对象转换为geoson数组?

// get character (points) reference from firebase
var refPOI = '{"-KiWVsBzdJ3EW3THO7bK":{"image":"3","lat":165.440135,"lon":-21.669071},"-KiWY9hLU12ebrlH7cXS":{"image":"5","lat":167.516824,"lon":-22.619914},"-KiWY9hMggrqlTNJMWzU":{"image":"8","lat":166.59579,"lon":-20.718007},"-KiWY9mpKP8gx7i8eGpo":{"image":"8","lat":167.100445,"lon":-20.961233},"-KiWY9oqGSHS_8Sdwpda":{"image":"9","lat":167.818109,"lon":-21.460377},"-KiWYA3hGwTUJGQjYx7O":{"image":"9","lat":165.458005,"lon":-21.770834}}';

var dataPOI = JSON.parse(refPOI);

var arr = [];

for(var x in dataPOI){
arr.push(dataPOI[x]);
}

console.log(dataPOI);
console.log(arr);

var geojson = {
        "type": "FeatureCollection",
        "features": [
        ]
    };

dataPOI.$loaded().then(function(results) {

    for(x=0;x<results.length;x++) {
        geojson.features.push({

            "type": "Feature",
            "properties": {
                "message": results[x].points,
                "image": results[x].image,
                "iconSize": [50, 50],
                "id": results[x].$id
            },
            "geometry": {
            "type": "Point",
            "coordinates": [
                results[x].lat,
                results[x].lon
                ]
            }
        });
    }
})

0 个答案:

没有答案