文档章节

arcgis jsapi接口入门系列(0):总览

c
 canneljls
发布于 10/17 17:16
字数 2151
阅读 8
收藏 0

开发环境:

arcgis jsapi版本4.9

由于我们这套代码是基于vue,webpack开发的,会有少数vue代码,但总体不影响

里面还有些我们公司的js库和html css,给出的代码不能百分百直接运行,主要还是接口的总体介绍

 

代码中常见且不知来源的变量说明:

this.map和this.mapView:就是地图的map和mapView

this.apiInstance:这是公司框架封装的用于实例化arcgis jsapi对象的,不是必须的,例如new this.apiInstance.Extent,用原版jsapi就改成new Extent

 

以下是所有文章代码引用到的一个通用js文件,在此统一贴出代码

//地图通用操作


let comm = {
    //图层
    layer: {
        /**
         * 创建天地图图层
         * 只支持经纬度坐标(需要平面的麦卡托坐标可以自行修改)
         * @param tdtLayerType 天地图图层类型,支持以下值:img=影像——经纬度,cia=影像注记——经纬度,vec=矢量——经纬度,cva=矢量注记——经纬度
         * ter=地形图——经纬度,cta=地形图注记——经纬度
         */
        createTdtLayer: function (apiInstance, tdtLayerType, layerConfig) {
            //切片配置
            //PS:加载天地图图层本质上是用自定义切片图层
            var tileInfo = new apiInstance.TileInfo({
                "rows": 256,
                "cols": 256,
                "compressionQuality": 0,
                //切片原点
                "origin": {
                    "x": -180,
                    "y": 90
                },
                //坐标系
                "spatialReference": {
                    //本方法只支持经纬度坐标,坐标系为wgs84
                    "wkid": 4326
                },
                "lods": [
                    //天地图固定的切片级别
                    {"level": 2, "resolution": 0.3515625, "scale": 147748796.52937502},
                    {"level": 3, "resolution": 0.17578125, "scale": 73874398.264687508},
                    {"level": 4, "resolution": 0.087890625, "scale": 36937199.132343754},
                    {"level": 5, "resolution": 0.0439453125, "scale": 18468599.566171877},
                    {"level": 6, "resolution": 0.02197265625, "scale": 9234299.7830859385},
                    {"level": 7, "resolution": 0.010986328125, "scale": 4617149.8915429693},
                    {"level": 8, "resolution": 0.0054931640625, "scale": 2308574.9457714846},
                    {"level": 9, "resolution": 0.00274658203125, "scale": 1154287.4728857423},
                    {"level": 10, "resolution": 0.001373291015625, "scale": 577143.73644287116},
                    {"level": 11, "resolution": 0.0006866455078125, "scale": 288571.86822143558},
                    {"level": 12, "resolution": 0.00034332275390625, "scale": 144285.93411071779},
                    {"level": 13, "resolution": 0.000171661376953125, "scale": 72142.967055358895},
                    {"level": 14, "resolution": 8.58306884765625e-005, "scale": 36071.483527679447},
                    {"level": 15, "resolution": 4.291534423828125e-005, "scale": 18035.741763839724},
                    {"level": 16, "resolution": 2.1457672119140625e-005, "scale": 9017.8708819198619},
                    {"level": 17, "resolution": 1.0728836059570313e-005, "scale": 4508.9354409599309},
                    {"level": 18, "resolution": 5.3644180297851563e-006, "scale": 2254.4677204799655},
                    {"level": 19, "resolution": 2.68220901489257815e-006, "scale": 1127.23386023998275},
                    {"level": 20, "resolution": 1.341104507446289075e-006, "scale": 563.616930119991375},
                    {"level": 21, "resolution": 6.705522537231445375e-007, "scale": 281.8084650599956875},
                    {"level": 22, "resolution": 3.3527612686157226875e-007, "scale": 140.90423252999784375},
                    {"level": 23, "resolution": 1.67638063430786134375e-007, "scale": 70.452116264998921875},
                    {"level": 24, "resolution": 8.38190317153930671875e-008, "scale": 35.2260581324994609375}
                ]
            });
            //设置图层全图范围
            let spatialReference = new apiInstance.SpatialReference({wkid: 4326});
            let fullExtent = new apiInstance.Extent(-180.0, -90.0, 180.0, 90.0, spatialReference);

            //图层配置
            layerConfig = layerConfig || {};
            $.extend(layerConfig, {
                //天地图切片的url模板
                urlTemplate: "http://{subDomain}.tianditu.cn/" + tdtLayerType + "_c/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=" + tdtLayerType + "&STYLE=default&TILEMATRIXSET=c&TILEMATRIX={level}&TILEROW={row}&TILECOL={col}&FORMAT=tiles",
                subDomains: ["t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7"],
                copyright: "",
                spatialReference: spatialReference,
                fullExtent: fullExtent,
                tileInfo: tileInfo,
            });

            //根据图层配置新建图层
            let layer = new apiInstance.WebTileLayer(layerConfig);

            return layer;
        },
        /**
         * 创建图形图层并添加到地图
         * @param apiInstance
         * @param map 地图
         * @param layerConfig 图层配置
         */
        createGraphicsLayerAndAddToMap: function (apiInstance, map, layerConfig) {
            let layer = new apiInstance.GraphicsLayer(layerConfig);
            //图层添加到地图
            map.add(layer);

            return layer;
        },
    },
    //几何
    geometry: {
        /**
         * wkt转点的图形(Graphic)
         * @param apiInstance api
         * @param wkt wkt
         * @param style 样式
         * @param sr 空间参考
         * @param attributes 属性字段值(可空)
         */
        wktToPointGraphic: function (apiInstance, wkt, style, sr, attributes) {
            //wkt转点的几何对象
            let point = comm.geometry.wktToPoint(apiInstance, wkt, sr);

            //生成点样式
            let markerSymbol = new apiInstance.SimpleMarkerSymbol(
                style,
            );

            //生成图形
            let graphic = new apiInstance.Graphic({
                //几何对象
                geometry: point,
                //样式
                symbol: markerSymbol,
                //属性字段
                attributes: attributes,
            });

            return graphic;
        },
        /**
         * wkt转点的文字的图形(Graphic)
         * @param apiInstance api
         * @param wkt wkt
         * @param style 样式
         * @param sr 空间参考
         * @param attributes 属性字段值(可空)
         */
        wktToTextGraphic: function (apiInstance, wkt, style, sr, attributes) {
            //wkt转点的几何对象
            let point = comm.geometry.wktToPoint(apiInstance, wkt, sr);

            //生成点样式,TextSymbol=文字样式
            let markerSymbol = new apiInstance.TextSymbol(
                style,
            );

            //生成图形
            let graphic = new apiInstance.Graphic({
                //几何对象
                geometry: point,
                //样式
                symbol: markerSymbol,
                //属性字段
                attributes: attributes,
            });

            return graphic;
        },
        /**
         * wkt转点的图片的图形(Graphic)
         * @param apiInstance api
         * @param wkt wkt
         * @param style 样式
         * @param sr 空间参考
         * @param attributes 属性字段值(可空)
         */
        wktToPicGraphic: function (apiInstance, wkt, style, sr, attributes) {
            //wkt转点的几何对象
            let point = comm.geometry.wktToPoint(apiInstance, wkt, sr);

            //生成点样式,PictureMarkerSymbol=图片点样式
            let pictureMarkerSymbol = new apiInstance.PictureMarkerSymbol(
                style,
            );

            //生成图形
            let graphic = new apiInstance.Graphic({
                //几何对象
                geometry: point,
                //样式
                symbol: pictureMarkerSymbol,
                //属性字段
                attributes: attributes,
            });

            return graphic;
        },
        /**
         * wkt转点几何对象
         * @param apiInstance api
         * @param wkt wkt
         * @param sr 空间参考
         * @returns {apiInstance.Polyline}
         */
        wktToPoint: function (apiInstance, wkt, sr) {
            //wkt转坐标对象
            let points = comm.geometry.wktToCoords(wkt);

            //生成线
            let point = new apiInstance.Point({
                //坐标
                x: points[0],
                y: points[1],
                //空间参考
                spatialReference: sr
            });

            return point;
        },
        /**
         * 坐标集合转点几何对象
         * @param apiInstance api
         * @param wkt wkt
         * @param sr 空间参考
         * @returns {apiInstance.Polyline}
         */
        coordToPoint: function (apiInstance, coord, sr) {
            //生成线
            let point = new apiInstance.Point({
                //坐标
                x: coord[0],
                y: coord[1],
                //空间参考
                spatialReference: sr
            });

            return point;
        },
        /**
         * 根据x y新建点几何对象
         * @param apiInstance api
         * @param wkt wkt
         * @param sr 空间参考
         * @returns {apiInstance.Polyline}
         */
        xyToPoint: function (apiInstance, x, y, sr) {
            //生成线
            let point = new apiInstance.Point({
                //坐标
                x: x,
                y: y,
                //空间参考
                spatialReference: sr
            });

            return point;
        },
        /**
         * 点转wkt
         * @param geometry
         * @returns {string}
         */
        pointToWkt: function (geometry) {
            return "POINT (" + geometry.x + " " + geometry.y + ")";
        },
        /**
         * 线几何对象转线的图形(Graphic)
         * @param apiInstance api
         * @param wkt wkt
         * @param style 样式
         * @param sr 空间参考
         * @param attributes 属性字段值(可空)
         */
        polylineToPolylineGraphic: function (apiInstance, geometry, style, sr, attributes) {
            //生成线样式
            let lineSymbol = new apiInstance.SimpleLineSymbol(
                style,
            );

            //生成图形
            let graphic = new apiInstance.Graphic({
                //几何对象
                geometry: geometry,
                //样式
                symbol: lineSymbol,
                //属性字段
                attributes: attributes,
            });

            return graphic;
        },
        /**
         * wkt转线的图形(Graphic)
         * @param apiInstance api
         * @param wkt wkt
         * @param style 样式
         * @param sr 空间参考
         * @param attributes 属性字段值(可空)
         */
        wktToPolylineGraphic: function (apiInstance, wkt, style, sr, attributes) {
            //wkt转线的几何对象
            let polyline = comm.geometry.wktToPolyline(apiInstance, wkt, sr);

            //生成线样式
            let lineSymbol = new apiInstance.SimpleLineSymbol(
                style,
            );

            //生成图形
            let graphic = new apiInstance.Graphic({
                //几何对象
                geometry: polyline,
                //样式
                symbol: lineSymbol,
                //属性字段
                attributes: attributes,
            });

            return graphic;
        },
        /**
         * wkt转线几何对象
         * @param apiInstance api
         * @param wkt wkt
         * @param sr 空间参考
         * @returns {apiInstance.Polyline}
         */
        wktToPolyline: function (apiInstance, wkt, sr) {
            //wkt转坐标对象
            let points = comm.geometry.wktToCoords(wkt);

            //生成线
            let polyline = new apiInstance.Polyline({
                //坐标
                paths: points,
                //空间参考
                spatialReference: sr
            });

            return polyline;
        },
        /**
         * 坐标转线几何对象
         * @param apiInstance api
         * @param coord 坐标对象,格式如:[[[113.545949, 22.24015749], [113.56989, 22.24916], [113.55324, 22.220588]]]
         * @param sr 空间参考
         * @returns {apiInstance.Polyline}
         */
        coordToPolyline: function (apiInstance, coord, sr) {
            //生成线
            let polyline = new apiInstance.Polyline({
                //坐标
                paths: coord,
                //空间参考
                spatialReference: sr
            });

            return polyline;
        },
        /**
         * 线转wkt
         * @param geometry
         * @returns {string}
         */
        polylineToWkt: function (geometry) {
            var wkt = [];
            var paths = geometry.paths;
            for (var i in paths) {
                var path = paths[i];
                for (var j in path) {
                    var p = path[j];
                    wkt.push(p.join(" "));
                }
            }
            return "LINESTRING (" + wkt.join(",") + ")";
        },
        /**
         * wkt转面的图形(Graphic)
         * @param apiInstance api
         * @param wkt wkt
         * @param style 样式
         * @param sr 空间参考
         * @param attributes 属性字段值(可空)
         */
        wktToPolygonGraphic: function (apiInstance, wkt, style, sr, attributes) {
            //wkt转面的几何对象
            let polygon = comm.geometry.wktToPolygon(apiInstance, wkt, sr);

            //生成面样式
            let fillSymbol = new apiInstance.SimpleFillSymbol(
                style,
            );

            //生成图形
            let graphic = new apiInstance.Graphic({
                //几何对象
                geometry: polygon,
                //样式
                symbol: fillSymbol,
                //属性字段
                attributes: attributes,
            });

            return graphic;
        },
        /**
         * 面几何对象转面的图形(Graphic)
         * @param apiInstance api
         * @param wkt wkt
         * @param style 样式
         * @param sr 空间参考
         * @param attributes 属性字段值(可空)
         */
        polygonToPolygonGraphic: function (apiInstance, geometry, style, sr, attributes) {
            //生成面样式
            let fillSymbol = new apiInstance.SimpleFillSymbol(
                style,
            );

            //生成图形
            let graphic = new apiInstance.Graphic({
                //几何对象
                geometry: geometry,
                //样式
                symbol: fillSymbol,
                //属性字段
                attributes: attributes,
            });

            return graphic;
        },
        /**
         * wkt转面几何对象
         * @param apiInstance api
         * @param wkt wkt
         * @param sr 空间参考
         * @returns {apiInstance.Polygon}
         */
        wktToPolygon: function (apiInstance, wkt, sr) {
            //wkt转坐标对象
            let points = comm.geometry.wktToCoords(wkt);

            //生成面
            let polygon = new apiInstance.Polygon({
                //坐标
                rings: points,
                //空间参考
                spatialReference: sr
            });

            return polygon;
        },
        /**
         * 坐标对象转面几何对象
         * @param apiInstance api
         * @param coord 坐标对象 格式是arcgis jsapi标准,例如:[[[113.527839, 22.27028], [113.527238, 22.2557786], [113.5437178, 22.2597268], [113.54423, 22.2730306], [113.527839, 22.27028]]]
         * @param sr 空间参考
         * @returns {apiInstance.Polygon}
         */
        coordToPolygon: function (apiInstance, coord, sr) {
            //生成面
            let polygon = new apiInstance.Polygon({
                //坐标
                rings: coord,
                //空间参考
                spatialReference: sr
            });

            return polygon;
        },
        /**
         * 面转wkt
         * @param geometry
         * @returns {string}
         */
        polygonToWkt: function (geometry) {
            let wkt = [];
            let rings = geometry.rings;
            for (let i in rings) {
                let ring = rings[i];
                for (let j in ring) {
                    let p = ring[j];
                    wkt.push(p.join(" "));
                }
            }
            return "POLYGON ((" + wkt.join(",") + "))";
        },
        /**
         * wkt转坐标对象
         * PS:坐标对象是arcgis jsapi格式
         * @param wkt
         * @returns {*}
         */
        wktToCoords: function (wkt) {
            var features, type, str;
            wkt = wkt.replace(/[\n\r]/g, " ");
            var matches = comm.geometry.regExes.typeStr.exec(wkt);
            if (matches) {
                type = matches[1].toLowerCase();
                str = matches[2];
                if (comm.geometry.parse[type]) {
                    features = comm.geometry.parse[type].apply(this, [str]);
                }
            }
            return features;
        },
        /**
         *  wkt转坐标对象的正则表单式
         */
        regExes: {
            'typeStr': /^\s*(\w+)\s*\(\s*(.*)\s*\)\s*$/,
            'spaces': /\s+/,
            'parenComma': /\)\s*,\s*\(/,
            'doubleParenComma': /\)\s*\)\s*,\s*\(\s*\(/,  // can't use {2} here
            'trimParens': /^\s*\(?(.*?)\)?\s*$/
        },
        /**
         * wkt转坐标对象
         */
        parse: {
            'point': function (str) {
                var coords = comm.util.trim(str).split(comm.geometry.regExes.spaces);
                for (var i in coords)
                    coords[i] = Number(coords[i]);
                return coords;//new esri.geometry.Point(coords[0], coords[1]);
            },
            'pointzm': function (str) {
                var coords = comm.util.trim(str).split(comm.geometry.regExes.spaces);
                for (var i in coords)
                    coords[i] = Number(coords[i]);
                return coords.slice(0, 2);//new esri.geometry.Point(coords[0], coords[1]);
            },
            'linestring': function (str) {
                var points = comm.util.trim(str).split(',');

                var components = [];
                for (var i = 0, len = points.length; i < len; ++i) {
                    components.push(comm.geometry.parse.point.apply(this, [points[i]]));
                }
                return components//new esri.geometry.Polyline(components);
            },
            'linestringzm': function (str) {
                var points = comm.util.trim(str).split(',');

                var components = [];
                for (var i = 0, len = points.length; i < len; ++i) {
                    components.push(comm.geometry.parse.pointzm.apply(this, [points[i]]));
                }
                return components//new esri.geometry.Polyline(components);
            },
            'multilinestring': function (str) {
                var line;
                var lines = comm.util.trim(str).split(comm.geometry.regExes.parenComma);
                var components = [];
                for (var i = 0, len = lines.length; i < len; ++i) {
                    line = lines[i].replace(comm.geometry.regExes.trimParens, '$1');
                    components.push(comm.geometry.parse.linestring.apply(this, [line]));
                }
                return components;
            },
            'polygon': function (str) {
                var ring, linestring, linearring;
                var rings = comm.util.trim(str).split(comm.geometry.regExes.parenComma);

                var components = [];
                for (var i = 0, len = rings.length; i < len; ++i) {
                    ring = rings[i].replace(comm.geometry.regExes.trimParens, '$1');
                    linestring = comm.geometry.parse.linestring.apply(this, [ring]);
                    components.push(linestring);
                }
                return components;
            }
        },
    },
    //通用
    util: {
        trim: function (str) {
            return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
        }
    }
};

module.exports = comm;

 

© 著作权归作者所有

共有 人打赏支持
c
粉丝 12
博文 48
码字总数 30759
作品 0
广州
程序员
私信 提问
ArcGIS API for Javascript 2.X 离线部署(以2.6为例)

先在官网上ArcGIS API for JavaScript download page(需要有账户)下载最新的API:arcgisjsv26_api.zip( 点击直接下载)和 SDK: arcgisjsv26_sdk.zip(直接下载); 2. 将下载下来的 arcg...

回忆在昨天
2012/10/24
0
0
用Tomcat8.5做Arcgis API for javascript的本地部署

step1:1从arcgis官网上下载Arcgis api for JavaScript 4.3 https://developers.arcgis.com/javascript/latest/guide/release-notes/ ps:前提是你需要有一个esri开发者账户否则不能下载。 st......

duxiuxiu
2017/05/18
0
0
arcgis jsapi接口入门系列(3):各种类型的图层添加

这里说的tomcat切片,是指arcgis server切片后,把切片图片文件用tomcat发布(其他任意web服务器发布都行) arcgis rest图层指用arcgis server发布的地图服务,用rest方式加载...

canneljls
10/17
0
0
vue地图可视化 ArcGIS篇(3)

ArcGIS for javascript开发心得 本次实例中采用ArcGIS for javascript3.24版本,由于版本3与4在API等存在较大区别,就不一一列举,详细区别看[官方解释] arcgis for js4.7版本能够自动创建l...

YJ在掘金
11/06
0
0
ArcGis Engine(AE)开发环境的搭建

要想做ArcGis Engine开发,就必须得搭建好一个开发环境,不搭建好开发环境,一切只能是纸上谈兵。在这里我们使用的是ArcGis10.0和VS2008搭建开发环境。具体方法如下: 所需软件: 1. VS2008。...

WhuGisExp
2013/03/15
0
2

没有更多内容

加载失败,请刷新页面

加载更多

DataFrames中的reindex用法

from pandas import DataFrame frame = DataFrame(np.arange(9).reshape((3,3)),index=['a','c','d'],columns=['Ohio','Texas','California'] states = ['Texas','Utah','California'] frame......

卖小孩的小火柴
8分钟前
0
0
拜托!面试请不要再问我Spring Cloud底层原理

毫无疑问,Spring Cloud是目前微服务架构领域的翘楚,无数的书籍博客都在讲解这个技术。不过大多数讲解还停留在对Spring Cloud功能使用的层面,其底层的很多原理,很多人可能并不知晓。因此本...

James-
9分钟前
1
0
Shiro框架

提供了认证,授权,加密,会话管理等功能 在spring配置文件中配置shiro,需要配置的有shiro的过滤器工厂,在里面我们可以配置什么页面需要认证,什么认证不需要认证,认证成功后跳转的路径,认证失败...

tinder_boy
12分钟前
0
0
有关定时任务的表达式--cron 详细解

Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义,Cron有如下两种语法格式: Seconds Minutes Hours DayofMonth Month DayofWeek Year或 Seconds Minu...

kuchawyz
13分钟前
1
0
下一代大数据处理引擎,阿里云实时计算独享模式重磅发布

11月14日,阿里云重磅发布了实时计算独享模式,即用户独享一部分物理资源,这部分资源在网络/磁盘/CPU/内存等资源上跟其他用户完全独立,是实时计算在原有共享模式基础上的重大升级。 (观看...

阿里云云栖社区
14分钟前
1
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部