我们正在构建一个使用 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 小部件结果)
如果我要再次选择这两个点,它会生成正确的距离