Google地图下载代码实现

  1. import java.io.BufferedOutputStream;  
  2. import java.io.BufferedInputStream;  
  3. import java.io.FileOutputStream;  
  4. import java.io.File;  
  5. import java.io.IOException;  
  6. import java.io.BufferedReader;  
  7. import java.io.InputStreamReader;  
  8. import java.io.InputStream;  
  9. import java.io.ByteArrayInputStream;  
  10. import java.io.ByteArrayOutputStream;  
  11.   
  12.   
  13. import java.sql.PreparedStatement ;  
  14. import java.sql.Connection;  
  15. import java.sql.SQLException;  
  16.   
  17. import java.util.concurrent.ExecutorService;  
  18. import java.util.concurrent.Executors;  
  19.   
  20. import java.net.URL;  
  21. import javax.imageio.ImageIO;  
  22. import java.awt.image.BufferedImage;  
  23.   
  24.   
  25. /** 
  26.  * 下载地图类 
  27.  */  
  28. public class DownloadMap implements Runnable {  
  29.   
  30.     private BufferedOutputStream out = null;  
  31.     private BufferedInputStream bis = null;  
  32.     private ByteArrayOutputStream baos = null;  
  33.     private ByteArrayInputStream bais = null;  
  34.   
  35.     private int ZOOM = 0;                   //地图的级别  
  36.     private Connection con = null;  
  37.     private PreparedStatement pstmt = null;  
  38.     private int  num = 0;                   //每级地图数目  
  39.     private final int width = 256;          //地图的宽  
  40.     private final int height = 256;         //地图的高  
  41.     private int counts = 0;                 //多线程下载每个线程下载的范围  
  42.     private int flag = 0;                   //协助多线程下载范围  
  43.       
  44.     public DownloadMap(int ZOOM,int flag,int count) {    
  45.         this.ZOOM = ZOOM;  
  46.         num = (int)Math.pow(2,ZOOM);   //每级地图横、竖有多少张  
  47.         con = DBBean.getCon("jdbc:mysql://localhost:3306/googlemap?useUnicode=true&characterEncoding=utf-8","root","123");  
  48.         this.flag = flag;  
  49.         this.counts = count;  
  50.           
  51.     }  
  52.   
  53.     public void run(){  
  54.         //System.out.println("it is start run");  
  55.           
  56.         //int  num = (int)Math.pow(2,ZOOM);                 //双重循环下载  
  57.         for(int i=(flag*counts);i<((flag+1)*counts);i++){  
  58.             for(int j=0;j<num;j++){    
  59.                 getMap(pixelToLng(128+256*i,ZOOM),pixelToLat(128+256*j,ZOOM),i,j);  
  60.             }  
  61.         }  
  62.   
  63.     }     
  64.   
  65.     //像素到经度  
  66.     public static double pixelToLng(double pixelX,int ZOOM){  
  67.         return pixelX * 360 / (256L << ZOOM)-180;  
  68.     }  
  69.     //像素到纬度  
  70.     public double pixelToLat(double pixelY,int ZOOM){  
  71.         double y = 2 * Math.PI * (1 - pixelY / (128 << ZOOM));  
  72.         double z = Math.pow(Math.E,y);  
  73.         double siny = (z-1) / (z+1);  
  74.         return Math.asin(siny) * 180 / Math.PI;  
  75.     }  
  76.       
  77.     static int  count = 0;              //标记下载了多少张了  
  78.     public synchronized void getMap(double x,double y,int i,int j){  
  79.         count ++;  
  80.         System.out.println(count);  
  81.         try{  
  82.             URL url=new URL("http://maps.googleapis.com/maps/api/staticmap?center="+y+","+x+"&zoom="+ZOOM+"&"+  
  83.                 "size=256x316&key=AIzaSyAbEVDxcEUvCt36Ahe9LWA5j0g_NnzHTF4&maptype=satellite&sensor=false");   //下载此url的图片  
  84.             //创建流  
  85.         //  baos = new ByteArrayOutputStream(4096);  
  86.             bis = new BufferedInputStream(url.openStream());                //打开流,注释掉的程序为将bis转换成二进制流  
  87.     /*      out = new BufferedOutputStream(baos); 
  88.             byte[] buff = new byte[4096]; 
  89.             int len = 0; 
  90.             while((len = bis.read(buff))!=-1) { 
  91.                 System.out.println("len = " + len); 
  92.                 out.write(buff,0,len); 
  93.             } 
  94.             out.flush(); 
  95.  
  96.             byte[] buff_array = baos.toByteArray(); 
  97.             System.out.println("buff_array.length = " + buff_array.length); 
  98.             InputStream in = new ByteArrayInputStream(buff_array); 
  99. */  
  100.             InputStream in = castToByte();                      //转换成二进制流,并写入数据库  
  101.               
  102.             String sql = "insert into map8(position,row,col,image) values(?,?,?,?)";      
  103.             pstmt = con.prepareStatement(sql);  
  104.             int position = j * num + i;  
  105.             pstmt.setInt(1,position);  
  106.             pstmt.setInt(2,j);  
  107.             pstmt.setInt(3,i);  
  108.             pstmt.setBinaryStream(4,in);  
  109.             pstmt.executeUpdate();  
  110.   
  111.                 bis.close();  
  112.                 in.close();  
  113.                 pstmt.close();  
  114.             //  out.close();  
  115.             //  baos.close();  
  116.         }catch(Exception e){  
  117.             e.printStackTrace();  
  118.         } finally {  
  119.             try{  
  120.                 bis.close();  
  121.                 //in.close();  
  122.                 //out.close();  
  123.                 //baos.close();  
  124.             } catch(IOException e) {  
  125.                 System.out.println("关闭流失败");  
  126.                 e.printStackTrace();  
  127.             }  
  128.         }  
  129.     }  
  130.   
  131.     //转换成二进制流  
  132.     public InputStream castToByte() {  
  133.           
  134.         BufferedImage buff2 = null;  
  135.         InputStream in = null;  
  136.         ByteArrayOutputStream baos = null;  
  137.         int[] buff = null;  
  138.         try{  
  139.               
  140.             BufferedImage buff1  = ImageIO.read(bis);                   //将流读到Imagebuffere里进行处理后输出  
  141.             buff2 = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);  
  142.             buff = new int[width*height];  
  143.             buff1.getRGB(0,30,width,height,buff,0,width);               //去除水印Google  
  144.             buff2.setRGB(0,0,width,height,buff,0,width);  
  145.         } catch(IOException e){  
  146.             System.out.println("读取流失败");  
  147.             e.printStackTrace();  
  148.         }  
  149.         try{  
  150.             baos = new ByteArrayOutputStream();                       
  151.             ImageIO.write(buff2,"png",baos);                    //将转换后的流写入字节输出流  
  152.             byte[] temp_buff = baos.toByteArray();  
  153.             in = new ByteArrayInputStream(temp_buff);           //转换成字节输入流  
  154.             baos.close();  
  155.         } catch(IOException e) {  
  156.             System.out.println("转换流失败");  
  157.             e.printStackTrace();  
  158.         }   
  159.         return in;  
  160.     }  
  161.       
  162.       
  163.     public static void main(String[] args) {  
  164.   
  165.         //ExecutorService service=Executors.newFixedThreadPool(35);  
  166.         int total = (int)Math.pow(2,8);                 //8级地图,开32个线程下载  
  167.         int small = total/32;  
  168.   
  169.         for(int i=0;i<32;i++){  
  170.             new Thread(new DownloadMap(8,0,small)).start();  
  171.             //new Thread(new DownloadMap(9)).start();  
  172.   
  173.         //  service.submit(new MyExecutor(i));  
  174.   
  175.         }  
  176.   
  177.         System.out.println("submit finish");  
  178.   
  179.         //service.shutdown();  
  180.           
  181.     }  
  182.   
  183. }  
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值