代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>精简版-演示</title>
<script src="https://api.map.baidu.com/api?type=webgl&v=1.0&ak=MOCOlcworadtFqL6jbaIwFsq"></script>
<script src="https://wuliu-web.cdn.bcebos.com/sdk/park-simple/0.0.4/index.umd.min.js"></script>
<style>
html,
body {
margin: 0;
padding: 0;
font-size: 14px;
color: rgba(0, 0, 0, 0.85);
}
.auth-form {
padding-top: 24px;
}
.form-item {
display: flex;
margin-bottom: 24px;
}
.form-item > label {
width: 12.5%;
margin-left: 12.5%;
padding-right: 8px;
line-height: 32px;
text-align: right;
}
.form-item > label:not(:empty)::after {
content: ':';
margin-left: 2px;
}
.form-item > label.required::before {
content: '*';
margin-right: 4px;
color: #ff4d4f;
}
.form-control {
width: 62.5%;
}
.form-control input,
.search input {
width: 100%;
height: 32px;
padding: 4px 11px;
border: 1px solid #d9d9d9;
border-radius: 2px;
outline: none;
}
.form-control input:focus {
border-color: #40a9ff;
}
.form-control input.error {
border-color: #ff4d4f;
}
.form-error {
min-height: 0;
color: #ff4d4f;
}
.btn-primary {
height: 32px;
padding: 4px 15px;
color: #fff;
background: #1890ff;
border: 1px solid #1890ff;
border-radius: 2px;
cursor: pointer;
}
.map-page {
position: relative;
}
.map {
width: 100%;
height: 100vh;
}
.search {
position: absolute;
top: 40px;
right: 20px;
z-index: 999;
width: 340px;
}
.search-result {
max-height: 256px;
margin: 4px 0 0;
padding: 4px 0;
overflow-y: auto;
list-style: none;
background: #fff;
border-radius: 2px;
box-shadow:
0 3px 6px -4px rgba(0, 0, 0, 0.12),
0 6px 16px 0 rgba(0, 0, 0, 0.08);
}
.search-result li {
padding: 5px 12px;
cursor: pointer;
}
.search-result li:hover,
.search-result li:focus {
background: #f5f5f5;
outline: none;
}
.search-result .address {
margin-left: 4px;
font-size: 10px;
color: #333;
}
</style>
</head>
<body>
<form id="authForm" class="auth-form" novalidate>
<div class="form-item">
<label for="ID" class="required">ID</label>
<div class="form-control">
<input id="ID" name="ID" type="text" autocomplete="off" />
<div class="form-error" role="alert"></div>
</div>
</div>
<div class="form-item">
<label for="Y" class="required">Y</label>
<div class="form-control">
<input id="Y" name="Y" type="text" autocomplete="off" />
<div class="form-error" role="alert"></div>
</div>
</div>
<div class="form-item">
<label for="SK" class="required">SK</label>
<div class="form-control">
<input id="SK" name="SK" type="text" autocomplete="off" />
<div class="form-error" role="alert"></div>
</div>
</div>
<div class="form-item">
<label for="parkCode">parkCode</label>
<div class="form-control">
<input id="parkCode" name="parkCode" type="text" autocomplete="off" />
</div>
</div>
<div class="form-item">
<label></label>
<div class="form-control">
<button type="submit" class="btn-primary">显示地图</button>
</div>
</div>
</form>
<div id="mapPage" class="map-page" hidden>
<div class="search">
<input
id="searchInput"
type="text"
placeholder="搜索需要检索的内容或者lng,lat"
autocomplete="off"
aria-label="搜索地点或经纬度" />
<ul id="searchResult" class="search-result" role="listbox" hidden></ul>
</div>
<div id="map" class="map"></div>
</div>
<script>
const {BMapGL} = window;
const REQUIRED_FIELDS = ['ID', 'Y', 'SK'];
let map = null;
let marker = null;
// 表单校验与页面切换
const authForm = document.getElementById('authForm');
authForm.addEventListener('submit', (e) => {
e.preventDefault();
const auth = {};
let valid = true;
REQUIRED_FIELDS.forEach((name) => {
const input = document.getElementById(name);
const value = input.value.trim();
input.classList.toggle('error', !value);
input.nextElementSibling.textContent = value ? '' : `请输入 ${name}`;
if (value) auth[name] = value;
else valid = false;
});
if (!valid) return;
const parkCode = document.getElementById('parkCode').value.trim();
if (parkCode) auth.parkCode = parkCode;
if (!window.ParkSimple?.LogisticsSimple) {
alert('未加载 LogisticsSimple SDK,请替换 script 地址');
return;
}
authForm.hidden = true;
document.getElementById('mapPage').hidden = false;
map = initMap(auth);
});
// 地图初始化与 LogisticsSimple 接入
const toPoint = ([lng, lat]) => new BMapGL.Point(lng, lat);
// 由对角两点生成首尾闭合的矩形顶点
const rectPath = (start, end) => [
toPoint(start),
toPoint([end[0], start[1]]),
toPoint(end),
toPoint([start[0], end[1]]),
toPoint(start),
];
function initMap({ID, Y, SK, parkCode}) {
const _map = new BMapGL.Map('map', {maxZoom: 23, enableIconClick: true});
_map.centerAndZoom(new BMapGL.Point(106.52830194662107, 38.23834139133826), 12);
_map.enableScrollWheelZoom(true);
new window.ParkSimple.LogisticsSimple({
ID,
Y,
SK,
parkCode,
map: _map,
onReady: (data) => {
console.log('地图渲染完成==', data);
if (!Array.isArray(data) || !data.length) return;
data.forEach((item) => {
const [start, end] = item.coordinates[0];
_map.addOverlay(
new BMapGL.Polygon(rectPath(start, end), {
fillOpacity: 0,
strokeStyle: 'dashed',
strokeColor: 'blue',
strokeWeight: 8,
strokeOpacity: 1,
}),
);
// 创建文本标注对象
const label = new BMapGL.Label(item.name, {
position: toPoint(start), // 指定文本标注所在的地理位置
offset: new BMapGL.Size(-30, 0), // 设置文本偏移量
});
label.setStyle({
color: 'red',
borderRadius: '5px',
borderColor: '#ccc',
padding: '4px',
fontSize: '16px',
fontFamily: '微软雅黑',
});
_map.addOverlay(label);
});
_map.setViewport(data[0].coordinates[0].slice(0, 2).map(toPoint));
},
});
return _map;
}
// 搜索框
const searchInput = document.getElementById('searchInput');
const searchResult = document.getElementById('searchResult');
let timeout = null;
let searchSeq = 0;
let pendingSeq = 0;
let localSearch = null;
let resultList = [];
const isLngAndLat = (value) => /^[\d.]+,[\d.]+$/.test(value);
const createEl = (tag, props) => Object.assign(document.createElement(tag), props);
function createResultItem({title, address}, index) {
const li = createEl('li', {tabIndex: 0});
li.setAttribute('role', 'option');
li.dataset.index = index;
li.append(createEl('span', {textContent: title}));
if (address) li.append(createEl('span', {className: 'address', textContent: address}));
return li;
}
function renderResult(list) {
resultList = list;
searchResult.replaceChildren(...list.map(createResultItem));
searchResult.hidden = !list.length;
}
function removeMarker() {
if (marker) map.removeOverlay(marker);
marker = null;
}
function onSelect({title, point}) {
removeMarker();
marker = new BMapGL.Marker(point);
// 在地图上添加点标记
map.addOverlay(marker);
map.setViewport([point]);
searchInput.value = title;
renderResult([]);
}
// 结果列表事件委托
const pickFromEvent = (e) => {
const li = e.target.closest('li[data-index]');
if (li) onSelect(resultList[li.dataset.index]);
};
searchResult.addEventListener('click', pickFromEvent);
searchResult.addEventListener('keydown', (e) => {
if (e.key === 'Enter') pickFromEvent(e);
});
// LocalSearch 在首次检索时创建并复用(地图初始化后才会触发检索)
const localSearchOpts = {
pageCapacity: 50,
onSearchComplete: (data) => {
if (pendingSeq !== searchSeq) return; // 丢弃过期结果
renderResult(data && Array.isArray(data._pois) ? data._pois : []);
},
};
function fetchServer(value) {
searchSeq += 1;
if (isLngAndLat(value)) {
const [lng, lat] = value.split(',').map(Number);
renderResult([{title: value, point: new BMapGL.Point(lng, lat)}]);
return;
}
pendingSeq = searchSeq;
localSearch ??= new BMapGL.LocalSearch(map, localSearchOpts);
localSearch.search(value);
}
searchInput.addEventListener('input', () => {
const value = searchInput.value.trim();
clearTimeout(timeout);
if (!value) {
searchSeq += 1;
renderResult([]);
removeMarker();
return;
}
timeout = setTimeout(() => fetchServer(value), 300);
});
</script>
</body>
</html>
预览