java 根据经纬度获取百度地图地址

来源:互联网 发布:云主机绑定域名 编辑:程序博客网 时间:2024/05/01 11:12
PrintWriter outprint = response.getWriter();         //String lat = "39.983424"; // 纬度          //String lng = "116.322987";  // 经度        String lng = request.getParameter("jingdu");        String lat = request.getParameter("weidu");        //outprint.print(lng + ";" + lat);        try{            URL url = new URL("http://api.map.baidu.com/geocoder/v2/?ak=wPPxsfCtNFm4PqEV68jbDdjP7U4twstj&callback=renderReverse&location="+ lat + "," + lng + "&output=json&pois=1");            HttpURLConnection ucon = (HttpURLConnection) url.openConnection();            ucon.connect();            InputStream in = ucon.getInputStream();            BufferedReader reader = new BufferedReader(new InputStreamReader(in,"UTF-8"));            String str = reader.readLine();             //outprint.print(str);            str = str.substring(str.indexOf("(") + 1, str.length()-1);            //outprint.print(str);            JSONObject jsonObject = new JSONObject(str);            String address = "";            address = jsonObject.getJSONObject("result").getString("formatted_address");            outprint.print(address);            outprint.close();            outprint=null;          }catch(Exception e){            e.printStackTrace();         }