Skip to content

Commit ddeceb3

Browse files
authored
Infowindow rectify coordinate Considering altitude (#2206)
* Infowindow rectify coordinate Considering altitude * fix lint * updates
1 parent 116bd93 commit ddeceb3

File tree

1 file changed

+54
-18
lines changed

1 file changed

+54
-18
lines changed

src/ui/InfoWindow.js

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { isFunction, isNumber, isObject, isString } from '../core/util';
22
import { createEl, addDomEvent, removeDomEvent, on, off } from '../core/util/dom';
3+
import Coordinate from '../geo/Coordinate';
34
import Point from '../geo/Point';
45
import Size from '../geo/Size';
56
import { Geometry, Marker, MultiPoint, LineString, MultiLineString } from '../geometry';
@@ -336,10 +337,16 @@ class InfoWindow extends UIComponent {
336337
}
337338

338339
_rectifyLineStringMouseCoordinate(lineString, mouseCoordinate) {
339-
const pts = lineString.getCoordinates().map(coordinate => {
340-
return this.getMap().coordToContainerPoint(coordinate);
340+
const map = this.getMap();
341+
const coordinates = lineString.getCoordinates() || [];
342+
const glRes = map.getGLRes();
343+
//coordinates to containerpoints
344+
const pts = coordinates.map(coordinate => {
345+
const renderPoints = map.coordToPointAtRes(coordinate, glRes);
346+
const altitude = coordinate.z || 0;
347+
return map._pointAtResToContainerPoint(renderPoints, glRes, altitude);
341348
});
342-
const mousePt = this.getMap().coordToContainerPoint(mouseCoordinate);
349+
const mousePt = map.coordToContainerPoint(mouseCoordinate);
343350
let minDis = Infinity, coordinateIndex = -1;
344351
// Find the point with the shortest distance
345352
for (let i = 0, len = pts.length; i < len; i++) {
@@ -350,24 +357,28 @@ class InfoWindow extends UIComponent {
350357
coordinateIndex = i;
351358
}
352359
}
353-
const filterPts = [];
354-
if (coordinateIndex === 0) {
355-
filterPts.push(pts[0], pts[1]);
356-
} else if (coordinateIndex === pts.length - 1) {
357-
filterPts.push(pts[coordinateIndex - 1], pts[coordinateIndex]);
358-
} else {
359-
filterPts.push(pts[coordinateIndex - 1], pts[coordinateIndex], pts[coordinateIndex + 1]);
360-
}
360+
const indexs = [coordinateIndex - 1, coordinateIndex, coordinateIndex + 1].filter(index => {
361+
return index >= 0 && index <= pts.length - 1;
362+
});
363+
364+
const filterPts = indexs.map(index => {
365+
return pts[index];
366+
});
361367
const xys = [];
362-
const { width, height } = this.getMap().getSize();
368+
const { width, height } = map.getSize();
363369
//Calculate all pixels in the field of view
364370
for (let i = 0, len = filterPts.length - 1; i < len; i++) {
371+
const coordinateIndex = i;
365372
const pt1 = filterPts[i], pt2 = filterPts[i + 1];
373+
// Vertical line
366374
if (pt1.x === pt2.x) {
367375
const miny = Math.max(0, Math.min(pt1.y, pt2.y));
368376
const maxy = Math.min(height, Math.max(pt1.y, pt2.y));
369377
for (let y = miny; y <= maxy; y++) {
370-
xys.push(new Point(pt1.x, y));
378+
xys.push({
379+
point: new Point(pt1.x, y),
380+
coordinateIndex
381+
});
371382
}
372383
} else {
373384
const k = (pt2.y - pt1.y) / (pt2.x - pt1.x);
@@ -377,23 +388,48 @@ class InfoWindow extends UIComponent {
377388
const maxx = Math.min(width, Math.max(pt1.x, pt2.x));
378389
for (let x = minx; x <= maxx; x++) {
379390
const y = k * (x - pt1.x) + pt1.y;
380-
xys.push(new Point(x, y));
391+
xys.push({
392+
point: new Point(x, y),
393+
coordinateIndex
394+
});
381395
}
382396
}
383397
}
384-
let minPtDis = Infinity, ptIndex = -1;
398+
let minPtDis = Infinity, ptIndex = -1, index = -1, containerPoint;
385399
// Find the point with the shortest distance
386400
for (let i = 0, len = xys.length; i < len; i++) {
387-
const pt = xys[i];
388-
const dis = mousePt.distanceTo(pt);
401+
const { point, coordinateIndex } = xys[i];
402+
const dis = mousePt.distanceTo(point);
389403
if (dis < minPtDis) {
390404
minPtDis = dis;
391405
ptIndex = i;
406+
index = coordinateIndex;
407+
containerPoint = point;
392408
}
393409
}
410+
if (ptIndex < 0) {
411+
return {
412+
dis: minPtDis,
413+
coordinate: mouseCoordinate
414+
};
415+
}
416+
// const coordinate = map.containerPointToCoord(containerPoint);
417+
const p1 = filterPts[index], p2 = filterPts[index + 1];
418+
const distance = p1.distanceTo(p2);
419+
const d = containerPoint.distanceTo(p1);
420+
const percent = d / distance;
421+
422+
const filterCoordinates = indexs.map(index => {
423+
return coordinates[index];
424+
});
425+
const c1 = filterCoordinates[index], c2 = filterCoordinates[index + 1];
426+
const x1 = c1.x, y1 = c1.y, z1 = c1.z || 0;
427+
const x2 = c2.x, y2 = c2.y, z2 = c2.z || 0;
428+
const dx = x2 - x1, dy = y2 - y1, dz = z2 - z1;
429+
const x = x1 + dx * percent, y = y1 + dy * percent, z = z1 + dz * percent;
394430
return {
395431
dis: minPtDis,
396-
coordinate: ptIndex < 0 ? mouseCoordinate : this.getMap().containerPointToCoord(xys[ptIndex])
432+
coordinate: new Coordinate(x, y, z)
397433
};
398434
}
399435

0 commit comments

Comments
 (0)