首页 > 其他 > 详细

通过百度获取IP地址对应的经纬度

时间:2017-01-14 17:12:23      阅读:316      评论:0      收藏:0      [点我收藏+]
/**
* 获取指定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 = y.substring(y.indexOf("\"") + 1, y.indexOf("\"", 1));


return new String[] { x, y };


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


return null;
}
 
 

通过百度获取IP地址对应的经纬度

原文:http://www.cnblogs.com/love540376/p/6285628.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!