ArcGIS for js 4.x 加载图层

二维:

1、创建vue项目

npm create vite@latest

2、安装ArcGIS JS API依赖包

npm install @arcgis/core

3、引入ArcGIS API for JavaScript模块

<script setup>
import "@arcgis/core/assets/esri/themes/light/main.css";
import Map from '@arcgis/core/Map.js';
import MapView from '@arcgis/core/views/MapView.js';
import TileLayer from '@arcgis/core/layers/TileLayer.js'
import WebTileLayer from '@arcgis/core/layers/WebTileLayer.js'
import Locate from "@arcgis/core/widgets/Locate.js";
import Compass from "@arcgis/core/widgets/Compass.js";
import ScaleBar from "@/pages/demo9/scaleBar.js"
import { onMounted } from "vue";

***
***
</script>

4、创建vue组件

<template>

	<view class="map" id="mapView"></view>
	
</template>

5、创建一个初始化函数initArcGisMap()用于创建Map实例和MapView实例

	
<script setup>

***
***

onMounted(()=>{
	//initMap();
	initTDTMap();
});

// geo地图加载
const initMap = () =>{
	const map = new Map({
		basemap:{
			baseLayers:[
				new TileLayer({
					url:"http://map.geoq.cn/arcgis/rest/services/ChinaOnlineCommunity_Mobile/MapServer"
				})
			]	
		}
	});
	
	const mapView = new MapView({
		// 长春坐标系
		center:[125.331345,43.8328],
		// 初始层级
		zoom:10,
		container:"mapView",
		map:map,
		constraints: {
			minZoom: 10,// 最小层级
			maxZoom: 19 // 最大层级
		}
	});

}
	
// 天地图加载
const initTDTMap = () =>{
	
	let webLayer = new WebTileLayer({
		urlTemplate: "http://{subDomain}.tianditu.gov.cn/vec_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=vec&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILECOL={col}&TILEROW={row}&TILEMATRIX={level}&tk=352d4b1313777a8643542046a28d17e5",
		subDomains: ['t0', 't1', 't2', 't3', 't4', 't5', 't6', 't7']
	});
	
	
	const map = new Map({
		basemap:{
			baseLayers:[webLayer]
		}
	});
	
	 const mapView = new MapView({
		// 默认中心点位
		center: [125.338, 43.882],
		map: map,
		// 初始层级
		zoom: 10,
		container: "mapView",
		constraints: {
			minZoom: 9,// 最小层级
			maxZoom: 17// 最大层级
		},
	});
	
	let compass = new Compass({
			view: mapView
		});

	let locateWidget = new Locate({
		view: mapView, // Attaches the Locate button to the view
	});


	// 移动默认的放大缩小按钮
    mapView.ui.move("zoom", "bottom-right");
    // 恢复方向和定位按钮
	mapView.ui.add([compass, locateWidget], "bottom-left");
	// 恢复方向和定位按钮位置
	mapView.ui.padding = {top:0,left:0,right:10,bottom:60}	
    //清除放大缩小按钮
    //mapView.ui.remove('zoom')
    // 清除底部powered by ESRI
	mapView.ui.remove("attribution");

	// 比例尺方法调用
	ScaleBar(mapView);
}
	
</script>

<style lang="scss" scoped>
    .map ::v-deep .esri-view-surface.esri-view-surface--touch-none:focus::after {
		outline: none !important;
	}
	.map {
		width: 100%;
		/* height: 185vw; */
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
	}
	#mapView{
		width: 100%;
		height:100%;
	}
	
	

</style>

加载地图可后设置container,使得mapView在方法外使用:

// mapView容器
const mapView = new MapView({
    // 长春坐标系
    center:[125.331345,43.8328],
    // 初始层级
    zoom:10,
    //container:"mapView",
    map:map,
    constraints: {
        minZoom: 10,// 最小层级
        maxZoom: 19 // 最大层级
    }
});
	
    
mapView.ui.remove('zoom')//清除放大缩小按钮
// 清除底部powered by ESRI
mapView.ui.remove("attribution");

const initMap = function(){
    mapView.container = "mapView";

};

封装加载地图:

let myMapView = ref();
// 封装map,mapView,zoom
let ToolsConfig = {
    map:null,
    view:null,
    zoom:10,
    viewpoint:null
};

onMounted(()=>{
    initMap();
})

// 地图
const dzLayer = new WebTileLayer({
	urlTemplate: "http://{subDomain}.tianditu.gov.cn/vec_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=vec&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILECOL={col}&TILEROW={row}&TILEMATRIX={level}&tk=7b79e37115f10c512f76cdecda599902",
	subDomains: ['t0', 't1', 't2', 't3', 't4', 't5', 't6', 't7']
});
 
// 标注
const dzbzLayer = new WebTileLayer({
	urlTemplate: "https://{subDomain}.tianditu.gov.cn/cva_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cva&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILECOL={col}&TILEROW={row}&TILEMATRIX={level}&tk=7b79e37115f10c512f76cdecda599902",
	subDomains: ['t0', 't1', 't2', 't3', 't4', 't5', 't6', 't7']
});

ToolsConfig.map = new Map({
    basemap:{
        baseLayers:[dzLayer],
		referenceLayers:[dzbzLayer]
    }
});


const map = new Map({
    basemap:{
        baseLayers:[dzLayer],
		referenceLayers:[dzbzLayer]
    }
});


ToolsConfig.view = new MapView({
    // 长春坐标系
    center:[125.331345,43.8328],
    // 初始层级
    zoom:10,
    //container:"mapView",
    map:ToolsConfig.map,
    constraints: {
        minZoom: 10,// 最小层级
        maxZoom: 19 // 最大层级
    }
});

//清除放大缩小按钮
ToolsConfig.view.ui.remove('zoom')
// 清除底部powered by ESRI
ToolsConfig.view.ui.remove("attribution");

const initMap = function(){
    ToolsConfig.view.container = "mapView";
    // 下面的方法也可以
    //ToolsConfig.view.container = myMapView.value.id;

     // 地图加载后调用
    ToolsConfig.view.when(function () {
        // 赋值初始化位置
        ToolsConfig.viewpoint = ToolsConfig.view.viewpoint.clone();
    });
    
};

 ToolsConfig.zoom = new Zoom({
    view:ToolsConfig.view,
});


// 放大缩小
 ToolsConfig.zoom.zoomIn();// 放大
 ToolsConfig.zoom.zoomOut();// 缩小
// 复位
// 设置后会自动视角定位
ToolsConfig.view.viewpoint = ToolsConfig.viewpoint;

 

比例尺设置(scaleBar.js文件):

import ScaleBar from "@arcgis/core/widgets/ScaleBar";
// 比例尺
const scale = ((mapView) =>{
	let scaleBar = new ScaleBar({
		view:mapView
	});
	
	// Add widget to the bottom left corner of the view
	mapView.ui.add(scaleBar, {
		position: "bottom-left"
	});
	
});

export default scale;

天地图: 

  

let basemap = new Basemap({
          baseLayers: [
            new WebTileLayer({
              //天地图电子地图
              urlTemplate:"http://{subDomain}.tianditu.gov.cn/vec_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=vec&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILECOL={col}&TILEROW={row}&TILEMATRIX={level}&tk=7b79e37115f10c512f76cdecda599902",
              subDomains: ["t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7"],
              copyright: "天地图",
            }),
          ],
          referenceLayers: [
            new WebTileLayer({
              //天地图电子地图注记
              // urlTemplate: "http://{subDomain}.tianditu.gov.cn/cva_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=vec&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILECOL={col}&TILEROW={row}&TILEMATRIX={level}&tk=7b79e37115f10c512f76cdecda599902",
              urlTemplate:window.PLATFROM_CONFIG.tdtMapdzdzzjUrl,
              subDomains: ["t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7"],
              copyright: "天地图注记",
            }),
          ],
          title: "basemap",
          id: "basemap",
        });

        let basemapyx = new Basemap({
          baseLayers: [
            new WebTileLayer({
              //天地图影像
              urlTemplate:"http://{subDomain}.tianditu.gov.cn/img_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILECOL={col}&TILEROW={row}&TILEMATRIX={level}&tk=7b79e37115f10c512f76cdecda599902",
              subDomains: ["t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7"],
              copyright: "天地图",
            }),
          ],
          referenceLayers: [
            new WebTileLayer({
              //天地图注记
              // urlTemplate: "http://{subDomain}.tianditu.gov.cn/cva_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=vec&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILECOL={col}&TILEROW={row}&TILEMATRIX={level}&tk=7b79e37115f10c512f76cdecda599902",
              urlTemplate:window.PLATFROM_CONFIG.tdtMapyxzjUrl,
              subDomains: ["t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7"],
              copyright: "天地图注记",
            }),
          ],
          title: "basemapxy",
          id: "basemapxy",
        });
        // basemap:{ baseLayers:[tdtMap, tdtMap_anno]},
        this.basemaps[0] = basemap;
        this.basemaps[1] = basemapyx;
        this.map = new Map({
          // basemap: "streets-vector",
          basemap: basemap,
          //layers: [layer]
        });

加载天地图示例:

<template>
	
		<view class="map" id="mapView">
		</view>
		 
	
</template>

<script setup>
import "@arcgis/core/assets/esri/themes/light/main.css";
import Map from "@arcgis/core/Map.js";
import MapView from "@arcgis/core/views/MapView.js";
import WebTileLayer from "@arcgis/core/layers/WebTileLayer.js";
import FeatureLayer from "@arcgis/core/layers/FeatureLayer.js"
import { onMounted } from "vue";	


onMounted(()=>{
	init();
});

const dzLayer = new WebTileLayer({
	urlTemplate: "http://{subDomain}.tianditu.gov.cn/vec_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=vec&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILECOL={col}&TILEROW={row}&TILEMATRIX={level}&tk=7b79e37115f10c512f76cdecda599902",
	subDomains: ['t0', 't1', 't2', 't3', 't4', 't5', 't6', 't7']
});

const dzbzLayer = new WebTileLayer({
	urlTemplate: "https://{subDomain}.tianditu.gov.cn/cva_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cva&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILECOL={col}&TILEROW={row}&TILEMATRIX={level}&tk=7b79e37115f10c512f76cdecda599902",
	subDomains: ['t0', 't1', 't2', 't3', 't4', 't5', 't6', 't7']
});

// 加载FeatureLayer
const featureLayer = new FeatureLayer({
	url:"http://116.141.0.114:48080/geoscene/rest/services/%E8%80%95%E4%BF%9D/%E5%8F%8C%E9%98%B3%E5%8C%BA%E5%9B%BE%E6%96%91/FeatureServer/0",
	outFields: ["*"], //加载所有要素字段
	opacity:0.5,//透明度
	//popupTemplate: TuCeng03TC, //加载模板,
	//definitionExpression: "",// 筛查
	// 渲染
	renderer: {
		type: "simple",
		symbol: {
			type: "simple-fill",//simple-line(线)、simple-fill(面)
			style: "solid",// solid 全部填充、cross十字交错、diamond菱形、square矩形、triangle三角形
			color: [255,20,60, 0.4],
			outline: {
				color: [255, 0, 0, 1],
				width: 2,
			},
		},
	}
});



// map初始化
const map = new Map({
	basemap:{
		baseLayers:[dzLayer],
		referenceLayers:[dzbzLayer]
	}
});


// 地图初始化
const init = function(){
	// mapView容器
	const mapView = new MapView({
		// 长春坐标系
		center:[125.331345,43.8328],
		// 初始层级
		zoom:10,
		container:"mapView",
		map:map,
		constraints: {
			minZoom: 10,// 最小层级
			maxZoom: 19 // 最大层级
		}
	});
	
	// 清除底部powered by ESRI
	mapView.ui.remove("attribution");
	
	map.add(featureLayer);
	

};

	
</script>

<style lang="scss" scoped>
	.map ::v-deep .esri-view-surface.esri-view-surface--touch-none:focus::after {
		outline: none !important;
	}
	.map {
		width: 100%;
		/* height: 185vw; */
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
	}
	#mapView{
		width: 100%;
		height:100%;
		padding: 0;
		margin: 0;
		
		.custom_popup{
			width: 50%;
			height: 30%;
			border: 1px solid red;
			background-color: #fff;
			position: absolute;
			z-index: 2;
			top: 0;
			right: 0;
			
			input{
				border: 1px solid yellow;
			}
			
			.scrollView{
				width: 100%;
				height:80%;
				border: 1px solid green;
			}
				
			.submit{
				background-color: blue;
			}
			
		}
	
	}
	
</style>

三维:

创建一个初始化函数initArcGisMap()用于创建Map实例和SceneView实例(三维场景)

注意:三维和二维引入的东西不一样

<template>
	<view id="sceneView">
		
</view>
</template>

<script setup>
import "@arcgis/core/assets/esri/themes/light/main.css";
import Map from '@arcgis/core/Map.js';
import MapView from '@arcgis/core/views/MapView.js';
import SceneView from '@arcgis/core/views/SceneView.js';
import { onMounted } from "vue";
	onMounted(()=>{
		initArcGisMap();
	})
	
	const initArcGisMap = () => {
		const map = new Map({
			basemap:"topo-vector"
		});
		
		const sceneView = new SceneView({
			 center: [-118.80500, 34.02700],
				zoom: 4,
				container: "sceneView",
				map: map
		});
		
		sceneView.ui.components = []
	
	}
	
</script>

<style lang="scss" scoped>
	#sceneView{
		width: 100%;
		height: 100vh;
	}

</style>

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值