java调用百度地图API通过URL根据地理位置中文处理返回值获取经纬度

百度地图api地址:http://developer.baidu.com/map/
简单样例使用:

String sCurrentLine = "";
            String sTotalString = "";
            String lat = "";
            String lng = "";
            try {  
            String sURL ="http://api.map.baidu.com/geocoder/v2/?address="+addressString+
                    "&output=json&ak=F6b7ec696d76b327964518ffb1c2e86c&callback=showLocation";
            java.net.URL l_url = new java.net.URL(sURL);
            java.net.HttpURLConnection l_connection = (java.net.HttpURLConnection) l_url.openConnection();
            l_connection.connect();
            java.io.BufferedReader l_reader = new java.io.BufferedReader(new java.io.InputStreamReader(l_connection.getInputStream()));
            while ((sCurrentLine = l_reader.readLine()) != null)

            {
                sTotalString+=sCurrentLine;
                lat = sTotalString.substring(sTotalString.indexOf("\"lat\":") 
                         + ("\"lat\":").length(), sTotalString.indexOf("},\"precise\""));
                lng = sTotalString.substring(sTotalString.indexOf("\"lng\":") 
                            + ("\"lng\":").length(), sTotalString.indexOf(",\"lat\""));
            } 
            l_reader.close();
            } catch (Exception e) {  
            }  
            if (StringUtils.hasText(lat)&&StringUtils.hasText(lng)&&Float.valueOf(lng)>104.2869 && Float.valueOf(lng)<107.6536&&
                    Float.valueOf(lat)>35.2494&&Float.valueOf(lat)<39.8758) {
                nx12369ReportEntity.setLat(lat);
                nx12369ReportEntity.setLng(lng);
                tableService.updateAddress(nx12369ReportEntity);
            }

实际应用代码:

String sCurrentLine = "";
        String sTotalString = "";
        String lat = "";
        String lng = "";
        String addressString = "宁夏回族自治区环境保护厅";
        try {  
        String sURL ="http://api.map.baidu.com/geocoder/v2/?address="+addressString+
                "&output=json&ak=F6b7ec696d76b327964518ffb1c2e86c&callback=showLocation";
        java.net.URL l_url = new java.net.URL(sURL);
        java.net.HttpURLConnection l_connection = (java.net.HttpURLConnection) l_url.openConnection();
        l_connection.connect();
        java.io.BufferedReader l_reader = new java.io.BufferedReader(new java.io.InputStreamReader(l_connection.getInputStream()));
        while ((sCurrentLine = l_reader.readLine()) != null)

        {
            sTotalString+=sCurrentLine;
            lat = sTotalString.substring(sTotalString.indexOf("\"lat\":") 
                     + ("\"lat\":").length(), sTotalString.indexOf("},\"precise\""));
            lng = sTotalString.substring(sTotalString.indexOf("\"lng\":") 
                        + ("\"lng\":").length(), sTotalString.indexOf(",\"lat\""));
        } 
        l_reader.close();
        } catch (Exception e) {  
        }  
        if (StringUtils.hasText(lat)&&StringUtils.hasText(lng)) {
            System.out.println("经度为"+lat);
            System.out.println("纬度为"+lng);
        }

你可能感兴趣的:(JavaWeb学习)