widget(窗口部件)是arcgis api for js 中的重要控件,教程中指出,该部件包括:
1)locating your current location on a map via the Locate widget,通过Locate部件定位
2)adding a legend to help visualize your map using the Legend widget,通过Legend添加图例
3)show a list of layers to provide users an easy way toggle them on/off with the LayerList widget,通过LayerList部件展示和控制layer
4) searching for features and locations within the map using the Search widget。通过Search部件查找要素
引用:esri/widgets
代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加2D底图切换部件</title>
<link rel="stylesheet" href="api4.6/libs/Arcgis_api/esri/themes/light/main.css">
<script src="api4.6/libs/Arcgis_api/dojo/dojo.js"></script>
<style>
html,
body,
#viewDiv
{
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
</style>
<script>
//加载引用
require([
"esri/Map",
"esri/views/MapView",
"esri/widgets/BasemapToggle"
],function (
Map,
MapView,
BasemapToggle)
{
//创建基础底图
var map=new Map(
{
basemap:"streets",
}
);
//创建底图视图类,引用map
var mapView=new MapView({
container:"viewDiv",
map:map,
center:[115,28.3026],//设置中心坐标
zoom:8,
});
//创建widgets切换部件并设置转换底图
var basemaptoggle=new BasemapToggle({
view:mapView,
nextBasemap:"hybrid",//下一底图
});
mapView.ui.add(basemaptoggle,"bottom-right");//MapView.ui可以add, move, or remove widgets
});
</script>
</head>
<body>
<div id="viewDiv"></div>
</body>
</html>