西藏巴青项目

gisPoiSearch.js 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { getGisAccessToken, getGisBaseUrl, getGisDefaultCenter } from './gisLoader'
  2. /** GIS POI 接口成功状态码 */
  3. const POI_STATUS_OK = 2000
  4. /**
  5. * 解析 POI location 字段(文档格式:纬度,经度)为地图坐标 [经度, 纬度]
  6. */
  7. export function parsePoiLngLat(location) {
  8. if (!location || typeof location !== 'string') {
  9. return null
  10. }
  11. const parts = location.split(',').map((n) => Number(String(n).trim()))
  12. if (parts.length < 2 || !parts.every((n) => Number.isFinite(n))) {
  13. return null
  14. }
  15. const [lat, lng] = parts
  16. return [lng, lat]
  17. }
  18. function normalizePoiList(data) {
  19. if (data?.status !== POI_STATUS_OK || !Array.isArray(data?.body?.pois)) {
  20. return []
  21. }
  22. return data.body.pois
  23. .map((row) => row?.poi || row)
  24. .filter((poi) => poi && typeof poi === 'object')
  25. }
  26. /**
  27. * 周边搜索(GIS 接口文档 §2.2 /agis/search/v1/pois/region)
  28. * @param {{ center?: [number, number], radius?: number, pageNum?: number, pageSize?: number }} [options]
  29. */
  30. export async function searchNearbyPois(options = {}) {
  31. const base = getGisBaseUrl()
  32. const key = getGisAccessToken()
  33. if (!base || !key) {
  34. return []
  35. }
  36. const center = options.center || getGisDefaultCenter()
  37. const [longitude, latitude] = center
  38. const radius = options.radius ?? 5000
  39. const pageNum = options.pageNum ?? 1
  40. const pageSize = options.pageSize ?? 1000
  41. const params = new URLSearchParams({
  42. longitude: String(longitude),
  43. latitude: String(latitude),
  44. radius: String(radius),
  45. page_num: String(pageNum),
  46. page_size: String(pageSize),
  47. key
  48. })
  49. const url = `${base}/agis/search/v1/pois/region?${params.toString()}`
  50. const res = await fetch(url)
  51. if (!res.ok) {
  52. return []
  53. }
  54. return normalizePoiList(await res.json())
  55. }
  56. /*
  57. * 多边形区域搜索(GIS 接口文档 §2.3)— 暂不使用
  58. *
  59. * export const BA_QING_POI_POLYGON = [...]
  60. * export function formatPolygonParam(points) { ... }
  61. * export async function searchPolygonPois(options = {}) { ... }
  62. */