leaflet加载geojson数据

leaflet加载geojson数据

通用geojson格式

var states = [{
    "type": "Feature",
    "properties": {"party": "Republican"},
    "geometry": {
        "type": "Polygon",
        "coordinates": [[
            [-104.05, 48.99],
            [-97.22,  48.98],
            [-96.58,  45.94],
            [-104.03, 45.94],
            [-104.05, 48.99]
        ]]
    }
}, {
    "type": "Feature",
    "properties": {"party": "Democrat"},
    "geometry": {
        "type": "Polygon",
        "coordinates": [[
            [-109.05, 41.00],
            [-102.06, 40.99],
            [-102.03, 36.99],
            [-109.04, 36.99],
            [-109.05, 41.00]
        ]]
    }
}];

新建geojson图层,绑定数据,添加到地图

var states = [{//geojson格式数据
    "type": "Feature",
    "properties": {"party": "Republican"},
    "geometry": {
        "type": "Polygon",
        "coordinates": [[
            [-104.05, 48.99],
            [-97.22,  48.98],
            [-96.58,  45.94],
            [-104.03, 45.94],
            [-104.05, 48.99]
        ]]
    }
}, {
    "type": "Feature",
    "properties": {"party": "Democrat"},
    "geometry": {
        "type": "Polygon",
        "coordinates": [[
            [-109.05, 41.00],
            [-102.06, 40.99],
            [-102.03, 36.99],
            [-109.04, 36.99],
            [-109.05, 41.00]
        ]]
    }
}];

L.geoJSON(states, {//geojson图层L.geoJSON
    
}).addTo(map)//绑定地图

geojson图层属性

pointToLayer

点的处理方式与折线和多边形不同。针对点提供pointToLayer对点做特殊处理转化为Marker或CircleMarker,默认情况下,为GeoJSON点绘制简单标记。

这里我们使用pointToLayer选项来创建CircleMarker:

var geojsonMarkerOptions = {
    radius: 8,
    fillColor: "#ff7800",
    color: "#000",
    weight: 1,
    opacity: 1,
    fillOpacity: 0.8
};

L.geoJSON(someGeojsonFeature, {
    pointToLayer: function (feature, latlng) {
        return L.circleMarker(latlng, geojsonMarkerOptions);
    }
}).addTo(map);

style
L.geoJSON(states, {
    style: function(feature) {//根据要素属性设置不同样式,可以制作唯一值渲染专题图等
        switch (feature.properties.party) {
            case 'Republican': return {color: "#ff0000"};
            case 'Democrat':   return {color: "#0000ff"};
        }
    }
}).addTo(map);
样式具体配置项,todo专门做一个style文章
onEachFeature

该onEachFeature选项是在将每个要素添加到GeoJSON图层之前调用的功能。使用此选项的常见原因是在单击时为功能附加弹出窗口。

function onEachFeature(feature, layer) {//针对每个要素采取的操作
    // does this feature have a property named popupContent?
    if (feature.properties && feature.properties.popupContent) {
        layer.bindPopup(feature.properties.popupContent);
    }
}

var geojsonFeature = {
    "type": "Feature",
    "properties": {
        "name": "Coors Field",
        "amenity": "Baseball Stadium",
        "popupContent": "This is where the Rockies play!"
    },
    "geometry": {
        "type": "Point",
        "coordinates": [-104.99404, 39.75621]
    }
};

L.geoJSON(geojsonFeature, {
    onEachFeature: onEachFeature//绑定属性
}).addTo(map);
filter

filter选项可用于控制GeoJSON功能的可见性。为此,我们传递一个函数作为filter选项。为GeoJSON图层中的每个要素调用此函数,并传递给feature和layer。然后,您可以使用要素属性中的值通过返回true或来控制可见性false。

根据属性进行过滤是否显示feature

var someFeatures = [{
    "type": "Feature",
    "properties": {
        "name": "Coors Field",
        "show_on_map": true
    },
    "geometry": {
        "type": "Point",
        "coordinates": [-104.99404, 39.75621]
    }
}, {
    "type": "Feature",
    "properties": {
        "name": "Busch Field",
        "show_on_map": false
    },
    "geometry": {
        "type": "Point",
        "coordinates": [-104.98404, 39.74621]
    }
}];

L.geoJSON(someFeatures, {
    filter: function(feature, layer) {
        return feature.properties.show_on_map;//true or false是否显示
    }
}).addTo(map);

发表评论

您的电子邮箱地址不会被公开。

CAPTCHAis initialing...