javascript - 将 javascript 转换为 json?

标签 javascript json geojson

我正在处理传单,我注意到许多示例使用单独的 js 文件,其中变量设置为 JSON 流。

我如何修改以下示例,以便它可以使用 geojson 读取 json 文件,并且在 javascript 中不包含变量声明?

代码如下所示:

<script type="text/javascript">

    var geoJsonData = {
        "type": "FeatureCollection", 
        "features": [
            { "type": "Feature", "id":"1", "properties": { "address": "2"   }, "geometry": { "type": "Point", "coordinates": [175.2209316333,-37.8210922667 ] } },
            { "type": "Feature", "id":"2", "properties": { "address": "151" }, "geometry": { "type": "Point", "coordinates": [175.2238417833,-37.80975435   ] } },
            { "type": "Feature", "id":"3", "properties": { "address": "21"  }, "geometry": { "type": "Point", "coordinates": [175.2169955667,-37.818193     ] } },
            { "type": "Feature", "id":"4", "properties": { "address": "14"  }, "geometry": { "type": "Point", "coordinates": [175.2240856667,-37.8216963    ] } },
            { "type": "Feature", "id":"5", "properties": { "address": "38B" }, "geometry": { "type": "Point", "coordinates": [175.2196982333,-37.8188702167 ] } },
            { "type": "Feature", "id":"6", "properties": { "address": "38"  }, "geometry": { "type": "Point", "coordinates": [175.2209942   ,-37.8192782833 ] } }
        ]
    };

    var tiles = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
        maxZoom: 18,
        attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
    });

    var map = L.map('map')
            .addLayer(tiles);

    var markers = L.markerClusterGroup();

    var geoJsonLayer = L.geoJson(geoJsonData, {
        onEachFeature: function (feature, layer) {
            layer.bindPopup(feature.properties.address);
        }
    });
    markers.addLayer(geoJsonLayer);

    map.addLayer(markers);
    map.fitBounds(markers.getBounds());
</script>

我知道可以使用 $.getJSON 来完成,但如果可能的话,我更喜欢使用 L.geoJson

最佳答案

要从文件中读取 JSON 数据,您可以使用 fetch 函数 ( read more )。

这是一个例子:

fetch('http://example.com/movies.json')
    .then(function(response) {
        return response.json();
    })
    .then(function(myJson) {
        // Do something with the JSON data
        console.table(myJson);
    });

就您而言:

function doSomething(geoJsonData) {
    var tiles = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
        maxZoom: 18,
        attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
    });

    var map = L.map('map')
            .addLayer(tiles);

    var markers = L.markerClusterGroup();

    var geoJsonLayer = L.geoJson(geoJsonData, {
        onEachFeature: function (feature, layer) {
            layer.bindPopup(feature.properties.address);
        }
    });
    markers.addLayer(geoJsonLayer);

    map.addLayer(markers);
    map.fitBounds(markers.getBounds());
}

fetch('http://myserver.com/myfile.json')
    .then(function(response) {
        return response.json();
    })
    .then(doSomething)
    .catch(function(err) {
        // In case of error, display the error message
        console.error(err);
    });

请注意,我将您的代码放入回调函数中,因为 fetch 函数是异步的。

关于javascript - 将 javascript 转换为 json?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52580187/

相关文章:

javascript - 使用 JavaScript 重新加载页面时出现问题

ruby-on-rails - 如何使我的回答达到要求的标准

geojson - GDAL,ogr2ogr "Cannot find proj.db"错误

javascript - 在引导模式内的元素上使用偏移量返回零

java - 为什么AJAX调用Servlet(异步)会被执行多次?

javascript - 将 html 元素本身传递给 javascript 函数

python - 将新数据附加到 JSON

javascript - 循环遍历为创建图表而返回的 JSON

java - 尝试在 Java 中加载 json 文件时出现 NPE

c# - 如何将 GeoJSON 数据绑定(bind)到 ASP.NET Core 中的参数?