0

我想在 Leaflet 中获取countyId

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

以上代码仅获取 StateName 和 CountyName。我如何获得 CountyId 或 StateId?

4

1 回答 1

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 上运行的代码,传入 object 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;
    }
};
于 2013-10-24T16:47:58.670 回答