JAvaコードは経緯度に基づいて百度地図apiから位置を取得し、javaはget要求を送信する
22336 ワード
public class GetLocationBaiduMap {
public static void main(String[] args) {
String lat="31.931349213122";
String lng="120.96189745647";
// System.out.println(System.getProperty("file.encoding"));
try {
String s=getLocationByBaiduMap(lng, lat);
System.out.println(s);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String getLocationByBaiduMap(String longitude,String latitude) throws Exception {
System.out.println("--------------"+longitude+"-----------------");
System.out.println("--------------"+latitude+"-----------------");
String locJson=geturl("http://api.map.baidu.com/geoconv/v1/?coords=" + longitude + "," +latitude + "&from=1&to=5&ak=5415646413");
//System.out.println(locJson);
JSONObject jobject = JSON.parseObject(locJson);
JSONArray jsonArray = jobject.getJSONArray("result");
String lat=jsonArray.getJSONObject(0).getString("y");
String lng=jsonArray.getJSONObject(0).getString("x");
//System.out.println(lat);
String addrJson = geturl("http://api.map.baidu.com/geocoder/v2/?ak=4684645464&location=" + lat + "," + lng + "&output=json&pois=1");
System.out.println(addrJson);
JSONObject jobjectaddr = JSON.parseObject(addrJson);
//JSONObject rJsonObject=jobjectaddr.getJSONObject("result");
//System.out.println(rJsonObject);
String addr=jobjectaddr.getJSONObject("result").getString("formatted_address");
//addr=(String) JSON.parseObject(addr).get("city");
//addr=new String(addr.getBytes("gbk"),"UTF-8");
return addr;
}
//{"addressComponent":{"adcode":"320612","city":" ","city_level":2,"country":" ","country_code":0,"country_code_iso":"CHN","country_code_iso2":"CN","direction":"","distance":"","district":" ","province":" ","street":" ","street_number":"","town":""},"business":"","cityCode":161,"formatted_address":" ","location":{"lat":31.93105317856547,"lng":120.96189662538525},"poiRegions":[],"pois":[{"addr":" 150 ","cp":" ","direction":" ","distance":"53","name":" C ","parent_poi":{"addr":" 150 ","direction":" ","distance":"150","name":" ","point":{"x":120.96296334228969,"y":31.931761377621539},"tag":" ; ","uid":"849be02e396bfeaac91b95aa"},"poiType":" ","point":{"x":120.9622446978817,"y":31.93134001354758},"tag":" ; ","tel":"","uid":"f112225ba25266f1acee7a4d","zip":""},{"addr":" ","cp":" ","direction":" ","distance":"132","name":" B ","parent_poi":{"addr":"","direction":"","distance":"","name":"","point":{"x":0.0,"y":0.0},"tag":"","uid":""},"poiType":" ","point":{"x":120.96298130839988,"y":31.931470253560478},"tag":" ; ","tel":"","uid":"b6a3354be42ff5c3af04575a","zip":""},{"addr":" B ","cp":" ","direction":" ","distance":"139","name":" B ","parent_poi":{"addr":" 150 ","direction":" ","distance":"150","name":" ","point":{"x":120.96296334228969,"y":31.931761377621539},"tag":" ; ","uid":"849be02e396bfeaac91b95aa"},"poiType":" ","point":{"x":120.96310707117128,"y":31.931324691180877},"tag":" ; ","tel":"","uid":"469e15180e64cfbe4d554731","zip":""},{"addr":" 150 ","cp":" ","direction":" ","distance":"150","name":" ","parent_poi":{"addr":"","direction":"","distance":"","name":"","point":{"x":0.0,"y":0.0},"tag":"","uid":""},"poiType":" ","point":{"x":120.96296334228969,"y":31.931761377621539},"tag":" ; ","tel":"","uid":"849be02e396bfeaac91b95aa","zip":""},{"addr":" 188 ","cp":" ","direction":" ","distance":"165","name":" ","parent_poi":{"addr":"","direction":"","distance":"","name":"","point":{"x":0.0,"y":0.0},"tag":"","uid":""},"poiType":" ","point":{"x":120.96241537592859,"y":31.932244028619185},"tag":" ; ","tel":"","uid":"958b6228161c9efe6140a976","zip":""},{"addr":" 188 ","cp":" ","direction":" ","distance":"166","name":" ","parent_poi":{"addr":"","direction":"","distance":"","name":"","point":{"x":0.0,"y":0.0},"tag":"","uid":""},"poiType":" ","point":{"x":120.96240639287349,"y":31.9322516897255},"tag":" ; ","tel":"","uid":"a90b125fccbbafe24e5547d3","zip":""},{"addr":" 188 ","cp":" ","direction":" ","distance":"169","name":" ","parent_poi":{"addr":"","direction":"","distance":"","name":"","point":{"x":0.0,"y":0.0},"tag":"","uid":""},"poiType":" ","point":{"x":120.96307113895088,"y":31.931883955895075},"tag":" ; ","tel":"","uid":"3f1eeec7f18bcbeb80a00c69","zip":""},{"addr":" 1 ","cp":" ","direction":" ","distance":"175","name":" ","parent_poi":{"addr":"","direction":"","distance":"","name":"","point":{"x":0.0,"y":0.0},"tag":"","uid":""},"poiType":" ","point":{"x":120.96033130714544,"y":31.930903325085948},"tag":" ; ","tel":"","uid":"81c203b13abeed1ffa0cad44","zip":""},{"addr":" 1 ","cp":" ","direction":" ","distance":"192","name":" ","parent_poi":{"addr":" 1 ","direction":" ","distance":"175","name":" ","point":{"x":120.96033130714544,"y":31.930903325085948},"tag":" ; ","uid":"81c203b13abeed1ffa0cad44"},"poiType":" ","point":{"x":120.96020554437405,"y":31.93075010056795},"tag":" ; ","tel":"","uid":"4adeeaf5713b572fede4a50f","zip":""},{"addr":" ( )","cp":" ","direction":" ","distance":"263","name":" ( )","parent_poi":{"addr":"","direction":"","distance":"","name":"","point":{"x":0.0,"y":0.0},"tag":"","uid":""},"poiType":" ","point":{"x":120.96426588527915,"y":31.931048888139203},"tag":" ; ","tel":"","uid":"d5d1421f67aadfdbadee7a04","zip":""}],"roads":[],"sematic_description":" C 53 "}
private static String geturl(String geturl) throws Exception {
// webservice url
URL url = new URL(geturl);
// http
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
//
httpURLConnection.setRequestMethod("POST");
//
httpURLConnection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
//
httpURLConnection.setDoOutput(true);
//
httpURLConnection.setDoInput(true);
// ,
OutputStream outputStream = httpURLConnection.getOutputStream();
// soap
// String requestXmlString = requestXml(" ");
String content = "user_id="+ URLEncoder.encode("13846", "utf-8");
//
outputStream.write(content.getBytes());
//
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = in.readLine()) != null){
buffer.append(line);
}
String str = buffer.toString();
return str;
}
}