地址到经纬度坐标转化的JAVA代码

2022-10-20,,,,

任务:有1000多条门店信息(放在excel中,包括地址,店名,电话等,但是没有经纬度坐标),老大让我用地址通过百度地图拾取坐标系统找到相应的坐标,然后加上坐标后更新到公司的数据库。

失败的方案:1、使用按键精灵,按键精灵是一个模仿键盘鼠标操作的软件,用来写动作脚本的,由于时间紧,没怎么研究,因为整套动作太复杂了按键精灵尝试了下不行就放弃了。

2、表单填充工具(就是把exel表格批量提交到网页),什么风越、乌溜漆(特别是这乌溜漆,还要钱,坑货)都尝试了下,结果都不满意。因为我要把excel中的内容提交到网页还要从网页获得相应的内容,所以这些用于批量注册的软件用不上。

解决方案:最后还是干起了我本行---写代码,把问题解决了。思路是:通过传入地址作为参数拼接url调用百度地图,然后解析返回的页面,提取经纬度坐标。

以下为具体步骤

1、修改excel表中的属性名(方便后面用查询读取)然后倒入到数据库。

2、代码实现

实体类

复制代码 代码如下:
public class shopinfo { 
    private string name; 
    private string scope; 
    private string address; 
    private string mobile;//手机  
    private string phone;//座机  
    private string description; 
    private string lat;//经度  
    private string lng;//纬度  

    public shopinfo() { 

    } 
//....get和set方法 

关键代码  模拟http和解析返回的数据:

复制代码 代码如下:
/*
 * 管理数据库连接的类
 */ 
public class dbmanager{ 

    private connection con = null ; 
    private statement sta = null ; 
    private resultset rs = null ; 
    private preparedstatement ps = null ; 

     
    private connection cons = null ; 
    private statement stas = null ; 
    private resultset rss = null ; 
    private preparedstatement pss = null ; 

     

     
    //连接本地mysql参数 ?后面的参数是解决中文乱码的  
    private string mysqldriver="com.mysql.jdbc.driver" ; 
    private string content="jdbc:mysql://127.0.0.1:3306/test?useunicode=true&characterencoding=utf8"; 
    private string un="***"; 
    private string up="****"; 

    //连接服务器mysql参数  
    private string mysqldriver1="com.mysql.jdbc.driver" ; 
    private string content1="jdbc:mysql://***********:3306/test?useunicode=true&characterencoding=utf8"; 
    private string un1="*******"; 
    private string up1="****"; 

 
    public dbmanager() 
    { 
        try { 

            class.forname(mysqldriver); 
            system.out.println("加载mysql驱动..."); 
            con = drivermanager.getconnection(content,un,up); 
            sta = con.createstatement(); 
            system.out.println("连接本地数据库成功!!"); 
            class.forname(mysqldriver1); 
            system.out.println("加载mysql驱动..."); 
            cons = drivermanager.getconnection(content1,un1,up1); 
            stas = cons.createstatement(); 
            system.out.println("连接服务器成功!!"); 

        } catch (exception e) { 
            e.printstacktrace();     
        } 

    } 

 
    public arraylist<shopinfo> getall(string tablename) throws sqlexception{ 
        arraylist<shopinfo> allshops=new arraylist(); 
        shopinfo si; 

        string sql="select * from "+tablename; 
        system.out.println(sql); 
        rs=sta.executequery(sql); 
        while(rs.next()){ 
            si=new shopinfo(); 
            si.setaddress(rs.getstring("address")); 
            si.setdescription(rs.getstring("names")+"欢迎您的光临"); 
            si.setmobile(rs.getstring("keeperphone")); 
            si.setscope(tablename); 
            si.setphone(rs.getstring("shoptel")); 
            getpoint(si); 
            allshops.add(si); 
            system.out.println("经度:"+si.getlat()+"  纬度:"+si.getlng()); 
        } 

        return allshops; 
    } 
    //-------------------------》关键代码根据地址获得坐标《--------------------------------  
    public void getpoint(shopinfo shop){ 
         try {   
                string scurrentline;   
                string stotalstring;   
                scurrentline = "";   
                stotalstring = "";   
                java.io.inputstream l_urlstream;   

                java.net.url l_url = new java.net.url("http://api.map.baidu.com/geocoder/v2/?address="+shop.getaddress().replaceall(" ", "")+"&output=json&ak=702632e1add3d4953d0f105f27c294b9&callback=showlocation");   
                java.net.httpurlconnection l_connection = (java.net.httpurlconnection) l_url.openconnection();   
                l_connection.connect();   
                l_urlstream = l_connection.getinputstream();   
                java.io.bufferedreader l_reader = new java.io.bufferedreader(new java.io.inputstreamreader(l_urlstream));    
                string str=l_reader.readline(); 
                //用经度分割返回的网页代码  
                string s=","+"\""+"lat"+"\""+":"; 
                string strs[]=str.split(s, 2); 
                string s1="\""+"lng"+"\""+":"; 
               string a[]=strs[0].split(s1, 2); 
               shop.setlng(a[1]); 
               s1="}"+","+"\""; 
              string a1[]=strs[1].split(s1, 2); 
               shop.setlat(a1[0]); 
            } catch (exception e) {   
                e.printstacktrace();   
            }   

    } 
    //存入数据库  
    public void inputall(arraylist<shopinfo> allshops){ 
        system.out.println("开始向服务器中写入"); 
        string sql2="insert into test.dc_shop (name,scope,address,phone,description,image,createtime,lat,lng) values (?,?,?,?,?,?,?,?,?)"; 
        try { 
            pss=cons.preparestatement(sql2); 
            system.out.println("-------------------------等待写入数据条数: "+allshops.size()); 
            for(int i=0;i<allshops.size();i++){ 
                   pss.setstring(1,allshops.get(i).getname()); 
                pss.setstring(2, allshops.get(i).getscope()); 
                pss.setstring(3, allshops.get(i).getaddress()); 
                pss.setstring(4, allshops.get(i).getphone()); 
                pss.setstring(5, allshops.get(i).getdescription()); 
                pss.setstring(6, null);//图片路径  
                pss.setstring(7, allshops.get(i).getmobile()); 
                pss.setstring(8, allshops.get(i).getlat()); 
                pss.setstring(9, allshops.get(i).getlng()); 

                pss.executeupdate(); 
            } 
            pss.close(); 
            cons.close(); 

            system.out.println("--->ok"); 
        } catch (sqlexception e) { 
            // todo auto-generated catch block  
            system.out.println("向mysql中更新数据时发生异常!"); 
            e.printstacktrace();     
        } 
    } 

在搞个main函数调用就ok了。

《地址到经纬度坐标转化的JAVA代码.doc》

下载本文的Word格式文档,以方便收藏与打印。