Javascript - 返回 2 个多边形时的点击问题
Javascript - onclick problem when 2 polygon are returned
我只是 javascript 的初学者。我尝试使用波兰 WKT 数据库更改传单中生成地图的方法。它有效,但仅当返回 1 个位置时。当返回更多位置时,Onclick 不起作用,f.ex。当您搜索 "Koty 4" - 有 2 个村庄的地块编号为 4。我做错了什么?
function load(){
map = L.map('mapa').setView([52, 19], 6);
L.tileLayer('https://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
var wmsLayer = L.tileLayer.wms('https://integracja.gugik.gov.pl/cgi-bin/KrajowaIntegracjaEwidencjiGruntow', {
layers: 'geoportal,dzialki,numery_dzialek,budynki',format: 'image/png', transparent: true,
}).addTo(map);
window.osmMap = map;
}
function addPolygon(points){
var gPoints = new Array();
var _p = points.split(',');
var cX = 0, cY = 0;
var p0 = null;
var gPoints2 = new Array();
var geojson_pgons = Terraformer.WKT.parse(geomWKT);
var xMin = 1000, yMin = 1000, xMax = -1000, yMax = -1000;
for(var i=0;i<_p.length;i++){
var point = _p[i].split(' ');
if(p0==null) p0 = point;
cX += parseFloat(point[0]);
cY += parseFloat(point[1]);
xMin = Math.min(xMin,point[0]);
xMax = Math.max(xMax,point[0]);
yMin = Math.min(yMin,point[1]);
yMax = Math.max(yMax,point[1]);
gPoints.push(new Array(point[1],point[0]));
}
map.eachLayer(function(polygon) {
if( polygon instanceof L.GeoJSON )
map.removeLayer(polygon);
});
var polygon = L.geoJson(geojson_pgons, {}).addTo(window.osmMap);
var bounds = geojson_pgons.bbox();
window.osmMap.fitBounds([ [bounds[1], bounds[0]], [bounds[3], bounds[2]] ]);
将您的代码更改为:
var fg;
function load(){
//...
fg = L.featureGroup().addTo(map);
}
function addPolygon(points){
var gPoints = new Array();
var _p = points.split(',');
fg.clearLayers() //Deleting the old polygons
var poly = L.polygon([]).addTo(fg);
for(var i=0;i<_p.length;i++){
var point = _p[i].split(' ');
poly.addLatLng([point[1],point[0]]);
}
var bounds = poly.getBounds();
window.osmMap.fitBounds(bounds);
}
如果将多边形添加到要素组,您可以调用 fg.clearLayers()
从地图中删除旧的多边形。
我只是 javascript 的初学者。我尝试使用波兰 WKT 数据库更改传单中生成地图的方法。它有效,但仅当返回 1 个位置时。当返回更多位置时,Onclick 不起作用,f.ex。当您搜索 "Koty 4" - 有 2 个村庄的地块编号为 4。我做错了什么?
function load(){
map = L.map('mapa').setView([52, 19], 6);
L.tileLayer('https://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
var wmsLayer = L.tileLayer.wms('https://integracja.gugik.gov.pl/cgi-bin/KrajowaIntegracjaEwidencjiGruntow', {
layers: 'geoportal,dzialki,numery_dzialek,budynki',format: 'image/png', transparent: true,
}).addTo(map);
window.osmMap = map;
}
function addPolygon(points){
var gPoints = new Array();
var _p = points.split(',');
var cX = 0, cY = 0;
var p0 = null;
var gPoints2 = new Array();
var geojson_pgons = Terraformer.WKT.parse(geomWKT);
var xMin = 1000, yMin = 1000, xMax = -1000, yMax = -1000;
for(var i=0;i<_p.length;i++){
var point = _p[i].split(' ');
if(p0==null) p0 = point;
cX += parseFloat(point[0]);
cY += parseFloat(point[1]);
xMin = Math.min(xMin,point[0]);
xMax = Math.max(xMax,point[0]);
yMin = Math.min(yMin,point[1]);
yMax = Math.max(yMax,point[1]);
gPoints.push(new Array(point[1],point[0]));
}
map.eachLayer(function(polygon) {
if( polygon instanceof L.GeoJSON )
map.removeLayer(polygon);
});
var polygon = L.geoJson(geojson_pgons, {}).addTo(window.osmMap);
var bounds = geojson_pgons.bbox();
window.osmMap.fitBounds([ [bounds[1], bounds[0]], [bounds[3], bounds[2]] ]);
将您的代码更改为:
var fg;
function load(){
//...
fg = L.featureGroup().addTo(map);
}
function addPolygon(points){
var gPoints = new Array();
var _p = points.split(',');
fg.clearLayers() //Deleting the old polygons
var poly = L.polygon([]).addTo(fg);
for(var i=0;i<_p.length;i++){
var point = _p[i].split(' ');
poly.addLatLng([point[1],point[0]]);
}
var bounds = poly.getBounds();
window.osmMap.fitBounds(bounds);
}
如果将多边形添加到要素组,您可以调用 fg.clearLayers()
从地图中删除旧的多边形。