Skip to content

Commit 506594f

Browse files
committed
fix a bug when calling AreaTool.endDraw, fix #1250
1 parent 851456d commit 506594f

File tree

3 files changed

+116
-8
lines changed

3 files changed

+116
-8
lines changed

src/map/tool/AreaTool.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,32 @@ class AreaTool extends DistanceTool {
112112

113113
_msOnDrawEnd(param) {
114114
this._clearTailMarker();
115-
const prjCoord = this.getMap()._pointToPrj(param['point2d']);
115+
let prjCoord;
116+
if (param['point2d']) {
117+
prjCoord = this.getMap()._pointToPrj(param['point2d']);
118+
} else {
119+
let prjCoords = param['geometry']._getPrjCoordinates();
120+
prjCoords = prjCoords.slice(0, prjCoords.length - 1);
121+
param['geometry']._setPrjCoordinates(prjCoords);
122+
prjCoord = prjCoords[prjCoords.length - 1];
123+
}
124+
if (param['geometry']._getPrjCoordinates().length < 3) {
125+
this._lastMeasure = 0;
126+
this._clearMeasureLayers();
127+
return;
128+
}
129+
116130
const ms = this._measure(param['geometry']);
117-
const endLabel = new Label(ms, param['coordinate'], this.options['labelOptions'])
131+
const projection = this.getMap().getProjection();
132+
const coord = projection.unproject(prjCoord);
133+
const endLabel = new Label(ms, coord, this.options['labelOptions'])
118134
.addTo(this._measureMarkerLayer);
119135
endLabel._setPrjCoordinates(prjCoord);
120136
let size = endLabel.getSize();
121137
if (!size) {
122138
size = new Size(10, 10);
123139
}
124-
this._addClearMarker(param['coordinate'], prjCoord, size['width']);
140+
this._addClearMarker(coord, prjCoord, size['width']);
125141
const geo = param['geometry'].copy();
126142
geo._setPrjCoordinates(param['geometry']._getPrjCoordinates());
127143
geo.addTo(this._measureLineLayer);

src/map/tool/DistanceTool.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,11 @@ class DistanceTool extends DrawTool {
307307

308308
_msOnDrawEnd(param) {
309309
this._clearTailMarker();
310+
if (param['geometry']._getPrjCoordinates().length < 2) {
311+
this._lastMeasure = 0;
312+
this._clearMeasureLayers();
313+
return;
314+
}
310315
let size = this._lastVertex.getSize();
311316
if (!size) {
312317
size = new Size(10, 10);
@@ -362,6 +367,11 @@ class DistanceTool extends DrawTool {
362367
}
363368
}
364369

370+
_clearMeasureLayers() {
371+
this._measureLineLayer.remove();
372+
this._measureMarkerLayer.remove();
373+
}
374+
365375
}
366376

367377
DistanceTool.mergeOptions(options);

test/map/tools/MeasureToolSpec.js

Lines changed: 87 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ describe('DistanceTool and AreaTool', function () {
33
var map;
44
var center = new maptalks.Coordinate(118.846825, 32.046534);
55

6-
function measure(tool) {
6+
function measure(tool, noDblClick) {
77
var center = map.getCenter();
88

99
var domPosition = GET_PAGE_POSITION(container);
@@ -76,10 +76,13 @@ describe('DistanceTool and AreaTool', function () {
7676
'clientX':point.x - 1,
7777
'clientY':point.y + 5
7878
});
79-
happen.dblclick(eventContainer, {
80-
'clientX':point.x - 1,
81-
'clientY':point.y + 5
82-
});
79+
if (!noDblClick) {
80+
happen.dblclick(eventContainer, {
81+
'clientX':point.x - 1,
82+
'clientY':point.y + 5
83+
});
84+
}
85+
8386
if (tool.isEnabled()) {
8487
expect(tool.getLastMeasure()).to.be.above(measure);
8588
measure = tool.getLastMeasure();
@@ -148,6 +151,38 @@ describe('DistanceTool and AreaTool', function () {
148151
result = tool.getLastMeasure();
149152
expect(result).to.be.above(0);
150153
});
154+
155+
it('endDraw', function () {
156+
var distanceTool = new maptalks.DistanceTool({
157+
metric : true,
158+
imperial:true
159+
}).addTo(map);
160+
expect(distanceTool.getLastMeasure()).to.be.eql(0);
161+
measure(distanceTool, true);
162+
distanceTool.endDraw();
163+
expect(distanceTool.getLastMeasure()).to.be.above(0);
164+
});
165+
166+
it('endDraw with 1 click', function () {
167+
var tool = new maptalks.DistanceTool().addTo(map);
168+
var center = map.getCenter();
169+
170+
var domPosition = GET_PAGE_POSITION(container);
171+
var point = map.coordinateToContainerPoint(center).add(domPosition);
172+
173+
var measure = 0;
174+
happen.mousedown(eventContainer, {
175+
'clientX':point.x,
176+
'clientY':point.y
177+
});
178+
happen.click(eventContainer, {
179+
'clientX':point.x,
180+
'clientY':point.y
181+
});
182+
var i;
183+
tool.endDraw();
184+
expect(tool.getLastMeasure()).to.be.eql(0);
185+
});
151186
});
152187

153188
describe('test areaTool', function () {
@@ -221,6 +256,53 @@ describe('DistanceTool and AreaTool', function () {
221256
result = tool.getLastMeasure();
222257
expect(result).to.be.above(0);
223258
});
259+
260+
it('endDraw with 2 clicks', function () {
261+
var tool = new maptalks.AreaTool().addTo(map);
262+
var center = map.getCenter();
263+
264+
var domPosition = GET_PAGE_POSITION(container);
265+
var point = map.coordinateToContainerPoint(center).add(domPosition);
266+
267+
var measure = 0;
268+
happen.mousedown(eventContainer, {
269+
'clientX':point.x,
270+
'clientY':point.y
271+
});
272+
happen.click(eventContainer, {
273+
'clientX':point.x,
274+
'clientY':point.y
275+
});
276+
var i;
277+
for (i = 1; i < 10; i++) {
278+
happen.mousemove(eventContainer, {
279+
'clientX':point.x + i,
280+
'clientY':point.y
281+
});
282+
}
283+
284+
happen.mousedown(eventContainer, {
285+
'clientX':point.x + 10,
286+
'clientY':point.y
287+
});
288+
happen.click(eventContainer, {
289+
'clientX':point.x + 10,
290+
'clientY':point.y
291+
});
292+
tool.endDraw();
293+
expect(tool.getLastMeasure()).to.be.eql(0);
294+
});
295+
296+
it('endDraw', function () {
297+
var tool = new maptalks.AreaTool().addTo(map).disable();
298+
measure(tool);
299+
var result = tool.getLastMeasure();
300+
expect(result).to.be(0);
301+
tool.enable();
302+
measure(tool, true);
303+
tool.endDraw();
304+
expect(tool.getLastMeasure()).to.be.above(0);
305+
});
224306
});
225307

226308

0 commit comments

Comments
 (0)