结合 geojson 和 json 传单
Combining geojson and json for leaftlet
我有一张带有地理Json图层的传单地图
var objJson = "https://raw.githubusercontent.com/salucci/Leaflet-Teste/master/BrasilNovo.json";
geojsonLayer = new L.GeoJSON.AJAX(objJson, { style: style,
onEachFeature: onEachFeature});
geojsonLayer.addTo(map);
info.addTo(map);
还有一个 Ajax 请求从本地 PHP 服务器接收 Json 数据。
$.ajax({
url: "http://127.0.0.1/projects/phpController.php",
type: "POST",
dataType: "json",
data: {"Codigo": 1100023},
success: function(data){
console.log(data); //here is my data
},
error: function(error){
console.log("Error:");
console.log(error);
}
});
GeoJson有点重,所以我不想每次都在服务器上生成整个GeoJson,想法是合并static GeoJson 和 dynamic Json 通过 ID(类似于 SQL 加入)在 Ajax 请求之后然后将合并后的对象放在 Leaflet Layer
GeoJson 看起来像:
{"type":"FeatureCollection","features":[
{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[-73,-7],[-73,-8]]]},"properties":{"field1":"value1","field2":"value2","ID":"1"}},
{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[-73,-7],[-73,-9]]]},"properties":{"field1":"value1","field2":"value2","ID":"2"}},
{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[-73,-7],[-73,-11]]]},"properties":{"field1":"value1","field2":"value2","ID":"3"}}]}
来自 Ajax 请求的 Json 看起来像:
[{"id":"1","field3":"value3","field4":"value4"},{"id":"2","field3":"value3","field4":"value4"},{"id":"3","field3":"value3","field4":"value4"}]
所以基本上我想将字段 field3 和 field4 及其值放入通过 id 连接的 GeoJson 属性中。使用 javascript 执行此操作的 best/fastest 方法是什么?
有没有办法在运行时稍后合并另一个(第三个)Json?
当 Leaflet 解析您的 GeoJSON 数据并从中构建一个 GeoJSON 层组(您已存储在 geojsonLayer
变量中)时,它将特征数据记录到 feature
属性 每个对应层。
因此,例如,在您的 geojsonLayer
中,您将得到(除其他外)一个多边形:(以下称为“layer
”)
layer.feature.type // "Feature"
layer.feature.geometry // {"type":"Polygon","coordinates":[[[-73,-7],[-73,-8]]]}
layer.feature.properties // {"field1":"value1","field2":"value2","ID":"1"}
例如你可以这样做:
geojsonLayer.eachLayer(function (layer) {
if (layer.feature.properties.ID === jsonObj.id) {
for (var key in jsonObj) {
layer.feature.properties[key] = jsonObj[key];
}
}
});
当然你可以改进你的算法来缓存对你的 Leaflet 层的引用,而不是每次都循环 geojsonLayer
。
我有一张带有地理Json图层的传单地图
var objJson = "https://raw.githubusercontent.com/salucci/Leaflet-Teste/master/BrasilNovo.json";
geojsonLayer = new L.GeoJSON.AJAX(objJson, { style: style,
onEachFeature: onEachFeature});
geojsonLayer.addTo(map);
info.addTo(map);
还有一个 Ajax 请求从本地 PHP 服务器接收 Json 数据。
$.ajax({
url: "http://127.0.0.1/projects/phpController.php",
type: "POST",
dataType: "json",
data: {"Codigo": 1100023},
success: function(data){
console.log(data); //here is my data
},
error: function(error){
console.log("Error:");
console.log(error);
}
});
GeoJson有点重,所以我不想每次都在服务器上生成整个GeoJson,想法是合并static GeoJson 和 dynamic Json 通过 ID(类似于 SQL 加入)在 Ajax 请求之后然后将合并后的对象放在 Leaflet Layer
GeoJson 看起来像:
{"type":"FeatureCollection","features":[
{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[-73,-7],[-73,-8]]]},"properties":{"field1":"value1","field2":"value2","ID":"1"}},
{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[-73,-7],[-73,-9]]]},"properties":{"field1":"value1","field2":"value2","ID":"2"}},
{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[-73,-7],[-73,-11]]]},"properties":{"field1":"value1","field2":"value2","ID":"3"}}]}
来自 Ajax 请求的 Json 看起来像:
[{"id":"1","field3":"value3","field4":"value4"},{"id":"2","field3":"value3","field4":"value4"},{"id":"3","field3":"value3","field4":"value4"}]
所以基本上我想将字段 field3 和 field4 及其值放入通过 id 连接的 GeoJson 属性中。使用 javascript 执行此操作的 best/fastest 方法是什么?
有没有办法在运行时稍后合并另一个(第三个)Json?
当 Leaflet 解析您的 GeoJSON 数据并从中构建一个 GeoJSON 层组(您已存储在 geojsonLayer
变量中)时,它将特征数据记录到 feature
属性 每个对应层。
因此,例如,在您的 geojsonLayer
中,您将得到(除其他外)一个多边形:(以下称为“layer
”)
layer.feature.type // "Feature"
layer.feature.geometry // {"type":"Polygon","coordinates":[[[-73,-7],[-73,-8]]]}
layer.feature.properties // {"field1":"value1","field2":"value2","ID":"1"}
例如你可以这样做:
geojsonLayer.eachLayer(function (layer) {
if (layer.feature.properties.ID === jsonObj.id) {
for (var key in jsonObj) {
layer.feature.properties[key] = jsonObj[key];
}
}
});
当然你可以改进你的算法来缓存对你的 Leaflet 层的引用,而不是每次都循环 geojsonLayer
。