这是我在优化项目时偶然发现的,算是对区划掩膜的优化,同时也剔除了一些冗余的写法,以前的那篇可以不用参考了直接看这个就行,以前那篇就不删了算是记录一下代码方面的提升。哈哈哈哈
这里用到的最重要的属性是:blendMode: 'destination-in'
然后用你想要加载的区划JSON数据使用GeoJSONLayer 加载出来
// 加载杭州市区划
const hzsLayer = new GeoJSONLayer({
url: 'https://geo.datav.aliyun.com/areas_v3/bound/330100_full.json',
id: "hzsLayer",
popupEnabled: false,
renderer: {
type: 'simple',
symbol: {
type: 'simple-fill',
color: [227, 139, 79, 0],
outline: {
color:
[0, 122, 204, 0.8],
width: 2
}
}}
});
如果想要边界线可以直接map.add()添加到地图中去区划数据加载出来后再使用下面的方法加载到GraphicsLayer 在这个layer中添加blendMode: 'destination-in'使目标/背景图层只在与顶部图层重叠的地方绘制,其他的都是透明的这个时候地图的背景就是透明的了
// 加载图形图层
const graphicsLayer = new GraphicsLayer({
blendMode: 'destination-in',
id:'graphicsLayer'
})
/** * 获得图层中的 所有的features 然后添加到graphicsLayer */
async function showCity (layer) {
const { features } = await layer.queryFeatures()
features.forEach(feature => {
if (feature) {
const gra = feature.clone()
graphicsLayer.add(gra)
}
})
}
现在地图都是透明的了 直接给地图的元素添加背景图片就OK了
下面是完整的代码(记得添加你自己天地图的key)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<title>地图掩膜添加背景图片</title>
<link rel="stylesheet" href="https://js.arcgis.com/4.23/esri/themes/light/main.css" />
<script src="https://js.arcgis.com/4.23/"></script>
<style>
html,
body,
#viewDiv {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
}
#viewDiv{
background: url('./image/mapbg.jpg') no-repeat;
background-size:100% 100%;
}
</style>
<script>
require([ "esri/Map", "esri/views/MapView","esri/layers/support/TileInfo", "esri/layers/WebTileLayer", "esri/layers/GeoJSONLayer", "esri/layers/GraphicsLayer", "esri/layers/GroupLayer","esri/geometry/Point","esri/Graphic"],
function (Map, MapView,TileInfo,WebTileLayer, GeoJSONLayer, GraphicsLayer, GroupLayer,Point,Graphic) {
const tileInfo = new TileInfo({
dpi: 96,
rows: 256,
cols: 256,
compressionQuality: 0,
origin: {
x: -180,
y: 90
},
spatialReference: {
wkid: 4490
},
lods: [
{ level: 0, levelValue: 1, resolution: 0.703125, scale: 295497593.05875003 },
{ level: 1, levelValue: 2, resolution: 0.3515625, scale: 147748796.52937502 },
{ level: 2, levelValue: 3, resolution: 0.17578125, scale: 73874398.264687508 },
{ level: 3, levelValue: 4, resolution: 0.087890625, scale: 36937199.132343754 },
{ level: 4, levelValue: 5, resolution: 0.0439453125, scale: 18468599.566171877 },
{ level: 5, levelValue: 6, resolution: 0.02197265625, scale: 9234299.7830859385 },
{ level: 6, levelValue: 7, resolution: 0.010986328125, scale: 4617149.8915429693 },
{ level: 7, levelValue: 8, resolution: 0.0054931640625, scale: 2308574.9457714846 },
{ level: 8, levelValue: 9, resolution: 0.00274658203125, scale: 1154287.4728857423 },
{ level: 9, levelValue: 10, resolution: 0.001373291015625, scale: 577143.73644287116 },
{ level: 10, levelValue: 11, resolution: 0.0006866455078125, scale: 288571.86822143558 },
{ level: 11, levelValue: 12, resolution: 0.00034332275390625, scale: 144285.93411071779 },
{ level: 12, levelValue: 13, resolution: 0.000171661376953125, scale: 72142.967055358895 },
{ level: 13, levelValue: 14, resolution: 8.58306884765625e-005, scale: 36071.483527679447 },
{ level: 14, levelValue: 15, resolution: 4.291534423828125e-005, scale: 18035.741763839724 },
{ level: 15, levelValue: 16, resolution: 2.1457672119140625e-005, scale: 9017.8708819198619 },
{ level: 16, levelValue: 17, resolution: 1.0728836059570313e-005, scale: 4508.9354409599309 },
{ level: 17, levelValue: 18, resolution: 5.3644180297851563e-006, scale: 2254.4677204799655 },
{ level: 18, levelValue: 19, resolution: 2.68220901489257815e-006, scale: 1127.23386023998275 },
{ level: 19, levelValue: 20, resolution: 1.341104507446289075e-006, scale: 563.616930119991375 }
]
})
const tianLayer = new WebTileLayer({ // 天地图影像地图
urlTemplate: "http://{subDomain}.tianditu.gov.cn/DataServer?T=img_c&x={col}&y={row}&l={level}&tk=xxxx",
subDomains: ['t0', 't1', 't2', 't3', 't4', 't5', 't6', 't7'],
tileInfo,
spatialReference: {
wkid: 4490
}
});
// 加载图形图层
const graphicsLayer = new GraphicsLayer({
blendMode: 'destination-in',
id:'graphicsLayer'
})
// 加载杭州市区划
const hzsLayer = new GeoJSONLayer({
url: 'https://geo.datav.aliyun.com/areas_v3/bound/330100_full.json',
id: "hzsLayer",
popupEnabled: false,
renderer: {
type: 'simple',
symbol: {
type: 'simple-fill',
color: [227, 139, 79, 0],
outline: {
color:
[0, 122, 204, 0.8],
width: 2
}
}}
});
showCity(hzsLayer)
/** * 获得图层中的 所有的features 然后添加到graphicsLayer */
async function showCity (layer) {
const { features } = await layer.queryFeatures()
features.forEach(feature => {
if (feature) {
const gra = feature.clone()
graphicsLayer.add(gra)
}
})
}
const map = new Map({
spatialReference: {
wkid: 4490
},
layers:[tianLayer,graphicsLayer]
});
const view = new MapView({
map: map,
spatialReference: {
wkid: 4490
},
container: "viewDiv",
center: {
x:119.517,
y:29.818,
spatialReference: {
wkid: 4490
}
}, // 指定中心点
zoom: 9,
});
});
</script>
<body>
<div id="viewDiv"></div>
</body>
</html>