packagedao;
/*
思路就是先根据名称确定经纬度再利用经纬度查询详细的地址
*/importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.UnsupportedEncodingException;importjava.net.MalformedURLException;importjava.net.URL;importjava.net.URLConnection;importcom.alibaba.fastjson.JSONObject;public classTests {/***@paramaddr
* 查询的地址
*@return*@throwsIOException*/
public String[] getCoordinate(String addr) throwsIOException {
String lng= null;//经度
String lat = null;//纬度
String address = null;try{
address= java.net.URLEncoder.encode(addr, "UTF-8");
}catch(UnsupportedEncodingException e1) {
e1.printStackTrace();
}//System.out.println(address);
String url = "http://api.map.baidu.com/geocoding/v3/?output=json&ak=你的ak值&coordtype=wgs84ll&address="+address;
URL myURL= null;
URLConnection httpsConn= null;try{
myURL= newURL(url);
}catch(MalformedURLException e) {
e.printStackTrace();
}
InputStreamReader insr= null;
BufferedReader br= null;try{
httpsConn=(URLConnection) myURL.openConnection();if (httpsConn != null) {
insr= new InputStreamReader( httpsConn.getInputStream(), "UTF-8");
br= newBufferedReader(insr);
String data= null;while((data= br.readLine())!=null){//System.out.println(data);
/** 在这里设置了一个条件判断,根据百度第图的返回值表当输入地址返回值的状态为‘0’时说明地址查询发生了错误
* 此时得到的经纬度也就是空了
* 所以当结果不为0时就退出返回空值,在循环调用的时候就判断其是否为空,决定如何进行下一步操作*/
if (data.charAt(10) != '0') {//System.out.println(data.charAt(10));
return null;
}
JSONObject json=JSONObject.parseObject(data);
lng= json.getJSONObject("result").getJSONObject("location").getString("lng");
lat= json.getJSONObject("result").getJSONObject("location").getString("lat");
}
}
}catch(IOException e) {
e.printStackTrace();
}finally{if(insr!=null){
insr.close();
}if(br!=null){
br.close();
}
}return newString[]{lng,lat};
}public String[] getAddr(String lng,String lat) throwsIOException {//System.out.println(lng );//System.out.println(lat);//String url = "http://api.map.baidu.com/geocoding/v3/?output=json&ak=你的ak值&coordtype=wgs84ll&location="+lat+","+lng;
String url = "http://api.map.baidu.com/reverse_geocoding/v3/?ak=你的ak值&output=json&coordtype=wgs84ll&location="+lat+","+lng;
URL myURL= null;
String province= "";
String city= "";
String qx= "";
String code= "";
URLConnection httpsConn= null;try{
myURL= newURL(url);
}catch(MalformedURLException e) {
e.printStackTrace();
}
InputStreamReader insr= null;
BufferedReader br= null;try{
httpsConn= (URLConnection) myURL.openConnection();//不使用代理
if (httpsConn != null) {
insr= new InputStreamReader( httpsConn.getInputStream(), "UTF-8");
br= newBufferedReader(insr);
String data= null;while((data= br.readLine())!=null){//System.out.println(data);
JSONObject json =JSONObject.parseObject(data);
province= json.getJSONObject("result").getJSONObject("addressComponent").getString("province");
city= json.getJSONObject("result").getJSONObject("addressComponent").getString("city");
qx= json.getJSONObject("result").getJSONObject("addressComponent").getString("district");
code= json.getJSONObject("result").getJSONObject("addressComponent").getString("adcode");
}
}
}catch(IOException e) {
e.printStackTrace();
}finally{if(insr!=null){
insr.close();
}if(br!=null){
br.close();
}
}return newString[]{province,city,qx,code};
}public static void main(String[] args) throwsIOException {
Tests getLatAndLngByBaidu= newTests();
String[] o= getLatAndLngByBaidu.getCoordinate("石家庄铁道大学");
String[] o1= getLatAndLngByBaidu.getAddr(o[0], o[1]);
System.out.println(o1[0]);
System.out.println(o1[1]);
System.out.println(o1[2]);
System.out.println(o1[3]);
}
}