/** * 根据坐标获取具体地址 * @param coor 坐标字符串 * @return */ public static String getAdd(String coor){ String urlString = "http://restapi.amap.com/v3/geocode/regeo?key=8325164e247e15eea68b59e89200988b&s=rsv3&location="+coor+"&radius=2800&callback=jsonp_452865_&platform=JS&logversion=2.0&sdkversion=1.3&appname=http%3A%2F%2Flbs.amap.com%2Fconsole%2Fshow%2Fpicker&csid=49851531-2AE3-4A3B-A8C8-675A69BCA316"; String res = ""; try { URL url = new URL(urlString); java.net.HttpURLConnection conn = (java.net.HttpURLConnection)url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("GET"); java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream(),"UTF-8")); String line; while ((line = in.readLine()) != null) { res += line+" "; } in.close(); } catch (Exception e) { System.out.println("error in wapaction,and e is " + e.getMessage()); } System.out.println(res); return res; }
/**
* 根据地名获取坐标
* @param
*/
public static String getCoor(String address){
String urlString = "http://restapi.amap.com/v3/place/text?s=rsv3&children=&key=8325164e247e15eea68b59e89200988b&page=1&offset=10&city=610100&language=zh_cn&callback=jsonp_25126_&platform=JS&logversion=2.0&sdkversion=1.3&appname=http%3A%2F%2Flbs.amap.com%2Fconsole%2Fshow%2Fpicker&csid=19FA0D45-180F-4D45-BCB4-C6C265F55FF6&keywords="+address;
String res = "";
try {
//http://restapi.amap.com/v3/geocode/regeo?key=8325164e247e15eea68b59e89200988b&s=rsv3&location=101.539737903028,36.79828256329313&radius=2800&callback=jsonp_452865_&platform=JS&logversion=2.0&sdkversion=1.3&appname=http%3A%2F%2Flbs.amap.com%2Fconsole%2Fshow%2Fpicker&csid=49851531-2AE3-4A3B-A8C8-675A69BCA316
URL url = new URL(urlString);
java.net.HttpURLConnection conn = (java.net.HttpURLConnection)url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("GET");
java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream(),"UTF-8"));
String line;
while ((line = in.readLine()) != null) {
res += line+" ";
}
in.close();
} catch (Exception e) {
System.out.println("error in wapaction,and e is " + e.getMessage());
}
System.out.println(res);
return res;
}
public static void main(String[] agrs) {
String add = getAdd("101.539737903028,36.79828256329313");
String json = add.substring(add.indexOf("(")+1,add.lastIndexOf(")"));
JSONObject jsonObject = JSONObject.fromObject(json);
jsonObject = JSONObject.fromObject(jsonObject.get("regeocode"));
System.out.println(jsonObject.getString("formatted_address"));
String coor = getCoor("西宁市");
System.out.println(coor);
}
运行结果:
青海省西宁市湟中县李家山镇拦李公路
jsonp_25126_({"status":"1","count":"513","info":"OK","infocode":"10000","suggestion":{"keywords":[],"cities":[]},"pois":[{"id":"B03CB0SN18","name":"西宁市","type":"地名地址信息;普通地名;地市级地名","typecode":"190104","biz_type":[],"address":"城中区","location":"101.778112,36.617042","tel":[],"distance":[],"biz_ext":{"rating":[],"cost":[]},"pname":"青海省","cityname":"西宁市","adname":"城中区","importance":[],"shopid":[],"shopinfo":"2","poiweight":[]},{"id":"B03CB10608","name":"西宁市口腔医院(香格里拉路)","type":"医疗保健服务;专科医院;口腔医院","typecode":"090202","biz_type":[],"address":"福路巷16号","location":"101.753742,36.598270","tel":"0971-8213133","distance":[],"biz_ext":{"rating":[],"cost":[]},"pname":"青海省","cityname":"西宁市","adname":"城中区","importance":[],"shopid":[],"shopinfo":"2","poiweight":[]},{"id":"B0FFG4AXXD","name":"西宁市第二十一中学","type":"科教文化服务;学校;中学","typecode":"141202","biz_type":[],"address":"柴达木路386号","location":"101.681923,36.663087","tel":[],"distance":[],"biz_ext":{"rating":[],"cost":[]},"pname":"青海省","cityname":"西宁市","adname":"城北区","importance":[],"shopid":[],"shopinfo":"0","poiweight":[]},{"id":"B03CB01D43","name":"西宁市人民政府","type":"政府机构及社会团体;政府机关;地市级政府及事业单位","typecode":"130103","biz_type":[],"address":"南关街43号","location":"101.778112,36.617042","tel":"0971-8230424","distance":[],"biz_ext":{"rating":[],"cost":[]},"pname":"青海省","cityname":"西宁市","adname":"城中区","importance":[],"shopid":[],"shopinfo":"2","poiweight":[]},{"id":"B03CB04AE8","name":"西宁市财政局","type":"政府机构及社会团体;政府机关;地市级政府及事业单位","typecode":"130103","biz_type":[],"address":"五四西路11号","location":"101.749706,36.634725","tel":"0971-6310710","distance":[],"biz_ext":{"rating":[],"cost":[]},"pname":"青海省","cityname":"西宁市","adname":"城西区","importance":[],"shopid":[],"shopinfo":"2","poiweight":[]},{"id":"B03CB108ZK","name":"西宁市中医院","type":"医疗保健服务;综合医院;综合医院","typecode":"090100","biz_type":[],"address":"生物园区经一路20号","location":"101.751826,36.688383","tel":[],"distance":[],"biz_ext":{"rating":"1.0","cost":[]},"pname":"青海省","cityname":"西宁市","adname":"城北区","importance":[],"shopid":[],"shopinfo":"2","poiweight":[]},{"id":"B03CB06R0R","name":"西宁市殡仪馆","type":"生活服务;丧葬设施;殡仪馆","typecode":"071903","biz_type":[],"address":"宁互公路","location":"101.875836,36.605621","tel":"0971-5130150","distance":[],"biz_ext":{"rating":"3.9","cost":[]},"pname":"青海省","cityname":"西宁市","adname":"城东区","importance":[],"shopid":[],"shopinfo":"0","poiweight":[]},{"id":"B03CB04659","name":"西宁市地税局","type":"政府机构及社会团体;工商税务机构;地税机关","typecode":"130703","biz_type":[],"address":"西关大街街道昆仑路9号锦绣城","location":"101.763260,36.621090","tel":[],"distance":[],"biz_ext":{"rating":[],"cost":[]},"pname":"青海省","cityname":"西宁市","adname":"城西区","importance":[],"shopid":[],"shopinfo":"2","poiweight":[]},{"id":"B03CB06ZDJ","name":"西宁市交通局","type":"政府机构及社会团体;政府机关;地市级政府及事业单位","typecode":"130103","biz_type":[],"address":"五四大街28号","location":"101.767748,36.628084","tel":"0971-6145937","distance":[],"biz_ext":{"rating":[],"cost":[]},"pname":"青海省","cityname":"西宁市","adname":"城西区","importance":[],"shopid":[],"shopinfo":"2","poiweight":[]},{"id":"B03CB04420","name":"西宁市民政局","type":"政府机构及社会团体;政府机关;地市级政府及事业单位","typecode":"130103","biz_type":[],"address":"海晏路2号国贸大厦","location":"101.756503,36.639296","tel":"0971-8295867","distance":[],"biz_ext":{"rating":[],"cost":[]},"pname":"青海省","cityname":"西宁市","adname":"城西区","importance":[],"shopid":[],"shopinfo":"2","poiweight":[]}]})
干货分享,请笑纳,方法还有待优化