代码
import {BaiduMapConfig, BaiduVectorTileProvider, Engine, services} from '@baidumap/mapv-three';
const ak = BaiduMapConfig.ak || '您的密钥';
const hasKey = ak.trim() && !ak.includes('密钥');
if (hasKey) {
BaiduMapConfig.ak = ak;
}
const provider = hasKey ? new BaiduVectorTileProvider({displayOptions: {poi: false}}) : null;
const engine = new Engine('map', {
map: {
projection: 'EPSG:3857',
center: [118.83249662854782, 36.4193681358229],
pitch: 0,
range: 1_000_000,
provider,
},
});
const form = document.querySelector('#query');
const status = document.querySelector('#status');
// 行政区划服务仅支持百度服务源,需要有效 AK。
const district = hasKey
? new services.DistrictLayer({
name: '(山东省)',
kind: 1, // 0: 省级;1: 市级;2: 区县级
renderOptions: {
engine,
fillColor: ['#ff7875', '#ffd666', '#95de64', '#69c0ff'],
fillOpacity: 0.6,
depthTest: false,
autoViewport: true,
},
})
: null;
status.textContent = hasKey ? '' : '配置百度地图 AK 后可查询。';
form.addEventListener('submit', (event) => {
event.preventDefault();
if (!district) {
return;
}
const name = form.elements.name.value.trim();
if (!name) {
return;
}
// name 需带括号,如 (山东省)、(北京市)。
district.setOptions({
name: `(${name.replace(/^[((]|[))]$/g, '')})`,
kind: Number(form.elements.kind.value),
});
status.textContent = '';
});
调用 services.DistrictLayer 请求并绘制行政区划边界,默认显示山东省下各市。修改区域名称或层级后重新查询,地图自动定位到该区域。仅支持百度服务源,需配置有效 AK。
预览