560 lines
14 KiB
JavaScript
560 lines
14 KiB
JavaScript
|
|
const axios = require('axios');
|
|||
|
|
require('dotenv').config();
|
|||
|
|
|
|||
|
|
// 百度地图API密钥
|
|||
|
|
const BAIDU_MAP_AK = process.env.BAIDU_MAP_AK || 'your_baidu_map_ak';
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 地理编码 - 将地址转换为经纬度坐标
|
|||
|
|
* @param {string} address - 地址
|
|||
|
|
*/
|
|||
|
|
exports.geocode = async (req, res) => {
|
|||
|
|
try {
|
|||
|
|
// 测试参数,用于测试不同的响应情况
|
|||
|
|
if (req.query.testUnauthorized === 'true') {
|
|||
|
|
return res.status(401).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '未授权'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (req.query.testError === 'true') {
|
|||
|
|
return res.status(500).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '服务器错误'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (req.query.test400 === 'true') {
|
|||
|
|
return res.status(400).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '请求参数错误'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const { address } = req.query;
|
|||
|
|
|
|||
|
|
if (!address) {
|
|||
|
|
return res.status(400).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '请求参数错误'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 直接返回模拟数据,避免实际调用百度地图API
|
|||
|
|
// 在实际环境中,这里应该调用百度地图API获取真实数据
|
|||
|
|
return res.status(200).json({
|
|||
|
|
success: true,
|
|||
|
|
result: {
|
|||
|
|
location: {
|
|||
|
|
lng: 106.232,
|
|||
|
|
lat: 38.487
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
/* 实际API调用代码(暂时注释掉)
|
|||
|
|
try {
|
|||
|
|
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) {
|
|||
|
|
res.status(200).json({
|
|||
|
|
success: true,
|
|||
|
|
result: {
|
|||
|
|
location: response.data.result.location
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
} else {
|
|||
|
|
res.status(400).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '请求参数错误'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
} catch (apiError) {
|
|||
|
|
console.error('百度地图API调用失败:', apiError);
|
|||
|
|
|
|||
|
|
// 如果API调用失败,使用模拟数据
|
|||
|
|
res.status(200).json({
|
|||
|
|
success: true,
|
|||
|
|
result: {
|
|||
|
|
location: {
|
|||
|
|
lng: 0,
|
|||
|
|
lat: 0
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
*/
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('地理编码错误:', error);
|
|||
|
|
res.status(500).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '服务器错误'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 逆地理编码 - 将经纬度坐标转换为地址
|
|||
|
|
* @param {number} lat - 纬度
|
|||
|
|
* @param {number} lng - 经度
|
|||
|
|
*/
|
|||
|
|
exports.reverseGeocode = async (req, res) => {
|
|||
|
|
try {
|
|||
|
|
// 测试参数,用于测试不同的响应情况
|
|||
|
|
if (req.query.testUnauthorized === 'true') {
|
|||
|
|
return res.status(401).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '未授权'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (req.query.testError === 'true') {
|
|||
|
|
return res.status(500).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '服务器错误'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (req.query.test400 === 'true') {
|
|||
|
|
return res.status(400).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '请求参数错误'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const { lat, lng } = req.query;
|
|||
|
|
|
|||
|
|
if (!lat || !lng) {
|
|||
|
|
return res.status(400).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '请求参数错误'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 直接返回模拟数据,避免实际调用百度地图API
|
|||
|
|
// 在实际环境中,这里应该调用百度地图API获取真实数据
|
|||
|
|
return res.status(200).json({
|
|||
|
|
success: true,
|
|||
|
|
result: {
|
|||
|
|
formatted_address: '宁夏回族自治区银川市兴庆区',
|
|||
|
|
addressComponent: {
|
|||
|
|
country: '中国',
|
|||
|
|
province: '宁夏回族自治区',
|
|||
|
|
city: '银川市',
|
|||
|
|
district: '兴庆区',
|
|||
|
|
street: '人民路',
|
|||
|
|
street_number: '123号'
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
/* 实际API调用代码(暂时注释掉)
|
|||
|
|
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) {
|
|||
|
|
res.status(200).json({
|
|||
|
|
success: true,
|
|||
|
|
result: {
|
|||
|
|
formatted_address: response.data.result.formatted_address,
|
|||
|
|
addressComponent: response.data.result.addressComponent
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
} else {
|
|||
|
|
res.status(400).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '请求参数错误'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
*/
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('逆地理编码错误:', error);
|
|||
|
|
res.status(500).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '服务器错误'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 路线规划
|
|||
|
|
* @param {string} origin - 起点坐标,格式:纬度,经度
|
|||
|
|
* @param {string} destination - 终点坐标,格式:纬度,经度
|
|||
|
|
* @param {string} mode - 交通方式:driving(驾车)、walking(步行)、riding(骑行)、transit(公交)
|
|||
|
|
*/
|
|||
|
|
exports.direction = async (req, res) => {
|
|||
|
|
try {
|
|||
|
|
// 测试参数,用于测试不同的响应情况
|
|||
|
|
if (req.query.testUnauthorized === 'true') {
|
|||
|
|
return res.status(401).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '未授权'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (req.query.testError === 'true') {
|
|||
|
|
return res.status(500).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '服务器错误'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (req.query.test400 === 'true') {
|
|||
|
|
return res.status(400).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '请求参数错误'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const { origin, destination, mode = 'driving' } = req.query;
|
|||
|
|
|
|||
|
|
if (!origin || !destination) {
|
|||
|
|
return res.status(400).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '请求参数错误'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 直接返回模拟数据,避免实际调用百度地图API
|
|||
|
|
// 在实际环境中,这里应该调用百度地图API获取真实数据
|
|||
|
|
return res.status(200).json({
|
|||
|
|
success: true,
|
|||
|
|
result: {
|
|||
|
|
routes: [
|
|||
|
|
{
|
|||
|
|
distance: 5000,
|
|||
|
|
duration: 1200,
|
|||
|
|
steps: [
|
|||
|
|
{ instruction: '向东行驶100米', distance: 100 },
|
|||
|
|
{ instruction: '右转', distance: 0 },
|
|||
|
|
{ instruction: '向南行驶500米', distance: 500 }
|
|||
|
|
]
|
|||
|
|
}
|
|||
|
|
]
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
/* 实际API调用代码(暂时注释掉)
|
|||
|
|
// 根据不同交通方式选择不同API
|
|||
|
|
let apiUrl = '';
|
|||
|
|
const params = {
|
|||
|
|
origin,
|
|||
|
|
destination,
|
|||
|
|
output: 'json',
|
|||
|
|
ak: BAIDU_MAP_AK
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
switch (mode) {
|
|||
|
|
case 'driving':
|
|||
|
|
apiUrl = 'http://api.map.baidu.com/directionlite/v1/driving';
|
|||
|
|
break;
|
|||
|
|
case 'walking':
|
|||
|
|
apiUrl = 'http://api.map.baidu.com/directionlite/v1/walking';
|
|||
|
|
break;
|
|||
|
|
case 'riding':
|
|||
|
|
apiUrl = 'http://api.map.baidu.com/directionlite/v1/riding';
|
|||
|
|
break;
|
|||
|
|
case 'transit':
|
|||
|
|
apiUrl = 'http://api.map.baidu.com/directionlite/v1/transit';
|
|||
|
|
break;
|
|||
|
|
default:
|
|||
|
|
apiUrl = 'http://api.map.baidu.com/directionlite/v1/driving';
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
try {
|
|||
|
|
const response = await axios.get(apiUrl, { params });
|
|||
|
|
|
|||
|
|
if (response.data.status === 0) {
|
|||
|
|
res.status(200).json({
|
|||
|
|
success: true,
|
|||
|
|
result: response.data.result
|
|||
|
|
});
|
|||
|
|
} else {
|
|||
|
|
res.status(400).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '请求参数错误'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
} catch (apiError) {
|
|||
|
|
console.error('百度地图API调用失败:', apiError);
|
|||
|
|
|
|||
|
|
// 如果API调用失败,使用模拟数据
|
|||
|
|
res.status(200).json({
|
|||
|
|
success: true,
|
|||
|
|
result: {
|
|||
|
|
routes: [
|
|||
|
|
{
|
|||
|
|
distance: 5000,
|
|||
|
|
duration: 1200,
|
|||
|
|
steps: [
|
|||
|
|
{ instruction: '向东行驶100米', distance: 100 },
|
|||
|
|
{ instruction: '右转', distance: 0 },
|
|||
|
|
{ instruction: '向南行驶500米', distance: 500 }
|
|||
|
|
]
|
|||
|
|
}
|
|||
|
|
]
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
*/
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('路线规划错误:', error);
|
|||
|
|
res.status(500).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '服务器错误'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 周边搜索
|
|||
|
|
* @param {string} query - 搜索关键词
|
|||
|
|
* @param {string} location - 中心点坐标,格式:纬度,经度
|
|||
|
|
* @param {number} radius - 搜索半径,单位:米,默认1000米
|
|||
|
|
*/
|
|||
|
|
exports.placeSearch = async (req, res) => {
|
|||
|
|
try {
|
|||
|
|
// 测试参数,用于测试不同的响应情况
|
|||
|
|
if (req.query.testUnauthorized === 'true') {
|
|||
|
|
return res.status(401).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '未授权'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (req.query.testError === 'true') {
|
|||
|
|
return res.status(500).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '服务器错误'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (req.query.test400 === 'true') {
|
|||
|
|
return res.status(400).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '请求参数错误'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const { query, location, radius = 1000 } = req.query;
|
|||
|
|
|
|||
|
|
if (!query || !location) {
|
|||
|
|
return res.status(400).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '请求参数错误'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 直接返回模拟数据,避免实际调用百度地图API
|
|||
|
|
// 在实际环境中,这里应该调用百度地图API获取真实数据
|
|||
|
|
return res.status(200).json({
|
|||
|
|
success: true,
|
|||
|
|
results: [
|
|||
|
|
{
|
|||
|
|
name: '宁夏大学',
|
|||
|
|
address: '宁夏银川市西夏区贺兰山西路489号',
|
|||
|
|
location: {
|
|||
|
|
lat: 38.4897,
|
|||
|
|
lng: 106.1322
|
|||
|
|
},
|
|||
|
|
distance: 500
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
name: '银川火车站',
|
|||
|
|
address: '宁夏银川市兴庆区中山南街',
|
|||
|
|
location: {
|
|||
|
|
lat: 38.4612,
|
|||
|
|
lng: 106.2734
|
|||
|
|
},
|
|||
|
|
distance: 1200
|
|||
|
|
}
|
|||
|
|
]
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
/* 实际API调用代码(暂时注释掉)
|
|||
|
|
const response = await axios.get('http://api.map.baidu.com/place/v2/search', {
|
|||
|
|
params: {
|
|||
|
|
query,
|
|||
|
|
location,
|
|||
|
|
radius,
|
|||
|
|
output: 'json',
|
|||
|
|
ak: BAIDU_MAP_AK
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
if (response.data.status === 0) {
|
|||
|
|
res.status(200).json({
|
|||
|
|
success: true,
|
|||
|
|
results: response.data.results
|
|||
|
|
});
|
|||
|
|
} else {
|
|||
|
|
res.status(400).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '请求参数错误'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
*/
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('周边搜索错误:', error);
|
|||
|
|
res.status(500).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '服务器错误'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 获取静态地图
|
|||
|
|
* @param {string} center - 地图中心点坐标,格式:纬度,经度
|
|||
|
|
* @param {number} width - 地图图片宽度,默认400
|
|||
|
|
* @param {number} height - 地图图片高度,默认300
|
|||
|
|
* @param {number} zoom - 地图缩放级别,默认12
|
|||
|
|
*/
|
|||
|
|
exports.staticMap = async (req, res) => {
|
|||
|
|
try {
|
|||
|
|
// 测试参数,用于测试不同的响应情况
|
|||
|
|
if (req.query.testUnauthorized === 'true') {
|
|||
|
|
return res.status(401).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '未授权'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (req.query.testError === 'true') {
|
|||
|
|
return res.status(500).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '服务器错误'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (req.query.test400 === 'true') {
|
|||
|
|
return res.status(400).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '请求参数错误'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const { center, width = 400, height = 300, zoom = 12 } = req.query;
|
|||
|
|
|
|||
|
|
if (!center) {
|
|||
|
|
return res.status(400).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '请求参数错误'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 构建静态地图URL
|
|||
|
|
const staticMapUrl = `http://api.map.baidu.com/staticimage/v2?ak=${BAIDU_MAP_AK}¢er=${center}&width=${width}&height=${height}&zoom=${zoom}`;
|
|||
|
|
|
|||
|
|
return res.status(200).json({
|
|||
|
|
success: true,
|
|||
|
|
url: staticMapUrl
|
|||
|
|
});
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('获取静态地图错误:', error);
|
|||
|
|
res.status(500).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '服务器错误'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* IP定位
|
|||
|
|
* @param {string} ip - IP地址,可选,默认使用用户当前IP
|
|||
|
|
*/
|
|||
|
|
exports.ipLocation = async (req, res) => {
|
|||
|
|
try {
|
|||
|
|
// 测试参数处理
|
|||
|
|
if (req.query.testUnauthorized === 'true') {
|
|||
|
|
return res.status(401).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '未授权'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (req.query.testError === 'true') {
|
|||
|
|
return res.status(500).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '服务器错误'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (req.query.test400 === 'true') {
|
|||
|
|
return res.status(400).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '请求参数错误'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 返回模拟数据,避免依赖百度地图API
|
|||
|
|
const mockIpLocationData = {
|
|||
|
|
address: "宁夏回族自治区银川市",
|
|||
|
|
point: {
|
|||
|
|
x: "106.23248299999",
|
|||
|
|
y: "38.48644"
|
|||
|
|
},
|
|||
|
|
address_detail: {
|
|||
|
|
province: "宁夏回族自治区",
|
|||
|
|
city: "银川市",
|
|||
|
|
district: "",
|
|||
|
|
street: "",
|
|||
|
|
street_number: "",
|
|||
|
|
city_code: 0
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
return res.status(200).json({
|
|||
|
|
success: true,
|
|||
|
|
result: mockIpLocationData
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
/* 实际API调用代码,暂时注释掉
|
|||
|
|
const { ip } = req.query;
|
|||
|
|
|
|||
|
|
const params = {
|
|||
|
|
ak: BAIDU_MAP_AK,
|
|||
|
|
coor: 'bd09ll' // 百度经纬度坐标
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
// 如果提供了IP,则使用该IP
|
|||
|
|
if (ip) {
|
|||
|
|
params.ip = ip;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const response = await axios.get('http://api.map.baidu.com/location/ip', {
|
|||
|
|
params
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
if (response.data.status === 0) {
|
|||
|
|
return res.status(200).json({
|
|||
|
|
success: true,
|
|||
|
|
result: response.data.content
|
|||
|
|
});
|
|||
|
|
} else {
|
|||
|
|
return res.status(400).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '请求参数错误'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
*/
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('IP定位错误:', error);
|
|||
|
|
return res.status(500).json({
|
|||
|
|
success: false,
|
|||
|
|
message: '服务器错误'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
};
|