| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- import { getGisAccessToken, getGisBaseUrl, getGisDefaultCenter } from './gisLoader'
- /** GIS POI 接口成功状态码 */
- const POI_STATUS_OK = 2000
- /**
- * 解析 POI location 字段(文档格式:纬度,经度)为地图坐标 [经度, 纬度]
- */
- export function parsePoiLngLat(location) {
- if (!location || typeof location !== 'string') {
- return null
- }
- const parts = location.split(',').map((n) => Number(String(n).trim()))
- if (parts.length < 2 || !parts.every((n) => Number.isFinite(n))) {
- return null
- }
- const [lat, lng] = parts
- return [lng, lat]
- }
- function normalizePoiList(data) {
- if (data?.status !== POI_STATUS_OK || !Array.isArray(data?.body?.pois)) {
- return []
- }
- return data.body.pois
- .map((row) => row?.poi || row)
- .filter((poi) => poi && typeof poi === 'object')
- }
- /**
- * 周边搜索(GIS 接口文档 §2.2 /agis/search/v1/pois/region)
- * @param {{ center?: [number, number], radius?: number, pageNum?: number, pageSize?: number }} [options]
- */
- export async function searchNearbyPois(options = {}) {
- const base = getGisBaseUrl()
- const key = getGisAccessToken()
- if (!base || !key) {
- return []
- }
- const center = options.center || getGisDefaultCenter()
- const [longitude, latitude] = center
- const radius = options.radius ?? 5000
- const pageNum = options.pageNum ?? 1
- const pageSize = options.pageSize ?? 1000
- const params = new URLSearchParams({
- longitude: String(longitude),
- latitude: String(latitude),
- radius: String(radius),
- page_num: String(pageNum),
- page_size: String(pageSize),
- key
- })
- const url = `${base}/agis/search/v1/pois/region?${params.toString()}`
- const res = await fetch(url)
- if (!res.ok) {
- return []
- }
- return normalizePoiList(await res.json())
- }
- /*
- * 多边形区域搜索(GIS 接口文档 §2.3)— 暂不使用
- *
- * export const BA_QING_POI_POLYGON = [...]
- * export function formatPolygonParam(points) { ... }
- * export async function searchPolygonPois(options = {}) { ... }
- */
|