ArcGIS中文API使用指南
背景介绍
ArcGIS API for JavaScript是Esri公司推出的一款强大的地图开发工具,主要用于构建交互式的Web地图应用,通过该API,开发者可以将ArcGIS Server提供的地图资源嵌入到Web应用中,实现地图展示、分析、3D数据管理及对实时数据的支持,本文将详细讲解如何在ArcGIS API for JavaScript中使用中文界面,并提供一些实用的开发技巧和示例代码。
基本流程
使用ArcGIS API for JavaScript进行开发的一般流程如下:
创建基本的HTML页面:设置页面的基本结构和样式。
引入ArcGIS API的库:加载所需的JavaScript文件。
创建地图并添加控件:初始化地图对象,并配置视图控件。
添加图层到地图:根据需求添加不同类型的图层。
显示和调试你的应用:在浏览器中查看效果并进行调试。
操作步骤
创建基本的HTML页面
创建一个新文件(例如index.html
),并添加基本的HTML结构:
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>ArcGIS示例</title> <link rel="stylesheet" href="https://js.arcgis.com/4.12/esri/themes/light/main.css"> <style> html, body, #viewDiv { height: 100%; margin: 0; } </style> </head> <body> <div id="viewDiv"></div> <script src="https://js.arcgis.com/4.12/"></script> <script> require([ "esri/Map", "esri/views/MapView", "esri/layers/FeatureLayer" ], function(Map, MapView, FeatureLayer) { var map = new Map({ basemap: "streets-vector" }); var view = new MapView({ container: "viewDiv", map: map, zoom: 4, center: [116.397477, 39.90865] // 北京天安门 }); var layer = new FeatureLayer({ url: "https://services.arcgis.com/P3ePLSWk1rvVsJbJdvI/ArcGIS/rest/services/World_Topo_Map/MapServer/0", visible: true, title: "World Topo Map" }); map.add(layer); }); </script> </body> </html>
这个简单的示例展示了如何创建一个基础地图,并在指定位置显示标记。
引入ArcGIS API的库
使用<script>
标签引入ArcGIS API,下面是引入的代码:
<script src="https://js.arcgis.com/4.12/"></script>
这段代码加载了最新版本的ArcGIS API for JavaScript库。
创建地图并添加控件
在上一步的代码中,我们实例化了地图和视图:
require([ "esri/Map", "esri/views/MapView", "esri/layers/FeatureLayer" ], function(Map, MapView, FeatureLayer) { var map = new Map({ basemap: "streets-vector" // 设置底图 }); var view = new MapView({ container: "viewDiv", // 地图容器 map: map, // 使用之前创建的地图 zoom: 4, // 初始缩放级别 center: [116.397477, 39.90865] // 确定地图中心位置(经度, 纬度) }); });
这里我们使用了“街道矢量”底图,并将视图中心设置为北京天安门。
添加图层到地图
可以通过添加图层来丰富地图内容,添加一个标记图层:
require(["esri/layers/GraphicsLayer"], function(GraphicsLayer) { var graphicsLayer = new GraphicsLayer(); map.add(graphicsLayer); // 将图层添加到地图上 // 创建标记 var point = { type: "point", longitude: 116.397477, latitude: 39.90865 }; var simpleMarkerSymbol = { type: "simple-marker", color: [226, 119, 40], outline: { // 轮廓 color: [255, 255, 255], width: 1 } }; var pointGraphic = { geometry: point, symbol: simpleMarkerSymbol }; graphicsLayer.add(pointGraphic); // 添加标记到图层 });
这段代码创建了一个标记并将其添加到地图上。
模块参考表格
以下是一个常用模块的参考表格:
模块名 | 描述 | 示例 | |
esri/Map |
地图对象,用于创建和管理地图 | var map = new Map({ basemap: 'streets' }); |
|
esri/views/MapView |
视图对象,用于控制地图的显示 | var view = new MapView({ container: 'viewDiv', map: map, zoom: 4, center: [116.397477, 39.90865] }); |
|
esri/layers/FeatureLayer |
要素图层,用于显示地理要素 | var layer = new FeatureLayer({ url: 'https://services.arcgis.com/...' }); |
|
esri/layers/GraphicsLayer |
图形图层,用于自定义图形元素 | var graphicsLayer = new GraphicsLayer(); |
|
esri/symbols/SimpleMarkerSymbol |
简单标记符号,用于标记点 | var symbol = { type: 'simple-marker', color: [226, 119, 40], outline: { color: [255, 255, 255], width: 1 } }; |
|
esri/Graphic |
图形对象,表示单个图形元素 | var pointGraphic = { geometry: point, symbol: simpleMarkerSymbol }; |
|
esri/geometry/Point |
点几何对象,表示一个地理位置 | var point = { type: 'point', longitude: 116.397477, latitude: 39.90865 }; |
|
esri/PopupTemplate |
弹出模板,用于显示信息窗口 | var template = { title: '{Name}', content: '${Address}' }; |
|
esri/tasks/query |
查询任务,用于查询地理数据 | var queryTask = new esri.tasks.QueryTask("URL"); |
|
esri/tasks/support/Query |
查询参数,用于定义查询条件 | var query = new esri.tasks.SupportQuery(); |
|
esri/widgets/SearchWidget |
搜索小部件,用于提供搜索功能 | var searchWidget = new Search({ view: view, container: 'searchDiv' }); |
|
esri/widgets/Legend |
图例小部件,用于显示图层信息 | var legend = new Legend({ view: view, container: 'legendDiv' }); |
|
esri/widgets/BasemapToggle |
底图切换小部件,用于切换不同底图 | var basemapToggle = new BasemapToggle({ view: view, nextBasemap: 'satellite' }); |
|
esri/widgets/Scalebar |
比例尺小部件,用于显示比例尺 | var scalebar = new Scalebar({ view: view, container: 'scalebarDiv' }); |
|
esri/widgets/DirectionsWidget |
方向小部件,用于导航 | var directionsWidget = new DirectionsWidget({ view: view, container: 'directionsDiv' }); |
|
esri/widgets/Edit |
编辑小部件,用于编辑要素 | var edit = new Edit({ view: view, layer: featureLayer }); |
|
esri/widgets/Print |
打印小部件,用于打印地图 | var print = new Print({ view: view, container: 'printDiv' }); |
|
esri/widgets/Bookmarks |
书签小部件,用于保存和恢复视图 | var bookmarks = new Bookmarks({ view: view, container: 'bookmarksDiv' }); |
|
esri/widgets/Expand |
展开小部件,用于展开或折叠内容 | var expand = new Expand({ view: view, expandIconClass: 'fa fa-expand', collapseIconClass: 'fa fa-compress', target: 'contentDiv' }); |
|
esri/widgets/Histogram |
直方图小部件,用于显示数值分布 | var histogram = new Histogram({ view: view, layer: featureLayer, field: 'POPULATION', title: 'Population Density' }); |
|
esri/widgets/TimeSlider |
时间滑块小部件,用于播放时间动画 | var timeSlider = new TimeSlider({ view: view, layer: featureLayer, interval: 1000, style: 'width: 200px; height: 50px;' }); |
|
esri/widgets/UniqueValueRenderer |
唯一值渲染器小部件,用于分类渲染 | var renderer = new UniqueValueRenderer({ field: 'Category', uniqueValues: ['Low', 'Medium', 'High'] }); |
|
esri/widgets/UniqueValueInfos |
唯一值信息小部件,用于显示分类信息 | var infos = new UniqueValueInfos({ view: view, renderer: renderer, container: 'infosDiv' }); |
|
esri/widgets/Legend |
图例小部件,用于显示图层信息 | var legend = new Legend({ view: view, container: 'legendDiv' }); |
|
esri/widgets/LayerList |
图层列表小部件,用于显示图层列表 | var layerList = new LayerList({ view: view, container: 'layerListDiv' }); |
|
esri/widgets/OverviewMap |
概览地图小部件,用于显示主地图的缩略图 | var overviewMap = new OverviewMap({ view: view, container: 'overviewDiv' }); |
|
esri/widgets/Print |
打印小部件,用于打印地图 | var print = new Print({ view: view, container: 'printDiv' }); |
|
esri/widgets/Swipe |
滑动小部件,用于比较两个图层 | var swipe = new Swipe({ view: view, container: 'swipeDiv' }); |
|
esri/widgets/TransparencySlider |
透明度滑块小部件,用于调整图层透明度 | var transparencySlider = new TransparencySlider({ view: view, layer: featureLayer, container: 'transparencyDiv' }); |
|
esri/widgets/BasemapGallery |
底图库小部件,用于切换不同底图 | var gallery = new BasemapGallery({ view: view, container: 'galleryDiv' }); |
|
esri/widgets/BasemapToggle |
底图切换小部件,用于切换不同底图 | var toggle = new BasemapToggle({ view: view, nextBasemap: 'satellite' }); |
|
esri/widgets/Bookmarks |
书签小部件,用于保存和恢复视图 | var bookmarks = new Bookmarks({ view: view, container: 'bookmarksDiv' }); |
|
esri/widgets/Compass |
罗盘小部件,用于指示方向 | var compass = new Compass({ view: view, container: 'compassDiv' }); |
|
esri/widgets/Geolocation |
定位小部件,用于获取用户当前位置 | var geolocation = new Geolocation({ view: view, container: 'geolocationDiv' }); |
|
esri/widgets/HomeButton |
主页按钮小部件,用于返回默认视图 | var homeButton = new HomeButton({ view: view, container: 'homeDiv' }); |
|
esri/widgets/Measurement |
测量小部件,用于测量距离和面积 | var measurement = new Measurement({ view: view, container: 'measurementDiv' }); |
|
esri/widgets/Navigation |
导航小部件,用于平移和缩放地图 | var navigation = new Navigation({ view: view, container: 'navigationDiv' }); |
|
esri/widgets/Scalebar |
比例尺小部件,用于显示比例尺 | var scalebar = new Scalebar({ view: view, container: 'scalebarDiv' }); |
|
esri/widgets/TimeSlider |
时间滑块小部件,用于播放时间动画 | var timeSlider = new TimeSlider({ view: view, layer: featureLayer, interval: 1000, style: 'width: 200px; height: 50px;' }); |
|
esri/widgets/UniqueValueRenderer |
唯一值渲染器小部件,用于分类渲染 | var renderer = new UniqueValueRenderer({ field: 'Category', uniqueValues: ['Low', 'Medium', 'High'] }); |
|
esri/widgets/UniqueValueInfos |
唯一值信息小部件,用于显示分类信息 | var infos = new UniqueValueInfos({ view: view, renderer: renderer, container: 'infosDiv' }); |
|
esri/widgets/Legend |
图例小部件,用于显示图层信息 | var legend = new Legend({ view: view, container: 'legendDiv' }); |
|
esri/widgets/LayerList |
图层列表小部件,用于显示图层列表 | var layerList = new LayerList({ view: view, container: 'layerListDiv' }); |
|
esri/widgets/OverviewMap |
概览地图小部件,用于显示主地图的缩略图 | var overviewMap = new OverviewMap({ view: view, container: 'overviewDiv' }); |
|
esri/widgets/Print |
打印小部件,用于打印地图 | var print = new Print({ view: view, container: 'printDiv' }); |
|
esri/widgets/Swipe |
滑动小部件,用于比较两个图层 | var swipe = new Swipe({ view: view, container: 'swipeDiv' }); |
|
esri/widgets/TransparencySlider |
透明度滑块小部件,用于调整图层透明度 | var transparencySlider = new TransparencySlider({ view: view, layer: featureLayer, container: 'transparencyDiv' }); |
|
esri/widgets/BasemapGallery |
底图库小部件,用于切换不同底图 | var gallery = new BasemapGallery({ view: view, container: 'galleryDiv' }); |
|
esri/widgets/BasemapToggle |
底图切换小部件,用于切换不同底图 | var toggle = new BasemapToggle({ view: view, nextBasemap: 'satellite' }); |
|
esri栏目二:常见问题与解答 |
esri/widgets/Bookmarks
|
书签小部件,用于保存和恢复视图 | var bookmarks = new Bookmarks({ view: view, container: 'bookmarksDiv' });` |
esri/widgets/Compass |
罗盘小部件,用于指示方向 | var compass = new Compass({ view: view, container: 'compassDiv' }); |
|
esri/widgets/Geolocation |
定位小部件,用于获取用户当前位置 | var geolocation = new Geolocation({ view: view, container: 'geolocationDiv' }); |
|
esri/widgets/HomeButton |
主页按钮小部件,用于返回默认视图 | var homeButton = new HomeButton({ view: view, container: 'homeDiv' }); |
|
esri/widgets/Measurement |
测量小部件,用于测量距离和面积 | var measurement = new Measurement({ view: view, container: 'measurementDiv' }); |
|
esri/widgets/Navigation |
导航小部件,用于平移和缩放地图 | var navigation = new Navigation({ view: view, container: 'navigationDiv' }); |
|
esri/widgets/Scalebar |
比例尺小部件,用于显示比例尺 | var scalebar = new Scalebar({ view: view, container: 'scalebarDiv' }); |
|
esri/widgets/TimeSlider |
时间滑块小部件,用于播放时间动画 | var timeSlider = new TimeSlider({ view: view, layer: featureLayer, interval: 1000, style: 'width: 200px; height: 50px;' }); |
|
esri/widgets/UniqueValueRenderer |
唯一值渲染器小部件,用于分类渲染 | var renderer = new UniqueValueRenderer({ field: 'Category', uniqueValues: ['Low', 'medium', High'] }); |
|
esri/widgets/UniqueValueInfos |
唯一值信息小部件,用于显示分类信息 | var infos = new UniqueValueInfos({ view: view, renderer: renderer, container: 'infosDiv' }); |
|
esri/widgets/Legend |
图列小部件,用于显示图层信息 | var legend = new Legend({ view: view, container: 'legendDiv' }); |
|
esri/widgets/LayerList |
图层列表小部件,用于显示图层列表 | var layerList = new LayerList({ view: view, container: 'layerListDiv' }); |
|
esri/widgets/OverviewMap |
概览地图小部件,用于显示主地图的缩略图 | var overviewDiv = new OverviwMap({ view: view, container: 'overviewDiv' }); |
以上内容就是解答有关“arcgis 中文api”的详细内容了,我相信这篇文章可以为您解决一些疑惑,有任何问题欢迎留言反馈,谢谢阅读。
原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/688265.html