街上Google地图下载代码实现

import java.io.BufferedOutputStream;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;


import java.sql.PreparedStatement ;
import java.sql.Connection;
import java.sql.SQLException;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import java.net.URL;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;


/**
 * 下载地图类
 */
public class DownloadMap implements Runnable {

	private BufferedOutputStream out = null;
	private BufferedInputStream bis = null;
	private ByteArrayOutputStream baos = null;
	private ByteArrayInputStream bais = null;

	private int ZOOM = 0;					//地图的级别
	private Connection con = null;
	private PreparedStatement pstmt = null;
	private int  num = 0;					//每级地图数目
	private final int width = 256;			//地图的宽
	private final int height = 256;			//地图的高
	private int counts = 0;					//多线程下载每个线程下载的范围
	private int flag = 0;					//协助多线程下载范围
	
	public DownloadMap(int ZOOM,int flag,int count) {  
		this.ZOOM = ZOOM;
		num = (int)Math.pow(2,ZOOM);   //每级地图横、竖有多少张
		con = DBBean.getCon("jdbc:mysql://localhost:3306/googlemap?useUnicode=true&characterEncoding=utf-8","root","123");
		this.flag = flag;
		this.counts = count;
		
	}

	public void run(){
		//System.out.println("it is start run");
		
 		//int  num = (int)Math.pow(2,ZOOM);					//双重循环下载
		for(int i=(flag*counts);i<((flag+1)*counts);i++){
			for(int j=0;j<num;j++){	
				getMap(pixelToLng(128+256*i,ZOOM),pixelToLat(128+256*j,ZOOM),i,j);
			}
		}

	}	

	//像素到经度
	public static double pixelToLng(double pixelX,int ZOOM){
		return pixelX * 360 / (256L << ZOOM)-180;
	}
	//像素到纬度
	public double pixelToLat(double pixelY,int ZOOM){
		double y = 2 * Math.PI * (1 - pixelY / (128 << ZOOM));
		double z = Math.pow(Math.E,y);
		double siny = (z-1) / (z+1);
		return Math.asin(siny) * 180 / Math.PI;
	}
	
	static int  count = 0;				//标记下载了多少张了
	public synchronized void getMap(double x,double y,int i,int j){
		count ++;
		System.out.println(count);
		try{
			URL url=new URL("http://maps.googleapis.com/maps/api/staticmap?center="+y+","+x+"&zoom="+ZOOM+"&"+
				"size=256x316&key=AIzaSyAbEVDxcEUvCt36Ahe9LWA5j0g_NnzHTF4&maptype=satellite&sensor=false");   //下载此url的图片
			//创建流
		//	baos = new ByteArrayOutputStream(4096);
			bis = new BufferedInputStream(url.openStream());				//打开流,注释掉的程序为将bis转换成二进制流
	/*		out = new BufferedOutputStream(baos);
			byte[] buff = new byte[4096];
			int len = 0;
			while((len = bis.read(buff))!=-1) {
				System.out.println("len = " + len);
				out.write(buff,0,len);
			}
			out.flush();

			byte[] buff_array = baos.toByteArray();
			System.out.println("buff_array.length = " + buff_array.length);
			InputStream in = new ByteArrayInputStream(buff_array);
*/
			InputStream in = castToByte();						//转换成二进制流,并写入数据库
			
			String sql = "insert into map8(position,row,col,image) values(?,?,?,?)";	
			pstmt = con.prepareStatement(sql);
			int position = j * num + i;
			pstmt.setInt(1,position);
			pstmt.setInt(2,j);
			pstmt.setInt(3,i);
			pstmt.setBinaryStream(4,in);
			pstmt.executeUpdate();

				bis.close();
				in.close();
				pstmt.close();
			//	out.close();
			//	baos.close();
		}catch(Exception e){
			e.printStackTrace();
		} finally {
			try{
				bis.close();
				//in.close();
				//out.close();
				//baos.close();
			} catch(IOException e) {
				System.out.println("关闭流失败");
				e.printStackTrace();
			}
		}
	}

	//转换成二进制流
	public InputStream castToByte() {
		
		BufferedImage buff2 = null;
		InputStream in = null;
		ByteArrayOutputStream baos = null;
		int[] buff = null;
		try{
			
			BufferedImage buff1  = ImageIO.read(bis);					//将流读到Imagebuffere里进行处理后输出
			buff2 = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
			buff = new int[width*height];
			buff1.getRGB(0,30,width,height,buff,0,width);				//去除水印Google
			buff2.setRGB(0,0,width,height,buff,0,width);
		} catch(IOException e){
			System.out.println("读取流失败");
			e.printStackTrace();
		}
		try{
			baos = new ByteArrayOutputStream();						
			ImageIO.write(buff2,"png",baos);					//将转换后的流写入字节输出流
			byte[] temp_buff = baos.toByteArray();
			in = new ByteArrayInputStream(temp_buff);			//转换成字节输入流
			baos.close();
		} catch(IOException e) {
			System.out.println("转换流失败");
			e.printStackTrace();
		} 
		return in;
	}
	
	
	public static void main(String[] args) {

		//ExecutorService service=Executors.newFixedThreadPool(35);
		int total = (int)Math.pow(2,8);					//8级地图,开32个线程下载
		int small = total/32;

		for(int i=0;i<32;i++){
			new Thread(new DownloadMap(8,0,small)).start();
			//new Thread(new DownloadMap(9)).start();

		//  service.submit(new MyExecutor(i));

		}

		System.out.println("submit finish");

		//service.shutdown();
		
	}

}
其中有个类是连接数据库的。这个是下载8级的地图。由于14级以上地图的数量就会变的更多。很难短时间下完。建议开很多台机器,用学校的机房下还是挺快的。一百多台一起。不过16级的地图就有40多亿了。反正够下的。下一篇讲下怎么恢复成一幅完整的地图
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值