map.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. """地图服务API路由"""
  2. from fastapi import APIRouter, HTTPException, Query
  3. from typing import Optional
  4. from ...models.schemas import (
  5. POISearchRequest,
  6. POISearchResponse,
  7. RouteRequest,
  8. RouteResponse,
  9. WeatherResponse
  10. )
  11. from ...services.amap_service import get_amap_service
  12. router = APIRouter(prefix="/map", tags=["地图服务"])
  13. @router.get(
  14. "/poi",
  15. response_model=POISearchResponse,
  16. summary="搜索POI",
  17. description="根据关键词搜索POI(兴趣点)"
  18. )
  19. async def search_poi(
  20. keywords: str = Query(..., description="搜索关键词", example="故宫"),
  21. city: str = Query(..., description="城市", example="北京"),
  22. citylimit: bool = Query(True, description="是否限制在城市范围内")
  23. ):
  24. """
  25. 搜索POI
  26. Args:
  27. keywords: 搜索关键词
  28. city: 城市
  29. citylimit: 是否限制在城市范围内
  30. Returns:
  31. POI搜索结果
  32. """
  33. try:
  34. # 获取服务实例
  35. service = get_amap_service()
  36. # 搜索POI
  37. pois = service.search_poi(keywords, city, citylimit)
  38. return POISearchResponse(
  39. success=True,
  40. message="POI搜索成功",
  41. data=pois
  42. )
  43. except Exception as e:
  44. print(f"❌ POI搜索失败: {str(e)}")
  45. raise HTTPException(
  46. status_code=500,
  47. detail=f"POI搜索失败: {str(e)}"
  48. )
  49. @router.get(
  50. "/weather",
  51. response_model=WeatherResponse,
  52. summary="查询天气",
  53. description="查询指定城市的天气信息"
  54. )
  55. async def get_weather(
  56. city: str = Query(..., description="城市名称", example="北京")
  57. ):
  58. """
  59. 查询天气
  60. Args:
  61. city: 城市名称
  62. Returns:
  63. 天气信息
  64. """
  65. try:
  66. # 获取服务实例
  67. service = get_amap_service()
  68. # 查询天气
  69. weather_info = service.get_weather(city)
  70. return WeatherResponse(
  71. success=True,
  72. message="天气查询成功",
  73. data=weather_info
  74. )
  75. except Exception as e:
  76. print(f"❌ 天气查询失败: {str(e)}")
  77. raise HTTPException(
  78. status_code=500,
  79. detail=f"天气查询失败: {str(e)}"
  80. )
  81. @router.post(
  82. "/route",
  83. response_model=RouteResponse,
  84. summary="规划路线",
  85. description="规划两点之间的路线"
  86. )
  87. async def plan_route(request: RouteRequest):
  88. """
  89. 规划路线
  90. Args:
  91. request: 路线规划请求
  92. Returns:
  93. 路线信息
  94. """
  95. try:
  96. # 获取服务实例
  97. service = get_amap_service()
  98. # 规划路线
  99. route_info = service.plan_route(
  100. origin_address=request.origin_address,
  101. destination_address=request.destination_address,
  102. origin_city=request.origin_city,
  103. destination_city=request.destination_city,
  104. route_type=request.route_type
  105. )
  106. return RouteResponse(
  107. success=True,
  108. message="路线规划成功",
  109. data=route_info
  110. )
  111. except Exception as e:
  112. print(f"❌ 路线规划失败: {str(e)}")
  113. raise HTTPException(
  114. status_code=500,
  115. detail=f"路线规划失败: {str(e)}"
  116. )
  117. @router.get(
  118. "/health",
  119. summary="健康检查",
  120. description="检查地图服务是否正常"
  121. )
  122. async def health_check():
  123. """健康检查"""
  124. try:
  125. # 检查服务是否可用
  126. service = get_amap_service()
  127. return {
  128. "status": "healthy",
  129. "service": "map-service",
  130. "mcp_tools_count": len(service.mcp_tool._available_tools)
  131. }
  132. except Exception as e:
  133. raise HTTPException(
  134. status_code=503,
  135. detail=f"服务不可用: {str(e)}"
  136. )