arcgis for js 之 获取两点之间的距离

时间:2024-03-23 22:34:20

换了新公司,接触新行业,半路出家,看着别人的代码,看着api慢慢理解。

需求如下;已知两点坐标求距离。

思路,没有,站在同事的肩膀上踩路子,给的这个链接 https://developers.arcgis.com/javascript/latest/api-reference/esri-tasks-GeometryService.html#distance

综合看完,基本调用如下:

new GeometryService(url).distance(new DistanceParameters(distanceUnit,geodesic,geometry1,geometry2),function(){})

注意:这个是基于地图的,地图一定得打开;geometry1 看是在哪种多边形下,两点的距离 是在 esri/geometry/Point 模块下的;创建方法为

getTwoPointDis(x1,y1,x2,y2,cb)  //传入点参数

function getTwoPointDis(x1,y1,x2,y2,cb){
  var p1,p2;
  map.task.getNewPoint(x1,y1,function(e){
    p1 = e
  });
  map.task.getNewPoint(x2,y2,function(e){
    p2 = e
  });
  var dd = new DistanceParam();
  dd.geometry1 = p1;
  dd.geometry2 = p2;
  map.task.geometryService(dd, function(e){
    var e = e.toFixed(3);
    cb(e)
  });
}

geometryService:(dd,successCallBack,errorCallBack){  //计算距离参数

  var distanceParameters = new DistanceParameters();
  distanceParameters.geometry1 = param.geometry1;
  distanceParameters.geometry2 = param.geometry2;
  distanceParameters.geodesic = param.geodesic;
  distanceParameters.distanceUnit = param.distanceUnit;
  geoService.distance(distanceParameters, successCallBack, errorCallBack);

}

 

getNewPoint:function(x,y,successCallBack){  //初始化点
  var p1 = new esri.geometry.Point();
  p1.x = x;
  p1.y = y;
  p1.spatialReference = this._impl._map.spatialReference;
  successCallBack(p1)
}