您的位置:首页 > 编程语言 > Java开发

JAVA通过物理地址获取经纬度

2012-11-19 11:12 686 查看
JAVA通过物理地址利用谷歌地图获取经纬度

public  String[] getCoordinate(String addr){
String address = null;
try {
address = java.net.URLEncoder.encode(addr,"UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
};
String output = "csv";
String key = "abc";
String url = String.format("http://maps.google.com/maps/geo?q=%s&output=%s&key=%s", address, output, key);
URL myURL = null;
URLConnection httpsConn = null;
//进行转码
try {
myURL = new URL(url);
} catch (MalformedURLException e) {

}
try {
httpsConn = (URLConnection) myURL.openConnection();
if (httpsConn != null) {
InputStreamReader insr = new InputStreamReader(
httpsConn.getInputStream(), "UTF-8");
BufferedReader br = new BufferedReader(insr);
String data = null;
if ((data = br.readLine()) != null) {
String[] retList = data.split(",");

return retList;
}
insr.close();
}
} catch (IOException e) {

}
return null;
}


返回一个String的数组。大家仔细打印看数组。数组的第2个为经度。第3个为纬度
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: