互联网有很多接口可以实现通过ip查询到具体的位置,如下:

通过淘宝IP地址库获取IP位置

1. 请求接口(GET):http://ip.taobao.com/service/getIpInfo.php?ip=[ip地址字串]
2. 响应信息:(json格式的)国家 、省(自治区或直辖市)、市(县)、运营商
3. 返回数据格式:
{"code":0,"data":{"ip":"210.75.225.254","country":"\u4e2d\u56fd","area":"\u534e\u5317",
"region":"\u5317\u4eac\u5e02","city":"\u5317\u4eac\u5e02","county":"","isp":"\u7535\u4fe1",
"country_id":"86","area_id":"100000","region_id":"110000","city_id":"110000",
"county_id":"-1","isp_id":"100017"}}
其中code的值的含义为,0:成功,1:失败。 
新浪的接口 :http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=218.192.3.42
返回值
var remote_ip_info = {"ret":1,"start":"218.192.0.0","end":"218.192.7.255","country":"\u4e2d\u56fd","province":"\u5e7f\u4e1c","city":"\u5e7f\u5dde","district":"","isp":"\u6559\u80b2\u7f51","type":"\u5b66\u6821","desc":"\u5e7f\u5dde\u5927\u5b66\u7eba\u7ec7\u670d\u88c5\u5b66\u9662"};
通过jqry 获取相应的数据
$.getScript('数据接口',function(){
//新浪:remote_ip_info.country
}) 
腾讯IP分享计划的地址获取IP所在地:
http://ip.qq.com/cgi-bin/searchip?searchip1=ip

用java调用淘宝ip查询接口查询地域的一个java实例:

[java] view plain copy
  1. import java.io.BufferedReader;
  2. import java.io.DataOutputStream;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.io.UnsupportedEncodingException;
  6. import java.net.HttpURLConnection;
  7. import java.net.URL;
  8. /**
  9. *  根据IP地址获取详细的地域信息
  10. *  @project:personGocheck
  11. *  @class:AddressUtils.java
  12. *  @author:heguanhua E-mail:37809893@qq.com
  13. *  @date:Nov 14, 2012 6:38:25 PM
  14. */
  15. public class AddressUtils {
  16. /**
  17. *
  18. * @param content
  19. *            请求的参数 格式为:name=xxx&pwd=xxx
  20. * @param encoding
  21. *            服务器端请求编码。如GBK,UTF-8等
  22. * @return
  23. * @throws UnsupportedEncodingException
  24. */
  25. public String getAddresses(String content, String encodingString)
  26. throws UnsupportedEncodingException {
  27. // 这里调用pconline的接口
  28. String urlStr = "http://ip.taobao.com/service/getIpInfo.php";
  29. // 从http://whois.pconline.com.cn取得IP所在的省市区信息
  30. String returnStr = this.getResult(urlStr, content, encodingString);
  31. if (returnStr != null) {
  32. // 处理返回的省市区信息
  33. System.out.println(returnStr);
  34. String[] temp = returnStr.split(",");
  35. if(temp.length<3){
  36. return "0";//无效IP,局域网测试
  37. }
  38. String region = (temp[5].split(":"))[1].replaceAll("\"", "");
  39. region = decodeUnicode(region);// 省份
  40. String country = "";
  41. String area = "";
  42. // String region = "";
  43. String city = "";
  44. String county = "";
  45. String isp = "";
  46. for (int i = 0; i < temp.length; i++) {
  47. switch (i) {
  48. case 1:
  49. country = (temp[i].split(":"))[2].replaceAll("\"", "");
  50. country = decodeUnicode(country);// 国家
  51. break;
  52. case 3:
  53. area = (temp[i].split(":"))[1].replaceAll("\"", "");
  54. area = decodeUnicode(area);// 地区
  55. break;
  56. case 5:
  57. region = (temp[i].split(":"))[1].replaceAll("\"", "");
  58. region = decodeUnicode(region);// 省份
  59. break;
  60. case 7:
  61. city = (temp[i].split(":"))[1].replaceAll("\"", "");
  62. city = decodeUnicode(city);// 市区
  63. break;
  64. case 9:
  65. county = (temp[i].split(":"))[1].replaceAll("\"", "");
  66. county = decodeUnicode(county);// 地区
  67. break;
  68. case 11:
  69. isp = (temp[i].split(":"))[1].replaceAll("\"", "");
  70. isp = decodeUnicode(isp); // ISP公司
  71. break;
  72. }
  73. }
  74. System.out.println(country+"="+area+"="+region+"="+city+"="+county+"="+isp);
  75. return region;
  76. }
  77. return null;
  78. }
  79. /**
  80. * @param urlStr
  81. *            请求的地址
  82. * @param content
  83. *            请求的参数 格式为:name=xxx&pwd=xxx
  84. * @param encoding
  85. *            服务器端请求编码。如GBK,UTF-8等
  86. * @return
  87. */
  88. private String getResult(String urlStr, String content, String encoding) {
  89. URL url = null;
  90. HttpURLConnection connection = null;
  91. try {
  92. url = new URL(urlStr);
  93. connection = (HttpURLConnection) url.openConnection();// 新建连接实例
  94. connection.setConnectTimeout(2000);// 设置连接超时时间,单位毫秒
  95. connection.setReadTimeout(2000);// 设置读取数据超时时间,单位毫秒
  96. connection.setDoOutput(true);// 是否打开输出流 true|false
  97. connection.setDoInput(true);// 是否打开输入流true|false
  98. connection.setRequestMethod("POST");// 提交方法POST|GET
  99. connection.setUseCaches(false);// 是否缓存true|false
  100. connection.connect();// 打开连接端口
  101. DataOutputStream out = new DataOutputStream(connection
  102. .getOutputStream());// 打开输出流往对端服务器写数据
  103. out.writeBytes(content);// 写数据,也就是提交你的表单 name=xxx&pwd=xxx
  104. out.flush();// 刷新
  105. out.close();// 关闭输出流
  106. BufferedReader reader = new BufferedReader(new InputStreamReader(
  107. connection.getInputStream(), encoding));// 往对端写完数据对端服务器返回数据
  108. // ,以BufferedReader流来读取
  109. StringBuffer buffer = new StringBuffer();
  110. String line = "";
  111. while ((line = reader.readLine()) != null) {
  112. buffer.append(line);
  113. }
  114. reader.close();
  115. return buffer.toString();
  116. } catch (IOException e) {
  117. e.printStackTrace();
  118. } finally {
  119. if (connection != null) {
  120. connection.disconnect();// 关闭连接
  121. }
  122. }
  123. return null;
  124. }
  125. /**
  126. * unicode 转换成 中文
  127. *
  128. * @author fanhui 2007-3-15
  129. * @param theString
  130. * @return
  131. */
  132. public static String decodeUnicode(String theString) {
  133. char aChar;
  134. int len = theString.length();
  135. StringBuffer outBuffer = new StringBuffer(len);
  136. for (int x = 0; x < len;) {
  137. aChar = theString.charAt(x++);
  138. if (aChar == '\\') {
  139. aChar = theString.charAt(x++);
  140. if (aChar == 'u') {
  141. int value = 0;
  142. for (int i = 0; i < 4; i++) {
  143. aChar = theString.charAt(x++);
  144. switch (aChar) {
  145. case '0':
  146. case '1':
  147. case '2':
  148. case '3':
  149. case '4':
  150. case '5':
  151. case '6':
  152. case '7':
  153. case '8':
  154. case '9':
  155. value = (value << 4) + aChar - '0';
  156. break;
  157. case 'a':
  158. case 'b':
  159. case 'c':
  160. case 'd':
  161. case 'e':
  162. case 'f':
  163. value = (value << 4) + 10 + aChar - 'a';
  164. break;
  165. case 'A':
  166. case 'B':
  167. case 'C':
  168. case 'D':
  169. case 'E':
  170. case 'F':
  171. value = (value << 4) + 10 + aChar - 'A';
  172. break;
  173. default:
  174. throw new IllegalArgumentException(
  175. "Malformed      encoding.");
  176. }
  177. }
  178. outBuffer.append((char) value);
  179. } else {
  180. if (aChar == 't') {
  181. aChar = '\t';
  182. } else if (aChar == 'r') {
  183. aChar = '\r';
  184. } else if (aChar == 'n') {
  185. aChar = '\n';
  186. } else if (aChar == 'f') {
  187. aChar = '\f';
  188. }
  189. outBuffer.append(aChar);
  190. }
  191. } else {
  192. outBuffer.append(aChar);
  193. }
  194. }
  195. return outBuffer.toString();
  196. }
  197. // 测试
  198. public static void main(String[] args) {
  199. AddressUtils addressUtils = new AddressUtils();
  200. // 测试ip 219.136.134.157 中国=华南=广东省=广州市=越秀区=电信
  201. String ip = "125.70.11.136";
  202. String address = "";
  203. try {
  204. address = addressUtils.getAddresses("ip="+ip, "utf-8");
  205. } catch (UnsupportedEncodingException e) {
  206. // TODO Auto-generated catch block
  207. e.printStackTrace();
  208. }
  209. System.out.println(address);
  210. // 输出结果为:广东省,广州市,越秀区
  211. }
  212. }

ip获取所在城市名称接口-JAVA相关推荐

  1. C# 解析百度天气数据,Rss解析百度新闻以及根据IP获取所在城市

    接口地址:http://api.map.baidu.com/telematics/v3/weather?location=上海&output=json&ak=hXWAgbsCC9UTk ...

  2. 根据IP获取具体城市名称及经纬度坐标

    用到两款工具, geoip-api 这个可以根据IP获取城市以及经纬度 但是获取的城市是拼音不是汉字 导致山西和陕西的拼音相同 无法分辨,所以引入第二款工具ip2region 可以根据ip获取到具体的 ...

  3. 根据ip获取所在城市 php,PHP:根据IP地址获取所在城市

    欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 if ( $useripFlag == chr( 2 ) ) { $AddrSeek = fread( $fd, 3 ): ...

  4. js通过IP地址获取所在城市

    话不多说,直接贴代码! 将这段代码运行一下就OK了: <html xmlns="http://www.w3.org/1999/xhtml"> <head runa ...

  5. 获取地区html代码,根据ip地址获取所在城市信息(省市县)代码

    [实例简介] [实例截图] [核心代码] 获取ip地址所在城市 var showIP = function(ip, remote_ip_info ){ if(remote_ip_info['ret'] ...

  6. H5获取用户所在城市 网页获取用户城市名称

    获取用户城市名称,这里我是使用的百度地图JSAPI 2.0 文档链接 实现步骤: 1.在index.html中引用百度地图的js文件,如下:(需要使用自己的ak,获取方式:点击去官网申请ak) < ...

  7. php ip 城市,php如何根据IP地址获取所在城市

    本文主要和大家分享php如何根据IP地址获取所在城市,本文主要以代码的形式,希望能帮助到大家.<?php header('Content-Type:text/html;Charset=utf-8 ...

  8. Java 通过 ip 获取所属城市信息

    文章目录 1 摘要 2 核心 Maven 依赖 3 离线数据 db 保存 4 核心代码 5 测试 6 推荐参考资料 1 摘要 在实际项目中可能会用到统计用户地理位置的需求,这时可以通过客户端的 ip ...

  9. js获取所在城市及IP

    最简单的方式获取所在城市 1.引入js: 直接在浏览器访问以下试试: http://pv.sohu.com/cityjson?ie=utf-8 2.取值: var ctiyName=returnCit ...

  10. uniapp|微信小程序获取当前城市名称--逆地址解析

    六年代码两茫茫,不思量,自难忘 6年资深前端主管一枚,只分享技术干货,项目实战经验 关注博主不迷路~ 文章目录 问题 解决步骤 看文档 逆地址解析 1. 创建应用 2. uniapp配置 3. 代码部 ...

最新文章

  1. windows下的虚拟机中的ubuntu系统如何连接无线网(wifi)
  2. php 三个点 三角形面积,知道三角形三个顶点坐标,求面积,我觉得我的没错,但未通过,麻烦大佬帮忙看下...
  3. 某合资源网4.5主题 完美无错版 emlog模板
  4. react-native ListView 封装 实现 下拉刷新/上拉加载更多
  5. 51 Nod 阶乘后面0的数量
  6. Git - 操作指南
  7. 无传感FOC控制中的转子位置和速度确定方法一
  8. C++ 变量、常量、符号常量
  9. 15. Window clearTimeout() 方法
  10. 【spring】spring动态代理和Spring_AOP
  11. 2021 年“认证杯”数学中国数学建模网络挑战赛 B题
  12. zzulioj 1819: 加加加!(油) (打表)
  13. 选课系统 mysql_「选课系统」我的数据库大作业——学生选课系统实现(准备) - seo实验室...
  14. HC05蓝牙模块特点及使用介绍
  15. 【第二章】NB-IoT模组BC95入网测试
  16. “嗲”上海文化,女人需要学会的4种气质
  17. 元宇宙3D设计系统【构思与展望】
  18. [论文学习] - 2014ECCV - TCDCN
  19. 一次真实的应急响应案例(Centos)——暴力破解、替换ps命令、留多个后门——事件复现(含靶场环境)
  20. 服务器操作系统密码忘记,服务器操作系统密码忘记了

热门文章

  1. 你会如何选型电容?关于电容的ESR?如何理解电容的阻抗-频率曲线?
  2. 支付宝小程序使用阿里图标
  3. 批量转换excel文件格式
  4. TiDB的设计哲学——Make It Work! Make It Right! Make It Fast!
  5. 2013.11.18周例会小结
  6. eclipse工具按键翻译
  7. openmodelica安装
  8. 1万美元FS-ISAC网络安全奖学金
  9. 强大的网页数据库管理工具Adminer
  10. php 二级导航,导航下面的二级导航,显示和隐藏,