如果我有地图,我画一个像这样的矩形: map.setView([14.378300,24.904200],5);
var tileLayer = L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 17,
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
});
tileLayer.addTo(map);
var rectangle = L.rectangle([ [21.616579, 29.487305], [7.798079, 20.522461]]);
map.addLayer(rectangle);
但是在另一个按钮上单击我想以另一种形式重新定义(重新绘制)矩形参数。 像这样:
$( ".select" ).click(function() {
rectangle = new L.rectangle([ [17.853290, 34.980469], [10.876465, 14.853516]]);
map.addLayer(rectangle);
});
这可能吗?拜托,有人帮助我:(
编辑:
当我定义两个矩形时,我尝试这样做,并且我为第二个矩形定义了两个on事件,但我收到此错误Uncaught TypeError: rectangle2.on is not a function
你可以帮我吗?
var rectangle, rectangle2;
rectangle =L.rectangle([ [21.616579, 29.487305], [7.798079, 20.522461]]);
map.addLayer(rectangle);
rectangle.editing.enable();
// Every time we move the selected area, refresh data about the selected area.
rectangle.on('edit', function() {
onRectangleChange(this);
});
rectangle2.on('edit', function() {
onRectangleChange(this);
});
$( ".select" ).click(function() {
rectangle.editing.disable();
map.removeLayer(rectangle);
rectangle2 = new L.rectangle([ [17.853290, 34.980469], [10.876465, 14.853516]]);
map.addLayer(rectangle2);
rectangle2.editing.enable();
});
答案 0 :(得分:2)
您的代码正在运行,只需插入jquery即可 https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js
并在旋转map.removeLayer(rectangle);
$( ".select" ).click(function() {
map.removeLayer(rectangle);
rectangle = new L.rectangle([ [17.853290, 34.980469], [10.876465, 14.853516]]);
map.addLayer(rectangle);
});