我们正在构建一个使用esri地图的Web应用程序。我想要做的是计算两点之间的距离。这些点的坐标位于两个独立的json对象中。接下来,我通过解析来自这两个json对象的纬度和经度值来生成两个esri点。 Esri点需要空间参考,因此我将mapView的空间参考设置为两点的空间参考。
问题:当我选择两个点时,它会完全重写其中一个点的纬度和经度。这导致两点的荒谬计算。
这就是我尝试过的
//calling the distance function
distance = this.distanceCalculator(incidentCenter, residence);
//distance calc function
private distanceCalculator(firstPt, secondPt): any {
//this.measureLayer.removeAll();
// When calculating the distance betweeen two points , we need to decypher the points coming in and attach a spatial reference.
const pointA: esri.Point = new this.Point({
spatialReference: this.mapView$.value.spatialReference,
latitude: firstPt.latitude,
longitude: firstPt.longitude
});
//so whats happening here is that
const pointB: esri.Point = new this.Point({
spatialReference: this.mapView$.value.spatialReference,
latitude: secondPt.latitude,
longitude: secondPt.longitude
});
console.log("Point a and B");
console.log("point a: ", pointA);
console.log("point b: ", pointB);
// Next we call the GemoetryEngine distance function and calculate the distance between the two points.
try {
const miles = this.GeometryEngine.distance(pointA, pointB, 'kilometers');
const kms = miles * this.MilesToKm;
return kms.toFixed(2);
} catch (e) {
this.logger.error('Error indistanceCalculator ', e);
}
}
截图
实际距离应该是以下(esri小部件结果)
如果我再次选择两个点,它会生成正确的距离
对我来说,面对时刻,我在代码中设置lat和long错误的一点导致了这个错误。谢谢大家的帮助。