一起艳恶学习百度地图api(三)

这里写图片描述
经纬度与省市地址互转:

/**
     * 坐标地址转为省份地址
     * @param location
     * @return
     */
    public static Map<String, String> getLatAndLngByAddress2(String location){
        String address = "";
        String province = "";
        String city = "";
        String district = "";
        String url = String.format("http://api.map.baidu.com/geocoder/v2/?"
        +"callback=renderReverse&location=%s&output=json&pois=1&ak=4rcKAZKG9OIl0wDkICSLx8BA",location);
        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) {
                    address = data.substring(data.indexOf("\"formatted_address\":")
                            +("\"formatted_address\":").length()+1,data.indexOf("\",\"business\""));
                    province = data.substring(data.indexOf("\"province\":")
                            +("\"province\":").length()+1,data.indexOf("\",\"city\""));
                    city = data.substring(data.indexOf("\"city\":")
                            +("\"city\":").length()+1,data.indexOf("\",\"district\""));
                    district = data.substring(data.indexOf("\"district\":")
                            +("\"district\":").length()+1,data.indexOf("\",\"adcode\""));
                }
                insr.close();
            }
        } catch (IOException e) {

        }
        Map<String, String> map = new HashMap<String, String>();
        map.put("address", address);
        map.put("province", province);
        map.put("city", city);
        map.put("district", district);
        return map;
}

    /**
     * 地址转为坐标地址
     * @param addr
     * @return
     */
    public static Map<String, BigDecimal> getLatAndLngByAddress(String addr){
        String address = "";
        String lat = "";
        String lng = "";
        try {  
            address = java.net.URLEncoder.encode(addr,"UTF-8");  
        } catch (UnsupportedEncodingException e1) {  
            e1.printStackTrace();  
        } 
        String url = String.format("http://api.map.baidu.com/geocoder/v2/?"
        +"ak=4rcKAZKG9OIl0wDkICSLx8BA&output=json&address=%s",address);
        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) {
                    lat = data.substring(data.indexOf("\"lat\":") 
                    + ("\"lat\":").length(), data.indexOf("},\"precise\""));
                    lng = data.substring(data.indexOf("\"lng\":") 
                    + ("\"lng\":").length(), data.indexOf(",\"lat\""));
                }
                insr.close();
            }
        } catch (IOException e) {

        }
        Map<String, BigDecimal> map = new HashMap<String, BigDecimal>();
        map.put("lat", new BigDecimal(lat));
        map.put("lng", new BigDecimal(lng));
        return map;
}

你可能感兴趣的:(java之路,api,string,百度地图,地图)