我想将来自geojson的附加信息绑定到一个传单标记弹出窗口。我从传单文档中查找了一些东西,但它不起作用。
var map = L.map('map').setView([51.9, 7.6], 11);
L.tileLayer('http://{s}.tile.cloudmade.com/5e4495ff4b0d454eb0443225198b7e6c/997/256/{z}/{x}/{y}.png', {
maxZoom: 16
}).addTo(map);
var polygon = {
"type": "Feature",
"properties": {
"name":"City BoundingBox",
"style": {
"color": "#004070",
"weight": 4,
"opacity": 1
}
},
"geometry": {
"type": "Polygon",
"coordinates": [[
[7.5,52.05],
[7.7,51.92],
[7.6,51.84],
[7.4,51.94],
[7.5,52.05]
]]
}
};
var myLayer = L.geoJson().addTo(map);
//myLayer.addData(polygon);
var popup = L.popup();
function onMapClick(e) {
popup
.setLatLng(e.latlng)
.setContent("You clicked the map at " + e.latlng.toString())
.openOn(map);
}
map.on('click', onMapClick);
<?php
$mdjson = file_get_contents("http://xxx/ows?service=WFS&version=1.0.0&outputFormat=JSON&request=GetFeature&typeName=xx:yy&maxFeatures=50");
echo "var geojsonMD = ".$mdjson.";";
?>
myLayer.addData(geojsonMD);
L.geoJson(geojsonMD, {
style: function (feature) {
return {color: feature.properties.color};
},
onEachFeature: function (feature, myLayer) {
layer.bindPopup(feature.properties.description);
}
}).addTo(map);希望你能帮助我。
诚挚的问候。
发布于 2013-01-26 23:38:46
假设服务返回的数据具有与多边形相似的属性,您确实可以将它们添加到同一个图层中。
var myLayer = L.geoJson(geojsonMD, {
style: function (feature) {
return feature.properties.style;
},
onEachFeature: function (feature, layer) {
layer.bindPopup(feature.properties.name);
}
})
myLayer.addData(polygon);
myLayer.addTo(map);http://jsfiddle.net/Wn5Kh/ (没有下载的数据,因为我没有URL)
如果geojsonMD具有不同的特征属性,那么添加两个GeoJson层没有什么问题。一个用于从服务检索的数据,另一个用于多边形。
发布于 2013-01-25 02:34:40
如单张文档中所述,您应该使用"onEachFeature“将包含所需信息的弹出窗口附加到GeoJson的每个功能:
onEachFeature选项是一个函数,在将每个要素添加到GeoJSON层之前,将在每个要素上调用该函数。使用此选项的常见原因是在单击要素时将弹出窗口附加到要素
您可以这样使用它:
var myLayer = L.geoJson(polygon, {
onEachFeature: yourOnEachFeatureFunction
}).addTo(map);
function yourOnEachFeatureFunction(feature, layer){
if (feature.properties.name) {
layer.bindPopup(feature.properties.name);
}
}在本例中,弹出窗口将显示每个单击要素的属性"name“的内容
发布于 2013-01-29 02:47:00
现在它起作用了。我想让leaflet自动从wfs中获取坐标和特征信息,并将它们添加到地图中。
这就是工作代码,谢谢你的帮助=)
<?php
echo "var geojsonMD = ".$mdjson.";";
?>
myLayer.addData(geojsonMD);
var myLayer = L.geoJson(geojsonMD, {
style: function (feature) {
return feature.properties.style;
},
onEachFeature: function (feature, layer) {
var strtype = '';
if (feature.properties.mdtype == 0) {
strtype = 'aaa';
} else if (feature.properties.mdtype == 1) {
strtype = 'bbb';
}
layer.bindPopup('<b>' + feature.properties.mdname + '</b><br>'
+ strtype + '<br><br>'
+ feature.properties.mdadress + '<br>'
+ feature.properties.mdfon);
}
})
myLayer.addTo(map);https://stackoverflow.com/questions/14506989
复制相似问题