大家早上好,谢谢你的帮助。 我正在通过传单库创建的地图页面(map.html)中创建,该文件从名为water_well.js的外部geojson文件中获取数据。之前由立交服务生成的此文件只是一个标记列表。每个标记都有一些特性。遵循一个例子:
"properties": {
"operator:type": "international",
"is_in:district": "west_mamprusi",
"is_in:region": "northern",
"source:date": "2012-02-11",
"source:ele": "gps",
"water_wells:source_type": "borehole"
},
主页面之前使用此javascript从文件中提取这些数据:
var wwMarker = L.geoJson(water_well, {
pointToLayer : function (feature, latlng) {
lat = feature.geometry.coordinates[0];
lng = feature.geometry.coordinates[1];
//following code that make error
op_type = feature.properties.operator_type;
district = feature.properties.is_in:district;
region = feature.properties.is_in:region;
source_date = feature.properties.source:date;
source_ele = feature.properties.source:ele;
source_type = feature.properties.water_wells:source_type;
.....
我确定问题是我的Zero javascript知识,但我不是程序员,我为我在布基纳法索从事水井的非政府组织做了这张地图。
提取数据的脚本在这一点上不起作用:
op_type = feature.properties.operator:type;
问题是“:”因为字符无效。
第二个问题是,第一个名为water_well.js的文件中并非所有标记都具有相同的“属性”填充广告实际上有人可能拥有不同的“属性组”这两个:
{
"type": "Feature",
"id": "node/1606958159",
"properties": {
"@id": "node/1606958159",
"amenity": "drinking_water",
"man_made": "water_well",
"name": "puits 4"
},
"geometry": {
"type": "Point",
"coordinates": [
-3.6235696,
12.02171
]
}
},
{
"type": "Feature",
"id": "node/1913126817",
"properties": {
"@id": "node/1913126817",
"ele": "170.8000030517578",
"grid_proximity": "grid_further_500_m",
"is_in:district": "builsa",
"is_in:region": "upper_east",
"man_made": "water_well",
"operational_status": "open",
"operator:type": "individual",
"pipe_connection": "no",
"pump": "manual",
"seasonal": "another_pattern",
"source": "MVP,Columbia University",
"source:date": "2012-02-14",
"source:ele": "gps",
"water_wells:source_type": "unprotected_well"
},
"geometry": {
"type": "Point",
"coordinates": [
-1.2430456,
10.3233693
]
}
},
也许有可能独立提取每个项目的所有属性,不管是哪一个......这可能是解决问题的更好方法,但我不知道该怎么做。
这就是我所做的(点击水龙头查看弹出窗口):www.h2openmap.org/map
这几乎就是我想做的事情(点击水龙头看弹出窗口):overpass-turbo.eu/s/7Ov
感谢您花时间阅读我的问题。 每个人都有美好的一天,弗朗西斯科
答案 0 :(得分:0)
您可以使用括号来访问这些属性,而不是使用:
district = feature.properties.is_in:district;
使用括号:
district = feature.properties['is_in:district'];
对属性访问者的引用:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors
现在,如果您想根据属性是否存在而执行某些操作,则可以在对象上使用hasOwnProperty
方法。由于feature.properties
是一个对象,您可以在条件中使用它:
if (features.properties.hasOwnProperty('is_in:district')) {
// Property exists, do stuff
}
// or
if (!features.properties.hasOwnProperty('is_in:district')) {
// Property does not exist, do stuff
}
如果您想根据其他多个属性执行某些操作,可以使用&&
(和)运算符:
if (features.properties.hasOwnProperty('is_in:district') &&
features.properties.hasOwnProperty('source:data')) {
// Properties exist, do stuff
}
// Or
if (!features.properties.hasOwnProperty('is_in:district') &&
!features.properties.hasOwnProperty('source:data')) {
// Properties do not exist, do stuff
}
您可以使用||
(或)运算符来查看是否至少有一个条件匹配:
if (features.properties.hasOwnProperty('is_in:district') ||
features.properties.hasOwnProperty('source:data')) {
// At least one of the properties exist, do stuff
}
// Or
if (!features.properties.hasOwnProperty('is_in:district') ||
!features.properties.hasOwnProperty('source:data')) {
// At least one of the properties does not exist, do stuff
}
参考此内容可以在"逻辑运算符":https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators
下找到您可以使用类似构建(或不构建)弹出窗口所需的数据对象的内容。希望有所帮助。