代码
import {
BaiduMapConfig,
BaiduVectorTileProvider,
Engine,
GeoJSONDataSource,
Label,
services,
urlUtils,
} 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: [116.405, 39.92],
pitch: 0,
range: 12_000,
provider: provider,
},
});
const form = document.querySelector('#query');
const status = document.querySelector('#status');
if (!hasKey) {
status.textContent = '配置百度地图 AK 后可查询。';
}
const submit = document.querySelector('#submit');
const geocoder = new services.Geocoder({
apiSource: services.API_SOURCE_BAIDU,
});
const marker = engine.add(
new Label({
type: 'icontext',
mapSrc: urlUtils.getAssetUrl('assets/images/marker_red.png'),
iconWidth: 25,
iconHeight: 27,
textAnchor: 'bottom',
textSize: 13,
textWeight: '600',
textFillStyle: '#18334a',
textStrokeStyle: '#ffffff',
textStrokeWidth: 3,
padding: [5, 4],
pixelOffset: [0, 12],
depthTest: false,
depthWrite: false,
}),
);
function showAddress(point, text) {
marker.visible = Boolean(point);
if (point) {
const source = GeoJSONDataSource.fromGeoJSON({
type: 'Feature',
geometry: {type: 'Point', coordinates: [...point, 5]},
properties: {text: shortText(text)},
});
source.defineAttribute('text');
marker.dataSource = source;
}
engine.requestRender();
}
function shortText(text) {
const chars = Array.from(text);
return chars.length > 14 ? chars.slice(0, 13).join('') + '…' : text;
}
function pointArray(point) {
const values = Array.isArray(point) ? point : [point?.x, point?.y];
const [lng, lat] = values;
return Number.isFinite(lng) && Number.isFinite(lat) ? [lng, lat] : null;
}
async function withTimeout(promise) {
let timer;
const timeout = new Promise((resolve, reject) => {
timer = setTimeout(() => reject(new Error('请求超时')), 12_000);
});
try {
return await Promise.race([promise, timeout]);
} finally {
clearTimeout(timer);
}
}
form.addEventListener('submit', async (event) => {
event.preventDefault();
if (!hasKey) {
status.textContent = '配置百度地图 AK 后可查询。';
return;
}
const address = form.elements.address.value.trim();
const city = form.elements.city.value.trim();
if (!address || !city) {
status.textContent = '请输入城市和详细地址。';
return;
}
submit.disabled = true;
status.textContent = '正在解析地址…';
showAddress(null);
try {
const point = await withTimeout(geocoder.getPoint(address, undefined, city));
const coordinates = pointArray(point);
if (!coordinates) {
status.textContent = '未解析出坐标。请检查地址及 AK 的服务权限。';
return;
}
showAddress(coordinates, address);
engine.map.lookAt(coordinates, {
range: 2_000,
pitch: 0,
});
const [lng, lat] = coordinates;
status.textContent = `BD-09:${lng.toFixed(6)}, ${lat.toFixed(6)}`;
} catch {
status.textContent = '地理编码失败,请检查网络及 AK 的服务权限。';
} finally {
submit.disabled = false;
}
});
调用 services.Geocoder.getPoint 将地址转换为 BD-09 经纬度。示例提供城市作为检索范围,并在地图上标出返回坐标。 缺少密钥时可浏览 OpenFreeMap 预览底图。 用图标和查询地址标注返回的坐标。
预览