如何在Leaflet中获得CountyId或StateId?

时间:2013-10-23 16:38:40

标签: javascript asp.net-mvc leaflet

我想在Leaflet中获得countyId

info.update = function (props) {
        if (props) {
            propsState = props.State;  //StateName
            propsName = props.name;    //CountyName
            propsRanking = props.Ranking;
        }
    };

上面的代码只获取StateName和CountyName。我怎样才能获得CountyId或StateId?

1 个答案:

答案 0 :(得分:0)

您的问题是ID号是属性子对象的一级。 GeoJSON的布局如下:

var statesData = {
    "type": "FeatureCollection",
    "features": [
    {
        "type": "Feature",
        "id": "01",
        "properties": {
            "name": "Alabama",
            "density": 94.65
        },
        "geometry": { 
           //continues on for a long time...

highlightFeature function中的代码(即在MouseOver上运行的代码)传入对象layer.feature.properties。这将成为您函数中的props。如果你看一下GeoJSON,它就比ID值低一级。

要解决此问题,请改为传递layer.feature,然后将您的功能更改为以下内容:

info.update = function (feature) {
    if (feature) {
      var props = feature.properties;
      propsState = props.State;  //StateName
      propsName = props.name;    //CountyName
      propsID = feature.id;  //NOTE we are using feature here, NOT props
      propsRanking = props.Ranking;
    }
};