根据IP地址获取地理位置 您所在的位置:网站首页 电脑如何查询ip地址的位置信息 根据IP地址获取地理位置

根据IP地址获取地理位置

2023-07-01 09:27| 来源: 网络整理| 查看: 265

之前做了一个天气预报,里面就用到了根据IP自动定位技术,今天就给大家分享一波:根据IP地址自动定位。

想要实现根据IP地址定位,你要做的事有如下两点:

第一:获取本机的IP地址

注意:这里指的是公网的IP地址,而不是局域网的IP地址,之前因为我是在学校,所以获取的本机IP是经过校园再分配的地址,不是公网的IP地址,导致定位失败。所以我们需要用到IP查询接口,之前用的是:http://ip.chinaz.com这个网址的接口,现在好像不能用了,于是又换了一个IP查询接口:http://whois.pconline.com.cn/。

我们用URL资源定位符进行资源的拉取,然后利用正则表达式匹配我们想要的内容,这样便可以拿到我们本机的公网IP地址。

第二步:根据IP获取地理位置

当我们拿到本机的公网IP后,就可以使用接口来查询对应IP的地理位置了。我调用的是百度的ip定位api服务,详情参见:

http://api.map.baidu.com/lbsapi/cloud/ip-location-api.htm

下面是测试代码:

package com.yc.weather; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.nio.charset.Charset; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.json.JSONException; import org.json.JSONObject; /** * 根据ip获取地址 * * @author jam * */ public class Address { /** * 读取所有内容 * * @param rd * @return * @throws IOException */ private static String readAll(Reader rd) throws IOException { StringBuilder sb = new StringBuilder(); int cp; while ((cp = rd.read()) != -1) { sb.append((char) cp); } return sb.toString(); } /** * 拉取网页所有内容,并转化为Json格式 * * @param url * @return * @throws IOException * @throws JSONException */ public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException { InputStream is = new URL(url).openStream(); try { BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); String jsonText = readAll(rd); JSONObject json = new JSONObject(jsonText); return json; } finally { is.close(); } } public String getAddress() { String ip = ""; // 这个网址似乎不能了用了 // String chinaz = "http://ip.chinaz.com"; // 改用了太平洋的一个网址 String chinaz = "http://whois.pconline.com.cn/"; StringBuilder inputLine = new StringBuilder(); String read = ""; URL url = null; HttpURLConnection urlConnection = null; BufferedReader in = null; try { url = new URL(chinaz); urlConnection = (HttpURLConnection) url.openConnection(); // 如有乱码的,请修改相应的编码集,这里是 gbk in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "gbk")); while ((read = in.readLine()) != null) { inputLine.append(read + "\r\n"); } // System.out.println(inputLine.toString()); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } // 这个是之前的正则表达式, // Pattern p = Pattern.compile("\\(.*?)\\"); // 通过正则表达式匹配我们想要的内容,根据拉取的网页内容不同,正则表达式作相应的改变 Pattern p = Pattern.compile("显示IP地址为(.*?)的位置信息"); Matcher m = p.matcher(inputLine.toString()); if (m.find()) { String ipstr = m.group(0); // 这里根据具体情况,来截取想要的内容 ip = ipstr.substring(ipstr.indexOf("为") + 2, ipstr.indexOf("的") - 1); System.out.println(ip); } JSONObject json = null; String city = null; try { // 这里调用百度的ip定位api服务 详见 http://api.map.baidu.com/lbsapi/cloud/ip-location-api.htm json = readJsonFromUrl("http://api.map.baidu.com/location/ip?ak=F454f8a5efe5e577997931cc01de3974&ip=" + ip); System.out.println(json); city = (((JSONObject) ((JSONObject) json.get("content")).get("address_detail")).get("city")).toString(); } catch (Exception e) { e.printStackTrace(); } return city; } public static void main(String[] args) throws IOException, JSONException { Address Addr = new Address(); String addr = Addr.getAddress(); System.out.println(addr); } }

上面这个类只需要导入一个json的jar包就可以,在maven项目的pom.xml文件中添加如下依赖:

org.json json 20090211

如果大家还不会使用maven,可以参考我的文章《maven入门笔记》,如果实在不想用maven的,可以去网上找json这个jar包,很容易找到的。

运行效果:

其实查询IP地址的接口和网站很多,大家随便百度都有,如果项目要上线并且长期使用的话,就需要找一家正规的公司进行合作了。

本来这个百度的IP定位API可以定位到街道甚至门牌号的,可能是因为我用的手机给电脑开的热点的原因,导致只能定位到市级,并且,这里我只取到了市级的位置,本来这个类我是用来查询天气时获取城市的一个类,只需要精确到市级就可以了,大家随便改改代码就可以查到自己想要的信息了,这里主要用到了json数据的解析。

OK,根据IP地址定位的经验分享到此告一段落,有时间我再给大家分享一下如何根据地理位置来查询天气。

文章属原创,如需引用,请注明出处,谢谢。

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有