1

如何在javascript中比较2个众所周知的文本点?例如,如果这些点是a = POINT(78.067606 12.994332) , b=POINT(78.067606 12.994332) , 然后如果我们比较(a==b)它给出 false,而不是 true 。为什么?有什么帮助吗?

4

1 回答 1

2

您可以比较点的 WKT(这只是两个字符串的比较):

var p1 = new OpenLayers.Geometry.Point(10.123, 10.456);
var p2 = new OpenLayers.Geometry.Point(10.123, 10.456);

var wkt1 = p1.toString();
var wkt2 = p2.toString();

if (wkt1 == wkt2)
    alert('Works as expected!');

有时点可能彼此非常接近,但它们的坐标可能略有不同。另一种选择是控制几何之间的距离:

var p1 = new OpenLayers.Geometry.Point(10.123, 10.456);
var p2 = new OpenLayers.Geometry.Point(10.124, 10.457);

var allowedDist = 0.002;
var dist = p1.distanceTo(p2);

if (dist < allowedDist)
    alert('Points are close enough');
于 2012-11-09T11:24:57.903 回答