编程语言
首页 > 编程语言> > arcgis api 3.x for js 入门开发系列一自写算法实现地图量算工具(附源码下载)

arcgis api 3.x for js 入门开发系列一自写算法实现地图量算工具(附源码下载)

作者:互联网

0.内容概览

  1. Geometry 地图服务方式实现地图距离以及面积的量算,简单描述
  1. arcgis api 提供的接口类 geometryEngine 实现地图距离以及面积的量算,简单描述
  2. 自定义距离以及面积算法方式实现地图距离以及面积的量算,重点介绍
  3. 源码下载

1. Geometry 地图服务方式

直接调用本机 ArcGIS Server 发布的 Geometry 服务:
http://localhost:6080/arcgis/rest/services/Utilities/Geometry/GeometryServer
利用该地图服务提供的函数Areas and Lengths,其中 Areas 用于量算面积,Lengths 用于量算距离,如图:

2. geometryEngine 接口类方式

利用arcgis api提供的geometryEngine 接口函数,具体参照官网的api文档介绍:geometryEngine

3.自定义距离以及面积算法方式

自己写算法来实现距离以及面积的量算,这样的好处是不依赖ArcGIS Server 几何服务 Geometry 以及arcgis api 接口类 geometryEngine,灵活应用在WebGIS 项目中,最终的实现效果图如下:

具体实现思路

创建一个独立的js文件,里面有量算工具类 DCIMeature,DCIMeature 类构造函数传入地图对象 map

construct: function (map) {  
    this._dciMap = map;  
    this._onClickHandler = dojo.hitch(this, this._onClickHandler);  
    this._onMouseMoveHandler = dojo.hitch(this, this._onMouseMoveHandler);  
    this._onDrawEndHandler = dojo.hitch(this, this._onDrawEndHandler);  
    this._onExtentChangeHandler = dojo.hitch(this, this._onExtentChangeHandler);  
    this._onGraphicClearHandler = dojo.hitch(this, this._onGraphicClearHandler);  
    this._graphicsLayer = new esri.layers.GraphicsLayer({ id: "DciMeatureGLyr" });  
}
DUtil.getDistanceInEarth = function (point1, point2) { 
var d = new Number(0); 
//1度等于0.0174532925199432957692222222222弧度 
//var radPerDegree=0.0174532925199432957692222222222; 
var radPerDegree = Math.PI / 180.0; 
if (DCI.Measure.map.spatialReference.wkid == "4326") { 
var latLength1 = Math.abs(this.translateLonLatToDistance({ x: point1.x, y: point2.y }).x - this.translateLonLatToDistance({ x: point2.x, y: point2.y }).x); 
var latLength2 = Math.abs(this.translateLonLatToDistance({ x: point1.x, y: point1.y }).x - this.translateLonLatToDistance({ x: point2.x, y: point1.y }).x); 
var lonLength = Math.abs(this.translateLonLatToDistance({ x: point1.x, y: point2.y }).y - this.translateLonLatToDistance({ x: point1.x, y: point1.y }).y); 
d = Math.sqrt(Math.pow(lonLength, 2) - Math.pow(Math.abs(latLength1 - latLength2) / 2, 2) + Math.pow(Math.abs(latLength1 - latLength2) / 2 + Math.min(latLength1, latLength2), 2)); 
} 
else { 
var len_prj = Math.pow((point2.x - point1.x), 2) + Math.pow((point2.y - point1.y), 2); 
        d = Math.sqrt(len_prj); 
} 
d = Math.ceil(d); 
return d; 
}; 
DUtil.translateLonLatToDistance = function (point) { 
var d = new Number(0); 
//1度等于0.0174532925199432957692222222222弧度 
//var radPerDegree=0.0174532925199432957692222222222; 
var radPerDegree = Math.PI / 180.0; 
var equatorialCircumference = Math.PI * 2 * 6378137; 
return { 
        x: Math.cos(point.y * radPerDegree) * equatorialCircumference * Math.abs(point.x / 360), 
        y: equatorialCircumference * Math.abs(point.y / 360) 
}; 
};   
//******求三角形面积**** 
DUtil.getTriangleArea = function (point1, point2, point3) { 
    var area = 0; 
    if (!point1 || !point2 || !point3) { 
        return 0; 
    } 
    if (DCI.Measure.map.spatialReference.wkid == "4326") {   
        point1 = this.translateLonLatToDistance(point1); 
        point2 = this.translateLonLatToDistance(point2); 
        point3 = this.translateLonLatToDistance(point3); 
    } 
    area = ((point1.x * point2.y - point2.x * point1.y) + (point2.x * point3.y - point3.x * point2.y) + (point3.x * point1.y - 
    point1.x * point3.y)) / 2; 
    return area; 
};

4.源码分享

源码下载

标签:point1,js,arcgis,point2,源码,._,var,translateLonLatToDistance,Math
来源: https://blog.csdn.net/liguoweioo/article/details/122296866