Skip to content

地理位置服务 API

基于 EdgeOne 边缘节点自动获取访客的地理位置信息,无需用户提供任何参数。

📡 接口信息

  • 接口路径: /geo
  • 请求方法: GET
  • 是否需要参数: 否
  • 响应格式: JSON

🚀 快速开始

基础调用

bash
curl https://www.160621.xyz/api/geo

JavaScript 调用

javascript
// 使用 fetch
fetch('https://www.160621.xyz/api/geo')
  .then(response => response.json())
  .then(result => {
    if (result.status === 'success') {
      console.log('地理位置信息:', result.data)
      console.log('国家:', result.data.geo.countryName)
      console.log('城市:', result.data.geo.cityName)
      console.log('IP:', result.data.clientIp)
    } else {
      console.error('获取失败:', result.message)
    }
  })

// 使用 async/await
async function getLocation() {
  try {
    const response = await fetch('https://www.160621.xyz/api/geo')
    const result = await response.json()

    if (result.status === 'success') {
      const location = result.data
      return location
    } else {
      throw new Error(result.message)
    }
  } catch (error) {
    console.error('获取地理位置失败:', error)
    throw error
  }
}

📋 响应字段

字段名类型说明示例
codenumber业务状态码200
statusstring状态标识"success"
messagestring提示信息"获取地理位置信息成功"
dataobject地理位置{ geo, uuid, clientIp }
timestampnumber服务器响应时间戳1736123456789

data 字段说明

字段名类型说明示例
geoobject地理位置信息{}
uuidstring用户唯一标识"8867920514367890123"
clientIpstring客户端 IP 地址"192.168.100.200"

📝 响应示例

成功响应示例

json
{
  "code": 200,
  "status": "success",
  "message": "获取地理位置信息成功",
  "data": {
    "geo": {
      "asn": 999999,
      "countryName": "United States",
      "countryCodeAlpha2": "US",
      "countryCodeAlpha3": "USA",
      "countryCodeNumeric": "840",
      "regionName": "California",
      "regionCode": "US-CA",
      "cityName": "Los Angeles",
      "latitude": 34.0522342,
      "longitude": -118.2436849,
      "cisp": "Verizon"
    },
    "uuid": "8867920514367890123",
    "clientIp": "192.168.100.200"
  },
  "timestamp": 1736123456789
}

错误响应示例

json
{
  "code": 500,
  "status": "error",
  "message": "获取地理位置信息失败",
  "data": {
    "details": "Internal server error"
  },
  "timestamp": 1736123456789
}

⚠️ 注意事项

1. 隐私保护

  • 该接口基于网络请求的 IP 地址获取位置
  • 无法获取精确的街道地址信息
  • 建议在隐私政策中说明位置信息的使用

2. 准确性限制

  • 位置信息基于 IP 地址数据库,可能存在偏差
  • 使用代理或 VPN 可能导致位置信息不准确
  • 某些网络环境下可能无法获取详细的位置信息

3. 缓存建议

javascript
// 位置信息缓存(1小时)
const locationCache = new Map()

async function getCachedLocation() {
  const cacheKey = 'user_location'
  const cached = locationCache.get(cacheKey)
  
  if (cached && Date.now() - cached.timestamp < 3600000) { // 1小时
    return cached.data
  }
  
  const location = await fetch('https://www.160621.xyz/api/geo').then(res => res.json())
  
  locationCache.set(cacheKey, {
    data: location,
    timestamp: Date.now()
  })
  
  return location
}

基于 EdgeOne 边缘计算平台构建