You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从responseJSON对象获取数据?GeoJSON访问相关问题咨询

问题解答

(1) 访问responseJSON.features[0].properties.name

你在$.getJSON的回调函数里拿到的data参数,就是解析后的responseJSON对象。直接在回调内部通过data.features[0].properties.name就能访问到对应属性。

示例代码修改:

var XX = $.getJSON("skiareas-polygons.geojson",function(data){
    // 访问第一个feature的name属性
    var firstAreaName = data.features[0].properties.name;
    console.log(firstAreaName); // 打印到控制台查看结果

    // 原有的地图加载代码
    L.geoJson(data, {
        style: function (feature) {
            return { 
                color : 'black',
                weight: 1,
                fill: true,
                fillColor: 'grey',
                fillOpacity: 0.3
            };
        }
    }).addTo(map);
});

(2) 获取GeoJSON格式版本

  • 获取所有索引的GeoJSON:回调里的data本身就是完整的GeoJSON FeatureCollection对象,直接使用即可(比如保存到变量、传递给其他方法)。
  • 获取仅索引0数据的GeoJSON格式:单个Feature需要符合GeoJSON规范,你可以构造对应的GeoJSON对象:
var XX = $.getJSON("skiareas-polygons.geojson",function(data){
    // 构造单个Feature类型的GeoJSON
    var singleFeatureGeoJSON = {
        "type": "Feature",
        "properties": data.features[0].properties,
        "geometry": data.features[0].geometry,
        "id": data.features[0].id // 原feature有id时保留
    };

    // 构造FeatureCollection类型的GeoJSON(仅包含第一个feature)
    var singleFeatureCollection = {
        "type": "FeatureCollection",
        "features": [data.features[0]]
    };

    // 验证:可以用Leaflet加载这个单独的GeoJSON
    L.geoJson(singleFeatureGeoJSON).addTo(map);
});

内容的提问来源于stack exchange,提问作者spreaderman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 09:04:51