在传单

时间:2016-10-19 07:29:20

标签: javascript jquery dictionary leaflet

如果我有地图,我画一个像这样的矩形: map.setView([14.378300,24.904200],5);

var tileLayer = L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
    maxZoom: 17,
    attribution: '&copy; <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);
});

这可能吗?拜托,有人帮助我:(

MY JSFIDDLE is here

编辑:

当我定义两个矩形时,我尝试这样做,并且我为第二个矩形定义了两个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();
});

1 个答案:

答案 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);
});

http://jsfiddle.net/y1nb7sow/2/