geoapify-mcp-server
Version:
Geoapify API MCP Server for location-based services - 一键部署的地理位置服务
293 lines (258 loc) • 7.33 kB
JavaScript
// Geoapify MCP Server 客户端调用示例
// 1. 地理编码示例
async function geocodeExample() {
const response = await fetch('http://localhost:50001/tools/geocode_address', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': 'your_mcp_api_key' // 如果设置了MCP_API_KEY
},
body: JSON.stringify({
arguments: {
address: "北京市天安门广场",
language: "zh",
limit: 5
}
})
});
const result = await response.json();
console.log('地理编码结果:', result);
}
// 2. 反向地理编码示例
async function reverseGeocodeExample() {
const response = await fetch('http://localhost:50001/tools/reverse_geocode', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': 'your_mcp_api_key'
},
body: JSON.stringify({
arguments: {
lat: 39.9042,
lon: 116.4074,
language: "zh"
}
})
});
const result = await response.json();
console.log('反向地理编码结果:', result);
}
// 3. 路线规划示例
async function routingExample() {
const response = await fetch('http://localhost:50001/tools/calculate_route', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': 'your_mcp_api_key'
},
body: JSON.stringify({
arguments: {
waypoints: ["39.9042,116.4074", "31.2304,121.4737"], // 北京到上海
mode: "drive",
type: "balanced",
details: "instruction_details"
}
})
});
const result = await response.json();
console.log('路线规划结果:', result);
}
// 4. 地点搜索示例
async function placesSearchExample() {
const response = await fetch('http://localhost:50001/tools/search_places', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': 'your_mcp_api_key'
},
body: JSON.stringify({
arguments: {
categories: "catering.restaurant,catering.cafe",
filter: "circle:116.4074,39.9042,1000", // 天安门周围1公里
limit: 20,
language: "zh"
}
})
});
const result = await response.json();
console.log('地点搜索结果:', result);
}
// 5. 地址自动补全示例
async function autocompleteExample() {
const response = await fetch('http://localhost:50001/tools/address_autocomplete', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': 'your_mcp_api_key'
},
body: JSON.stringify({
arguments: {
text: "北京市朝阳",
country: "cn",
limit: 10,
language: "zh"
}
})
});
const result = await response.json();
console.log('地址自动补全结果:', result);
}
// 6. 等时线计算示例
async function isolineExample() {
const response = await fetch('http://localhost:50001/tools/calculate_isoline', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': 'your_mcp_api_key'
},
body: JSON.stringify({
arguments: {
lat: 39.9042,
lon: 116.4074,
type: "time",
mode: "drive",
range: 1800 // 30分钟可达范围
}
})
});
const result = await response.json();
console.log('等时线结果:', result);
}
// 7. 批量调用示例
async function batchExample() {
const addresses = [
"北京市天安门广场",
"上海市外滩",
"广州市珠江新城",
"深圳市南山区"
];
const promises = addresses.map(address =>
fetch('http://localhost:50001/tools/geocode_address', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': 'your_mcp_api_key'
},
body: JSON.stringify({
arguments: {
address,
language: "zh"
}
})
}).then(res => res.json())
);
const results = await Promise.all(promises);
console.log('批量地理编码结果:', results);
}
// 8. 错误处理示例
async function errorHandlingExample() {
try {
const response = await fetch('http://localhost:50001/tools/geocode_address', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': 'invalid_key'
},
body: JSON.stringify({
arguments: {
address: "无效地址"
}
})
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
const result = await response.json();
if (!result.success) {
throw new Error(result.error || '未知错误');
}
console.log('成功结果:', result);
} catch (error) {
console.error('错误处理:', error.message);
}
}
// 9. 使用axios的示例
const axios = require('axios');
class GeoapifyMCPClient {
constructor(baseURL, apiKey) {
this.client = axios.create({
baseURL,
headers: {
'Content-Type': 'application/json',
'X-API-Key': apiKey
},
timeout: 30000
});
}
async geocode(address, options = {}) {
const response = await this.client.post('/tools/geocode_address', {
arguments: { address, ...options }
});
return response.data;
}
async reverseGeocode(lat, lon, options = {}) {
const response = await this.client.post('/tools/reverse_geocode', {
arguments: { lat, lon, ...options }
});
return response.data;
}
async calculateRoute(waypoints, mode = 'drive', options = {}) {
const response = await this.client.post('/tools/calculate_route', {
arguments: { waypoints, mode, ...options }
});
return response.data;
}
async searchPlaces(categories, filter, options = {}) {
const response = await this.client.post('/tools/search_places', {
arguments: { categories, filter, ...options }
});
return response.data;
}
}
// 使用客户端类
async function clientClassExample() {
const client = new GeoapifyMCPClient('http://localhost:50001', 'your_mcp_api_key');
try {
// 地理编码
const geocodeResult = await client.geocode('北京市故宫博物院', { language: 'zh' });
console.log('地理编码:', geocodeResult);
// 搜索附近餐厅
const placesResult = await client.searchPlaces(
'catering.restaurant',
'circle:116.4074,39.9042,1000',
{ limit: 10, language: 'zh' }
);
console.log('附近餐厅:', placesResult);
} catch (error) {
console.error('客户端错误:', error.message);
}
}
// 运行示例
async function runExamples() {
console.log('=== Geoapify MCP Server 调用示例 ===\n');
await geocodeExample();
await reverseGeocodeExample();
await routingExample();
await placesSearchExample();
await autocompleteExample();
await isolineExample();
await batchExample();
await errorHandlingExample();
await clientClassExample();
}
// 如果直接运行此文件
if (require.main === module) {
runExamples().catch(console.error);
}
module.exports = {
GeoapifyMCPClient,
geocodeExample,
reverseGeocodeExample,
routingExample,
placesSearchExample,
autocompleteExample,
isolineExample,
batchExample,
errorHandlingExample,
clientClassExample
};