Files
nxxmdata/backend/scripts/test-map-api.js
2025-08-25 15:00:46 +08:00

139 lines
4.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 百度地图API测试脚本
* 用于测试百度地图API服务是否正常工作
*/
require('dotenv').config();
const axios = require('axios');
// 百度地图API密钥
const BAIDU_MAP_AK = process.env.BAIDU_MAP_AK || 'your_baidu_map_ak';
// 测试地理编码API
async function testGeocode() {
try {
console.log('测试地理编码API...');
const address = '宁夏回族自治区银川市兴庆区北京东路';
const response = await axios.get('http://api.map.baidu.com/geocoding/v3', {
params: {
address,
output: 'json',
ak: BAIDU_MAP_AK
}
});
if (response.data.status === 0) {
console.log('地理编码成功:');
console.log(`地址: ${address}`);
console.log(`经度: ${response.data.result.location.lng}`);
console.log(`纬度: ${response.data.result.location.lat}`);
return true;
} else {
console.error('地理编码失败:', response.data.message);
return false;
}
} catch (error) {
console.error('地理编码测试错误:', error.message);
return false;
}
}
// 测试逆地理编码API
async function testReverseGeocode() {
try {
console.log('\n测试逆地理编码API...');
// 银川市中心坐标
const lat = 38.4864;
const lng = 106.2324;
const response = await axios.get('http://api.map.baidu.com/reverse_geocoding/v3', {
params: {
location: `${lat},${lng}`,
output: 'json',
ak: BAIDU_MAP_AK
}
});
if (response.data.status === 0) {
console.log('逆地理编码成功:');
console.log(`经度: ${lng}, 纬度: ${lat}`);
console.log(`地址: ${response.data.result.formatted_address}`);
return true;
} else {
console.error('逆地理编码失败:', response.data.message);
return false;
}
} catch (error) {
console.error('逆地理编码测试错误:', error.message);
return false;
}
}
// 测试IP定位API
async function testIpLocation() {
try {
console.log('\n测试IP定位API...');
const response = await axios.get('http://api.map.baidu.com/location/ip', {
params: {
ak: BAIDU_MAP_AK,
coor: 'bd09ll'
}
});
if (response.data.status === 0) {
console.log('IP定位成功:');
console.log(`地址: ${response.data.content.address}`);
console.log(`经度: ${response.data.content.point.x}`);
console.log(`纬度: ${response.data.content.point.y}`);
return true;
} else {
console.error('IP定位失败:', response.data.message);
return false;
}
} catch (error) {
console.error('IP定位测试错误:', error.message);
return false;
}
}
// 运行所有测试
async function runTests() {
console.log('===== 百度地图API测试 =====');
console.log(`使用的API密钥: ${BAIDU_MAP_AK}`);
if (BAIDU_MAP_AK === 'your_baidu_map_ak' || BAIDU_MAP_AK === 'your_baidu_map_ak_here') {
console.warn('警告: 您正在使用默认API密钥请在.env文件中设置有效的BAIDU_MAP_AK');
}
console.log('\n开始测试...');
const geocodeResult = await testGeocode();
const reverseGeocodeResult = await testReverseGeocode();
const ipLocationResult = await testIpLocation();
console.log('\n===== 测试结果汇总 =====');
console.log(`地理编码API: ${geocodeResult ? '✅ 通过' : '❌ 失败'}`);
console.log(`逆地理编码API: ${reverseGeocodeResult ? '✅ 通过' : '❌ 失败'}`);
console.log(`IP定位API: ${ipLocationResult ? '✅ 通过' : '❌ 失败'}`);
const allPassed = geocodeResult && reverseGeocodeResult && ipLocationResult;
console.log(`\n总体结果: ${allPassed ? '✅ 所有测试通过' : '❌ 部分测试失败'}`);
if (!allPassed) {
console.log('\n可能的问题:');
console.log('1. API密钥无效或未正确设置');
console.log('2. 网络连接问题');
console.log('3. 百度地图API服务暂时不可用');
console.log('\n解决方案:');
console.log('1. 检查.env文件中的BAIDU_MAP_AK设置');
console.log('2. 确保您的网络可以访问百度地图API');
console.log('3. 稍后再试');
}
}
// 执行测试
runTests().catch(error => {
console.error('测试执行错误:', error);
});