java 實現地址到經緯度座標的轉化

來源:互聯網
上載者:User

任務:有1000多條門店資訊(放在excel中,包括地址,店名,電話等,但是),老大讓我用地址通過找到相應的座標,然後加上座標後更新到公司的資料庫。

:1、使用按鍵精靈,按鍵精靈是一個模仿鍵盤滑鼠操作的軟體,用來寫動作指令碼的,由於時間緊,沒怎麼研究,因為整套動作太複雜了按鍵精靈嘗試了下不行就放棄了。

2、表單填充工具(就是把exel表格批量提交到網頁),什麼風越、烏溜漆(特別是這烏溜漆,還要錢,坑貨)都嘗試了下,結果都不滿意。因為我要把excel中的內容提交到網頁還要從網頁獲得相應的內容,所以這些用於大量註冊的軟體用不上。


:最後還是幹起了我本行--,把問題解決了。。

以下為具體步驟

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方法

/* * 管理資料庫串連的類 */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 blockSystem.out.println("向mysql中更新資料時發生異常!");e.printStackTrace();}}
在搞個main函數調用就ok了,有疑問請留言。

                       

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.