码迷,mamicode.com
首页 > 数据库 > 详细

java 实现百度地图的地址解析及在数据库的读取存储过程

时间:2017-04-13 16:46:52      阅读:343      评论:0      收藏:0      [点我收藏+]

标签:ext   try   hash   acl   地址   需要   start   广州市   ble   

  package action;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

public class LocationUtil {

        private static final String ak = "XG8Ed4Vi6BN67wrmFLc7x63oYOayXIR1";
        
        /**
        * 返回输入地址的经纬度坐标 key lng(经度),lat(纬度)
        */
        public static Map<String, String> getLatitude(String address) {
                try {
                        // 将地址转换成utf-8的16进制
                        address = URLEncoder.encode(address, "UTF-8");
                        // 如果有代理,要设置代理,没代理可注释
                        // System.setProperty("http.proxyHost","192.168.172.23");
                        // System.setProperty("http.proxyPort","3209");
                                                          
                        URL resjson = new URL("http://api.map.baidu.com/geocoder/v2/?callback=renderOption&output=json&address="
                                        + address + "&city=广州市&output=json&ak=" + ak);
                      //  System.out.println(resjson);
                        BufferedReader in = new BufferedReader(new InputStreamReader(resjson.openStream()));
                        String res;
                        StringBuilder sb = new StringBuilder("");
                        while ((res = in.readLine()) != null) {
                                sb.append(res.trim());
                        }
                        in.close();
                        String str = sb.toString();
                        //System.out.println("return json:" + str);
                        if(str!=null&&!str.equals("")){
                                Map<String, String> map = null;
                                int lngStart = str.indexOf("\"lng\"");
                                int lngEnd = str.indexOf(",\"lat\"");
                                int latEnd = str.indexOf("},\"precise");
                                if (lngStart > 0 && lngEnd > 0 && latEnd > 0) {
                                        String lng = str.substring(lngStart + 6, lngEnd);
                                        String lat = str.substring(lngEnd + 7, latEnd);
                                        map = new HashMap<String, String>();
                                        map.put("lng", lng);
                                        map.put("lat", lat);
                                        return map;
                                }
                        }
                } catch (Exception e) {
                        e.printStackTrace();
                }
                return null;
        }
           
        public static void main(String args[]) {
                
                Map<String, String> map = LocationUtil.getLatitude("越秀区人民中路1号");
                if (null != map) {
                        System.out.println(map.get("lng"));
                        System.out.println(map.get("lat"));
                }
        }
}

 

package action;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;

public class JDBC {

        public static void main(String[] args) {

                Connection connection = null;
                Statement statement = null;
                try {
                        //1、加载数据库驱动
                        Class.forName("oracle.jdbc.OracleDriver");
                        //2、创建数据库连接
                        String url = "jdbc:oracle:thin:@localhost:1520:orcl";
                        connection = DriverManager.getConnection(url, "scot", "tiger");
                        
                        //3、进行数据库操作
                        statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);//Oracle数据库需要设置才能进行updata
                        ResultSet rs = statement.executeQuery("select djxh,dz,gps from t_location");//sql语句中出现多余的标点符号 ;,  会报错无效字符
                        LocationUtil locationUtil=new LocationUtil();
                        while(rs.next()){
                           // System.out.println(rs.toString());
                            String a=rs.getNString("djxh");
                            String bString=rs.getNString("dz");
                           Map  cString=locationUtil.getLatitude(bString);
                            String dString =cString .get("lng").toString()+","+cString.get("lat").toString();/得到经纬度
                            //System.out.println(dString);
                            rs.updateString("gps",dString);//存入数据库
                            rs.updateRow();
                            System.out.println(rs.getNString("dz")+" : "+rs.getNString("gps"));
                            
                     
                   
                        }
                        rs.close();
                        
                } catch (Exception e) {
                        e.printStackTrace();
                } finally {
                        //4、关闭连接
                        try {
                                if(statement != null){
                                        statement.close();
                                }
                                if(connection != null){
                                        connection.close();
                                }
                        } catch (SQLException e) {
                                e.printStackTrace();
                        }
                }
        }

}

 

java 实现百度地图的地址解析及在数据库的读取存储过程

标签:ext   try   hash   acl   地址   需要   start   广州市   ble   

原文地址:http://www.cnblogs.com/pakeng/p/6704505.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!