135 lines
4.0 KiB
JavaScript
135 lines
4.0 KiB
JavaScript
/**
|
||
* 获取建筑围栏坐标的脚本
|
||
* 使用腾讯地图 WebService API
|
||
*/
|
||
|
||
const https = require('https')
|
||
|
||
// 由部署环境注入,避免把地图服务凭据写入仓库。
|
||
const API_KEY = process.env.TENCENT_MAP_WEBSERVICE_KEY
|
||
|
||
// 深圳自然博物馆坐标
|
||
const MUSEUM_LAT = 22.692763
|
||
const MUSEUM_LNG = 114.363487
|
||
|
||
/**
|
||
* 方法1:地点搜索 - 获取POI详情
|
||
*/
|
||
function searchPlace() {
|
||
const url = `https://apis.map.qq.com/ws/place/v1/search?keyword=深圳自然博物馆&boundary=nearby(${MUSEUM_LAT},${MUSEUM_LNG},1000)&key=${API_KEY}`
|
||
|
||
https.get(url, (res) => {
|
||
let data = ''
|
||
res.on('data', (chunk) => { data += chunk })
|
||
res.on('end', () => {
|
||
const result = JSON.parse(data)
|
||
console.log('=== 地点搜索结果 ===')
|
||
console.log(JSON.stringify(result, null, 2))
|
||
|
||
if (result.status === 0 && result.data.length > 0) {
|
||
const poi = result.data[0]
|
||
console.log('\n建筑信息:')
|
||
console.log('名称:', poi.title)
|
||
console.log('地址:', poi.address)
|
||
console.log('坐标:', poi.location)
|
||
console.log('POI ID:', poi.id)
|
||
}
|
||
})
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 方法2:逆地址解析 - 获取周边建筑
|
||
*/
|
||
function reverseGeocode() {
|
||
const url = `https://apis.map.qq.com/ws/geocoder/v1/?location=${MUSEUM_LAT},${MUSEUM_LNG}&key=${API_KEY}&get_poi=1`
|
||
|
||
https.get(url, (res) => {
|
||
let data = ''
|
||
res.on('data', (chunk) => { data += chunk })
|
||
res.on('end', () => {
|
||
const result = JSON.parse(data)
|
||
console.log('\n=== 逆地址解析结果 ===')
|
||
console.log(JSON.stringify(result, null, 2))
|
||
})
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 方法3:手动计算矩形围栏
|
||
* 根据建筑大致尺寸估算
|
||
*/
|
||
function calculateRectangle(centerLat, centerLng, widthMeters, heightMeters) {
|
||
// 纬度1度 ≈ 111km
|
||
// 经度1度 ≈ 111km * cos(纬度)
|
||
const latPerMeter = 1 / 111000
|
||
const lngPerMeter = 1 / (111000 * Math.cos(centerLat * Math.PI / 180))
|
||
|
||
const halfWidth = widthMeters / 2
|
||
const halfHeight = heightMeters / 2
|
||
|
||
const polygon = {
|
||
points: [
|
||
{
|
||
latitude: centerLat + halfHeight * latPerMeter,
|
||
longitude: centerLng - halfWidth * lngPerMeter,
|
||
label: '西北角'
|
||
},
|
||
{
|
||
latitude: centerLat + halfHeight * latPerMeter,
|
||
longitude: centerLng + halfWidth * lngPerMeter,
|
||
label: '东北角'
|
||
},
|
||
{
|
||
latitude: centerLat - halfHeight * latPerMeter,
|
||
longitude: centerLng + halfWidth * lngPerMeter,
|
||
label: '东南角'
|
||
},
|
||
{
|
||
latitude: centerLat - halfHeight * latPerMeter,
|
||
longitude: centerLng - halfWidth * lngPerMeter,
|
||
label: '西南角'
|
||
},
|
||
],
|
||
strokeWidth: 3,
|
||
strokeColor: '#E0E100',
|
||
fillColor: '#E0E10020',
|
||
zIndex: 1
|
||
}
|
||
|
||
console.log('\n=== 计算的矩形围栏 ===')
|
||
console.log(`建筑尺寸: ${widthMeters}m × ${heightMeters}m`)
|
||
console.log('坐标点:')
|
||
polygon.points.forEach(point => {
|
||
console.log(` ${point.label}: ${point.latitude.toFixed(6)}, ${point.longitude.toFixed(6)}`)
|
||
})
|
||
console.log('\n复制到代码:')
|
||
console.log(JSON.stringify(polygon, null, 2))
|
||
|
||
return polygon
|
||
}
|
||
|
||
if (!API_KEY) {
|
||
console.error('缺少 TENCENT_MAP_WEBSERVICE_KEY;请通过环境变量注入受域名/IP 限制的腾讯地图 WebService Key。')
|
||
process.exitCode = 1
|
||
return
|
||
}
|
||
|
||
console.log('正在获取深圳自然博物馆建筑围栏信息...\n')
|
||
|
||
// 执行查询
|
||
searchPlace()
|
||
reverseGeocode()
|
||
|
||
// 假设建筑尺寸为 150m × 100m(需要根据实际调整)
|
||
setTimeout(() => {
|
||
console.log('\n' + '='.repeat(50))
|
||
calculateRectangle(MUSEUM_LAT, MUSEUM_LNG, 150, 100)
|
||
}, 2000)
|
||
|
||
console.log('\n提示:')
|
||
console.log('1. 如果API返回的数据中没有建筑轮廓,需要手动标注')
|
||
console.log('2. 推荐使用腾讯地图坐标拾取器: https://lbs.qq.com/getPoint/')
|
||
console.log('3. 切换到卫星图模式,沿建筑外围点击获取坐标')
|
||
console.log('4. 或使用上面计算的矩形围栏,根据实际建筑尺寸调整参数')
|