本文最后更新于:2024年2月5日 下午

IP 地址会与真实位置有一个大概的对应关系,形成 IP - 地点 的映射,本文记录相应工具和使用方法。

简介

  • IP - 地址 的映射本质上只要建立一个字典就好了,但是数据量还是很大的,有专门的机构已经做了相关的工作,我们可以使用这些 API 进行查表获取信息
  • 已知的工具:
    • ip-api
    • 百度地图 API
    • 腾讯地图 API
    • IP 数据云 API

IP-API

  • IP-API 有公用的 API 可以免费提供 IP 全球定位查询功能

  • 使用方法

  • 通过访问如下地址可以获取 IP 对应的地址信息

1
http://ip-api.com/json/{ip}?fields=61439&lang=zh-CN
  • 示例:
1
2
3
4
http://ip-api.com/json/101.88.133.75?fields=61439&lang=zh-CN

-->
{"status":"success","country":"中国","countryCode":"CN","region":"SH","regionName":"上海","city":"上海","zip":"","lat":31.2222,"lon":121.4581,"timezone":"Asia/Shanghai","isp":"China Telecom (Group)","org":"Chinanet SH","as":"AS4812 China Telecom (Group)","query":"101.88.133.75"}

不过该 API 速度很慢

百度 Web 服务 API

  • 说明文档:https://lbs.baidu.com/index.php?title=webapi/ip-api

  • 支持 IPv6 IPv4 地址

  • 不支持海外场景的地址查询

  • 申请百度 Web 服务 API key

  • 请求URL

    1
    https://api.map.baidu.com/location/ip?ak=您的AK&ip=您的IP&coor=bd09ll //GET请求 
  • 请求参数:

    参数名称 参数含义 类型 备注
    ip 用户上网的IP地址,请求中如果不出现或为空,会针对发来请求的IP进行定位。 如您需要通过IPv6来获取位置信息,请提交工单申请。 string 可选
    ak 开发者密钥,可在API控制台申请获得 string 必填
    sn 若用户所用AK的校验方式为SN校验时该参数必填。其他AK校验方式的可不填写 string 可选
    coor 设置返回位置信息中,经纬度的坐标类型,分别如下: coor不出现、或为空:百度墨卡托坐标,即百度米制坐标 coor = bd09ll:百度经纬度坐标,在国测局坐标基础之上二次加密而来 coor = gcj02:国测局02坐标,在原始GPS坐标基础上,按照国家测绘行业统一要求,加密后的坐标 注意:百度地图的坐标类型为bd09ll,如果结合百度地图使用,请注意坐标选择 string 可选
  • 返回结果(JSON格式):

    结果字段 字段含义 类型
    address 详细地址信息 string
    content address 简要地址信息 string
    address_detail city 城市 string
    city_code 百度城市代码 string
    province 省份 string
    point x 当前城市中心点经度 string
    y 当前城市中心点纬度 string
    status 结果状态返回码,请参考本章节后面的介绍 string
  • 示例:

    1
    2
    3
    https://api.map.baidu.com/location/ip?ak=LrsHHZ2OArsUFeqIG9hc6vf8pHe57ZQD&ip=101.88.133.75&coor=bd09ll

    {"address":"CN|\u4e0a\u6d77\u5e02|\u4e0a\u6d77\u5e02|None|None|99|99","content":{"address":"\u4e0a\u6d77\u5e02","address_detail":{"adcode":"310100","city":"\u4e0a\u6d77\u5e02","city_code":289,"district":"","province":"\u4e0a\u6d77\u5e02","street":"","street_number":""},"point":{"x":"121.48053886","y":"31.23592904"}},"status":0}

    文字乱了码了,但是可以看到经纬度是对的。

腾讯 API

​ 该请求为GET请求

https://apis.map.qq.com/ws/location/v1/ip

  • 请求参数
参数 必填 说明 示例
key 开发密钥(Key) key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-*****
ip IP地址,缺省时会使用请求端的IP ip=111.206.145.41
output 返回格式:支持JSON/JSONP,默认JSON output=json
callback JSONP方式回调函数 callback=function1
  • 响应结果
名称 类型 必填 说明
status number 状态码,0为正常,其它为异常,详细请参阅状态码说明
message string 对status的描述
result object IP定位结果
ip string 用于定位的IP地址
location object 定位坐标。**注:**IP定位服务精确到市级,该位置为IP地址所属的行政区划政府坐标。
lat number 纬度
lng number 经度
ad_info object 定位行政区划信息
nation string 国家
province string
city string
district string
adcode number 行政区划代码
  • 调用示例

// GET请求,注意参数值要进行URL编码
https://apis.map.qq.com/ws/location/v1/ip?ip=111.206.145.41&key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//响应示例:
{
"status": 0,
"message": "Success",
"result": {
"ip": "111.206.145.41",
"location": {
"lat": 39.90469,
"lng": 116.40717
},
"ad_info": {
"nation": "中国",
"province": "北京市",
"city": "北京市",
"district": "",
"adcode": 110000
}
}
}

IP 数据云

  • IP 数据云支持全球IP 定位,速度快,免费 1000次/日 已经涨价用不起了

  • 官网地址:https://www.ipdatacloud.com/

  • API 使用方法

    1
    https://api.ipdatacloud.com/v2/query?ip={待查询 IP}&key={用户 Key}
  • 调用示例:

    1
    2
    3
    4
    https://api.ipdatacloud.com/v2/query?ip=101.88.133.75&key=xxxxxxxxxxxx

    -->
    {"code":200,"data":{"area_code":"310100","city":"上海","city_code":"021","continent":"亚洲","country":"中国","country_code":"CN","country_english":"","district":"","elevation":"15","ip":"101.88.133.75","isp":"电信","latitude":"31.231706","local_time":"2023-01-17 22:56","longitude":"121.472644","province":"上海","street":"","version":"V4","weather_station":"CHXX0116","zip_code":"200000"},"msg":"success"}
  • 速度很快

IP2location.io

  • 支持全球 IP 定位,速度一般,每月免费 30000 次
  • 官网地址:https://www.ip2location.io/
  • 需要申请账号,开通免费套餐即可使用

  • API 使用方法:
1
https://api.ip2location.io/?key=<your API KEY>&ip=<IP String>
  • 返回示例:
1
{"ip":"159.223.86.165","country_code":"SG","country_name":"Singapore","region_name":"Singapore","city_name":"Singapore","latitude":1.289987,"longitude":103.850281,"zip_code":"178958","time_zone":"+08:00","asn":"14061","as":"DigitalOcean LLC","is_proxy":false}

总结

工具种类 支持IPv4 支持IPv6 费用 支持境内场景 支持境外场景 响应速度
IP-API × 免费
百度 API 免费 ×
腾讯 API 免费 ×
IP 数据云 需要额外开通 每天免费1000次 付费
IP2location.io IP 每月免费 30000 次 一般
  • 综合来看,对于访问量不那么大的网站,IP2location.io 综合来看还是比较优秀的了。

参考资料



文章链接:
https://www.zywvvd.com/notes/tools/ip-locate/ip-locate/


“觉得不错的话,给点打赏吧 ୧(๑•̀⌄•́๑)૭”

微信二维码

微信支付

支付宝二维码

支付宝支付

根据 IP 地址定位经纬度
https://www.zywvvd.com/notes/tools/ip-locate/ip-locate/
作者
Yiwei Zhang
发布于
2023年1月17日
许可协议