通过ip可获取ip所在的区域信息,有很多公司提供第三方接口。我自己用了高德地图的api。
第一次玩这个的同学,可能不熟悉,所以特此说明3点:
①完成调用,需发起http请求。java调用方式参考:https://blog.csdn.net/S_L__/article/details/104931630
②以下链接只是示范,更具体的使用请参考其官网api。
③所谓的免费并不是全部免费,不同公司每月的免费条数不同,具体参考参考其官网api。
网上收集的几个可用的免费api:
api地址:https://lbs.amap.com/api/webservice/guide/api/ipconfig
使用步骤:①注册申请key.
② 调用http请求:https://restapi.amap.com/v3/ip?ip=114.247.50.2&output=json&key=<用户的key>
③返回值:
{
- "status" :"1",
- "info" :"OK",
- "infocode" :"10000",
- "province" :"北京市",
- "city" :"北京市",
- "adcode" :"110000",
- "rectangle" :"116.0119343,39.66127144;116.7829835,40.2164962"
}
接口参数与形式比较多,可以打开网站查看
https://whois.pconline.com.cn/ipJson.jsp
返回结果
if (window.IPCallBack) { IPCallBack({ "ip": "58.214.246.10", "pro": "江苏省", "proCode": "320000", "city": "无锡市", "cityCode": "320200", "region": "", "regionCode": "0", "addr": "江苏省无锡市 电信", "regionNames": "", "err": "" }); }
http://pv.sohu.com/cityjson ,编码默认GBK
http://pv.sohu.com/cityjson?ie=utf-8 设置编码
返回结果:
var returnCitySN = {"cip": "110.184.56.21", "cid": "510100", "cname": "四川省成都市"};
http://ip.ws.126.net/ipquery
返回结果:
var lo="四川省", lc="成都市";
var localAddress={city:"成都市", province:"四川省"}
5.淘宝(该方式还未试过)
http://ip.taobao.com/service/getIpInfo.php?ip=114.114.114.114
返回结果
{ "code": 0, "data": { "ip": "114.114.114.114", "country": "中国", "area": "", "region": "江苏", "city": "南京", "county": "XX", "isp": "XX", "country_id": "CN", "area_id": "", "region_id": "320000", "city_id": "320100", "county_id": "xx", "isp_id": "xx" } }
参考:https://www.cnblogs.com/appleat/p/9269898.html