WKT对比?

时间:2012-11-09 08:44:43

标签: javascript openlayers

如何在javascript中比较2个众所周知的文本点? 例如,如果点是a = POINT(78.067606 12.994332) , b=POINT(78.067606 12.994332) , 那么如果我们比较(a==b)它会给出错误,而不是真实。为什么? 有什么帮助吗?

1 个答案:

答案 0 :(得分: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');