zoukankan      html  css  js  c++  java
  • 通过百度获取IP地址对应的经纬度

    /**
    * 获取指定IP对应的经纬度(为空返回当前机器经纬度)

    * @param ip
    * @return
    */
    public static String[] getIPXY(String ip) {

    String ak = "百度申请的Key";
    if (null == ip) {
    ip = "";
    }

    try {

    URL url = new URL("http://api.map.baidu.com/location/ip?ak=" + ak
    + "&ip=" + ip + "&coor=bd09ll");
    InputStream inputStream = url.openStream();
    InputStreamReader inputReader = new InputStreamReader(inputStream);
    BufferedReader reader = new BufferedReader(inputReader);
    StringBuffer sb = new StringBuffer();
    String str;
    do {
    str = reader.readLine();
    sb.append(str);
    } while (null != str);


    str = sb.toString();
    if (null == str || str.isEmpty()) {
    return null;
    }


    // 获取坐标位子
    int index = str.indexOf("point");
    int end = str.indexOf("}}", index);


    if (index == -1 || end == -1) {
    return null;
    }


    str = str.substring(index - 1, end + 1);
    if (null == str || str.isEmpty()) {
    return null;
    }


    String[] ss = str.split(":");
    if (ss.length != 4) {
    return null;
    }


    String x = ss[2].split(",")[0];
    String y = ss[3];


    x = x.substring(x.indexOf(""") + 1, x.indexOf(""", 1));
    y = x.substring(y.indexOf(""") + 1, y.indexOf(""", 1));


    return new String[] { x, y };


    } catch (MalformedURLException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }


    return null;
    }

  • 相关阅读:
    Bootstrap3入门
    Pi
    比Redis更快:Berkeley DB面面观
    搞定KMP匹配算法
    elasticsearch文档-analysis
    21本计算机数学相关的免费电子书
    [Android开发常见问题-12] Android开发中debug.keystore如何使用。
    (Java实现) 组合的输出
    (Java实现) 自然数的拆分
    (Java实现) 自然数的拆分
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3278377.html
Copyright © 2011-2022 走看看