Kiba518

Kiba518

沈阳-架构-开发。

Fork me on GitHub

一文让你彻底掌握ArcGisJS地图管理的秘密

使用ArcGis开发地图

引用ArcGisJS

使用ArcGisJS开发地图,首先需要引入ArcGis的Js文件和CSS文件,引入方式有两种,一种是官网JS引用,一种是本地JS引用。如下:

官网JS引用

1
2
3
  <link rel="stylesheet"
href="https://js.arcgis.com/4.20/esri/themes/light/main.css">
<script src="https://js.arcgis.com/4.20/"></script>

本地JS引用

1
2
<link rel="stylesheet" href="http://192.168.1.28:419/arcgis_js_api/javascript/4.19/esri/themes/light/main.css">
    <script src="http://192.168.1.28:419/arcgis_js_api/javascript/4.19/init.js"></script>

require内置对象

require是ArcGisJS开发的起点,类似于C#中的引入命名空间的using,不同的是require引入的都是js文件,每个js文件都是一个大的js类。

require有两个参数,第一个参数接收js文件地址,第二个参数输出一个函数,函数的参数返回引入js文件的js类,类顺序与上面引入js文件的顺序的一致。

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<script>
        require(["esri/config",
            "esri/Map",
            "esri/views/MapView",
            "esri/Basemap",
            "esri/layers/Layer"
        ], function (
            esriConfig,
            Map,
            MapView,
            Basemap,
            Layer) {
            //使用地图对象   
        }
        );
</script>

下面我们看一个ArcGisJS本地部署的网站下的esri文件夹的结构。

如图所示,我们上面使用"esri/config"字符串引入的js文件就是esri文件夹下的config.js文件。

地图开发

基础开发

地图开发主要是在require的输出函数中做的,具体开发逻辑是使用Map类创建地图,使用View类绑定div元素,然后将Map地图对象赋值到View类中,实现地图在div中展示。

Map类与View类各有两个子类, WebMap,MapView对应二维地图,SceneMap,SceneView对应三维地图。

编写代码展示地图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>WebArcGis_JS4</title>
    <style>
        html,
        body,
        #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
        }
    </style>
    <link rel="stylesheet" href="https://js.arcgis.com/4.20/esri/themes/light/main.css">
    <script src="https://js.arcgis.com/4.20/"></script>
    <script>
        require(["esri/config",
            "esri/Map",
            "esri/views/MapView",
            "esri/layers/MapImageLayer",
            "esri/Basemap",
            "esri/layers/Layer"
        ], function (
            esriConfig,
            Map,
            MapView,
            MapImageLayer,
            Basemap,
            Layer) {
            esriConfig.apiKey = "YOUR_API_KEY";
            
            let layer = new MapImageLayer({
                url: "http://192.168.1.2:6080/arcgis/rest/services/SampleWorldCities/MapServer"
            });
            const map = new Map({
                basemap: new Basemap({ baseLayers: [layer] }),
                logo: false,  //不显示Esri的logo
            });
            const view = new MapView({
                map: map,
                center: [125.04658531829789, 41.978062677899004],
                zoom: 13, // Zoom level
                container: "viewDiv" // Div的Id
            }); 
        });
    </script>
</head>
<body>
    <div id="viewDiv"></div>
</body>
</html>

如上代码所示,我们先定义了一个layer图层,并指定地图url(地址来自于ArcGisServer发布),然后定义了一个Map对象,将定义好的图层定义为底图(地图有很多个图层组成,最下面的图层为底图),Map对象初始化时接受basemap参数,其值为图层对象,含义为设置底图图层。

然后定义个view对象,初始化接受两个主要参数,一个是map,一个是container,map赋值我们上面定义的map对象,container指向一个div的id。

最后,我们再body中定义一个div取名viewDiv。

然后我们运行,界面如下:

监听事件

基础地图使用编写完后,我们编写一个监听事件,代码如下:

1
2
3
4
5
6
7
8
9
//监听单击事件
view.on("click", function (event) {
    console.log(event);
    console.log("x:" + event.mapPoint.x);
    console.log("y:" + event.mapPoint.y);
    console.log("longitude:" + event.mapPoint.longitude);
    console.log("latitude:" + event.mapPoint.latitude);
});

我们使用view对象的on函数,实现了一个点击事件的监听,界面效果如下:

监视属性

ArcGis里除了监听,还支持监视,下面我们使用watch函数监视scale(比例尺)属性。

1
2
3
var handle = view.watch("scale", function (newValue, oldValue, propertyName, target) {
               console.log(propertyName + " changed from " + oldValue + " to " + newValue);
           });

小部件

ArcGis还提供丰富的小部件,比如比例尺,坐标,指南针等,我们只需要引入对应的js类,就可以使用这些小部件了。

小部件使用代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>WebArcGis_JS4</title>
    <style>
        html,
        body,
        #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
        }
    </style>
    <link rel="stylesheet" href="https://js.arcgis.com/4.20/esri/themes/light/main.css">
    <script src="https://js.arcgis.com/4.20/"></script>
    <script>
        require(["esri/config",
            "esri/Map",
            "esri/WebMap",
            "esri/views/MapView",
            "esri/widgets/Feature",
            "esri/Graphic",
            "esri/tasks/support/Query",
            "esri/widgets/FeatureTable",
            "esri/layers/FeatureLayer",
            "esri/layers/GraphicsLayer",
            "esri/layers/MapImageLayer",
            "esri/layers/BaseDynamicLayer",
            "esri/Basemap",
            "esri/layers/TileLayer",
            "esri/layers/ImageryLayer",
            "esri/widgets/Home",
            "esri/layers/support/Field",
            "esri/geometry/Point",
            "esri/widgets/LayerList",
            "esri/widgets/Swipe",
            "esri/widgets/AreaMeasurement2D",
            "esri/widgets/DistanceMeasurement2D",
            "esri/widgets/BasemapLayerList",
            "esri/widgets/Bookmarks",
            "esri/widgets/Expand",
            "esri/widgets/Compass",
            "esri/widgets/CoordinateConversion",
            "esri/widgets/Fullscreen",
            "esri/widgets/ScaleBar",
            "esri/widgets/Print"
        ], function
            (esriConfig,
                Map,
                WebMap,
                MapView,
                Feature,
                Graphic,
                Query,
                FeatureTable,
                FeatureLayer,
                GraphicsLayer,
                MapImageLayer,
                BaseDynamicLayer,
                Basemap,
                TileLayer,
                ImageryLayer,
                Home,
                Field,
                Point,
                LayerList,
                Swipe,
                AreaMeasurement2D,
                DistanceMeasurement2D,
                BasemapLayerList,
                Bookmarks,
                Expand,
                Compass,
                CoordinateConversion,
                Fullscreen,
                ScaleBar,
                Print
            )  {
            var baseUrl = "http://192.168.1.21:6080/arcgis/rest/services/SampleWorldCities/MapServer";
            
            let layer = new MapImageLayer({
                url: baseUrl
            });
            const map = new Map({
                basemap: new Basemap({ baseLayers: [layer] }),
                logo: false,  //不显示Esri的logo
            });
            const view = new MapView({
                map: map,
                center: [125.04658531829789, 41.978062677899004],
                zoom: 3, // Zoom level
                container: "viewDiv" // Div的Id
            });
            //==============坐标小部件
            var ccWidget = new CoordinateConversion({
                view: view
            });
            view.ui.add(ccWidget, "bottom-left");
            //===============比例尺小部件
            let scaleBar = new ScaleBar({
                view: view,
                style: "ruler"
            });
            view.ui.add(scaleBar, {
                position: "bottom-left"
            });
            //===============指南针小部件
            var compassWidget = new Compass({
                view: view
            });
            view.ui.add(compassWidget, "bottom-right");
            //===============图层小部件
            let basemapLayerList = new BasemapLayerList({
                basemapTitle: "图层列表",
                view: view
            });
            var basemapLayerListExpand = new Expand({
                view: view,
                content: basemapLayerList,
                expandTooltip: "图层",
                expanded: false
            });
            view.ui.add(basemapLayerListExpand, "top-left");
            //===============主页小部件
            let homeWidget = new Home({
                view: view
            });
            view.ui.add(homeWidget, "top-left");
            //===============全屏小部件
            fullscreen = new Fullscreen({
                view: view
            });
            view.ui.add(fullscreen, "top-left");
            //===============书签小部件
            const bookmarks = new Bookmarks({
                view: view,
                editingEnabled: true
            });
            const bkExpand = new Expand({
                view: view,
                content: bookmarks,
                expandTooltip: "书签",
                expanded: false
            });
            view.ui.add(bkExpand, "top-left");
            //===============测量小部件
            let distanceWidget = new DistanceMeasurement2D({
                view: view
            });
            var distanceExpand = new Expand({
                view: view,
                content: distanceWidget,
                expandTooltip: "距离测量",
                expanded: false
            });
            view.ui.add(distanceExpand, "top-left");
            let areaWidget = new AreaMeasurement2D({
                view: view
            });
            var areaExpand = new Expand({
                view: view,
                content: areaWidget,
                expandTooltip: "面积测量",
                expanded: false
            });
            view.ui.add(areaExpand, "top-left");
        });
    </script>
</head>
<body>
    <div id="viewDiv"></div>
</body>
</html>

效果图如下:

属性图层

属性图层的类是FeatureLayer,FeatureLayer类有三个比较重要的属性source(数据源),fields(图层中可用字段),popupTemplate(点击弹出模板)。

正确为这三个属性赋值后,我们就可以实现在地图上画两个图形(这里画圆点),并且点击图形弹出图形的属性。

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
            var baseUrl = "http://192.168.50.28:6080/arcgis/rest/services/SampleWorldCities/MapServer";
            
            let layer = new MapImageLayer({
                url: baseUrl
            });
            const map = new Map({
                basemap: new Basemap({ baseLayers: [layer] }),
                logo: false,  //不显示Esri的logo
            });
            let featuresSource = [
                {
                    geometry: {
                        type: "point",
                        x: 61.94658531829789,
                        y: 41.978062677899004
                    },
                    attributes: {
                        ObjectID: 1,
                        Name: "Kiba",
                        MsgTime: Date.now(),
                        Msg: "Hello Kiba"
                    }
                },
                {
                    geometry: {
                        type: "point",
                        latitude: 41.04658531829789,
                        longitude: 60.978062677899004
                    },
                    attributes: {
                        ObjectID: 2,
                        Name: "Kiba518",
                        MsgTime: Date.now(),
                        Msg: "Hello Kiba518"
                    }
                }
            ];
            // 弹出框设置
            const popupTemplate = {
                "title": "数据信息",
                "content": "<b>Id:</b> {ObjectID}<br><b>姓名:</b> {Name}<br><b>时间:</b> {MsgTime}<br><b>消息:</b> {Msg}"
            }
            let layer1 = new FeatureLayer({
                source: featuresSource,  
                objectIdField: "ObjectID",//唯一标识字段
                fields://定义图层中可用字段 name字段名  alias别名  type类型
                    [{
                        name: "ObjectID",
                        alias: "ObjectID",
                        type: "oid"
                    }, {
                        name: "Name",
                        alias: "Name",
                        type: "string"
                    },
                    {
                        name: "MsgTime",
                        alias: "MsgTime",
                        type: "date"
                    },
                    {
                        name: "Msg",
                        alias: "Msg",
                        type: "string"
                    }],
                popupTemplate: popupTemplate
            });
            map.add(layer1);
            const view = new MapView({
                map: map,
                center: [65.04658531829789, 41.978062677899004],
                zoom: 13, // Zoom level
                container: "viewDiv" // Div的Id
            });

实现结果如下:

结语

到此ArcGis实现地图管理就介绍完了。

PS:很多公司把会给会GIS开发的程序员更高的工资。

----------------------------------------------------------------------------------------------------

到此,到此JArcGisJS地图管理就已经介绍完了。

代码已经传到Github上了,欢迎大家下载。

Github地址: https://github.com/kiba518/ArcGisWebJs

----------------------------------------------------------------------------------------------------

注:此文章为原创,任何形式的转载都请联系作者获得授权并注明出处!
若您觉得这篇文章还不错,请点击下方的推荐】,非常感谢!

https://www.cnblogs.com/kiba/p/15133225.html

 

 

posted @   kiba518  阅读(1446)  评论(2编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示