下载谷歌离线地图瓦片图
生活随笔
收集整理的這篇文章主要介紹了
下载谷歌离线地图瓦片图
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
項目中遇到一個需求,需要將某個地圖區域的離線地圖下載下來,整理很多網上的資料自己實現根據起始點的經緯度下載離線地圖,代碼如下
import java.io.*; import java.net.URL; import java.net.URLConnection; import java.util.HashMap; import java.util.Map; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream;public class Test {private static final int BUFFER_SIZE = 2 * 1024;public static void main(String[] args) throws Exception {double [] start = new double[]{36.03267263,103.480619123};//最大緯度 最小精度 起點double [] end = new double[]{35.522920921,103.520211928};//最小緯度 最大精度 終點// 36.03267263 103.480619123 35.522920921 103.520211928int [] z = new int[] {8,9};/*** 谷歌地圖地址參數* lyrs = 類型** h = roads only 路線圖* m = standard roadmap* p = terrain 地形圖* r = somehow altered roadmap* s = satellite only 衛星圖* t = terrain only* y = hybrid 混合*/String src = "http://mt0.google.cn/vt/lyrs=m@180000000&hl=zh-CN&gl=cn&x=%s&y=%s&z=%s&s=Ga";String targetDir ="D:\\map";//getGoogleMap(start,end,z,src,targetDir); //獲取谷歌地圖瓦片圖getFileInfo(targetDir);//獲取下載之后的文件信息toZip(targetDir, "D:/map.zip",true);//壓縮下載的文件}/*** 獲取下載之后的文件信息* @param targetDir*/ public static void getFileInfo(String targetDir){File file = new File(targetDir);System.out.println("下載后的文件大小:"+file.length()/1024+"M");File[] files = file.listFiles();for (File f : files){String level = f.getName();int count =0;File[] cfiles = f.listFiles();for (File cf : cfiles){count+=cf.list().length;}System.out.println("L"+level+"文件數量: "+count);} } /*** 根據起始點經緯度獲取地圖信息* @param startPoint 數組 如:new double[]{35.522920921,103.480619123};* @param endPoint 數組 如:new double[]{36.033726441,103.520211928};* @param z 地圖級別 new int[] {8,9}; 0-17* @param src 地圖下載地址* @param targetDir 本地保存的路徑* @throws IOException*/ public static void getGoogleMap(double [] startPoint,double [] endPoint,int [] z,String src,String targetDir) throws IOException {// int zoom = 15;double minlat = startPoint[0]; //35.522920921double minlon = startPoint[1];//103.480619123double maxlat = endPoint[0];//36.033726441double maxlon = endPoint[1];//103.520211928for (int k = z[0]; k <= z[1]; k ++){Map<String, Integer> minMap = getTileNumber(minlat, minlon, k);Map<String, Integer> maxMap = getTileNumber(maxlat, maxlon, k);int minX = minMap.get("x");int minY = minMap.get("y") ;int maxX = maxMap.get("x");int maxY = maxMap.get("y");//for (int i = minX; i <= maxX; i++) {for (int j = minY; j <= maxY; j++) {String url = String.format(src, i, j, k);System.out.println(url);downLaodImages(url, i, j, k,targetDir);}}} }/*** 根據經緯度獲取瓦片坐標 算法請參考* https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames* @param lat* @param lon* @param zoom* @return*/ public static Map getTileNumber(final double lat, final double lon, final int zoom) {Map<String,Integer> map = new HashMap<>();int xtile = (int)Math.floor( (lon + 180) / 360 * (1<<zoom) ) ;int ytile = (int)Math.floor( (1 - Math.log(Math.tan(Math.toRadians(lat)) + 1 / Math.cos(Math.toRadians(lat))) / Math.PI) / 2 * (1<<zoom) ) ;if (xtile < 0)xtile=0;if (xtile >= (1<<zoom))xtile=((1<<zoom)-1);if (ytile < 0)ytile=0;if (ytile >= (1<<zoom))ytile=((1<<zoom)-1);map.put("z",zoom);map.put("x",xtile);map.put("y",ytile);return map; }/**** @param url 下載圖片的url* @param x* @param y* @param z* @throws IOException*/ public static void downLaodImages(String url,int x,int y, int z,String targetDir) throws IOException {URL realUrl = new URL(url);// 打開和URL之間的連接URLConnection conn = realUrl.openConnection();// 設置通用的請求屬性conn.setRequestProperty("Accept-Charset", "utf-8");conn.setRequestProperty("contentType", "utf-8");conn.setRequestProperty("accept", "*/*");conn.setRequestProperty("connection", "Keep-Alive");conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");File zFile = new File(targetDir+"\\"+z);if(!zFile.exists()){zFile.mkdir();}File xFile = new File(targetDir+"\\"+z+"\\"+x);if(!xFile.exists()){xFile.mkdir();}DataInputStream dataInputStream = new DataInputStream(conn.getInputStream()) ;;FileOutputStream fileOutputStream = null;//根據坐標 x y z生成文件名 下載png無損圖片String imageName = targetDir+"\\"+z+"\\"+x+"\\"+y+".png";fileOutputStream = new FileOutputStream(new File(imageName));ByteArrayOutputStream output = new ByteArrayOutputStream();byte[] buffer = new byte[1024];int length;while ((length = dataInputStream.read(buffer)) > 0) {output.write(buffer, 0, length);}fileOutputStream.write(output.toByteArray());dataInputStream.close();fileOutputStream.close();output.close();System.out.println("已完成下載!"); }/*** 壓縮*/public static void toZip(String srcDir, String outName, boolean KeepDirStructure)throws RuntimeException{long start = System.currentTimeMillis();ZipOutputStream zos = null ;try {FileOutputStream out= new FileOutputStream(new File(outName));zos = new ZipOutputStream(out);File sourceFile = new File(srcDir);compress(sourceFile,zos,sourceFile.getName(),KeepDirStructure);long end = System.currentTimeMillis();System.out.println("壓縮完成,耗時:" + (end - start) +" ms");} catch (Exception e) {throw new RuntimeException("zip error",e);}finally{if(zos != null){try {zos.close();} catch (IOException e) {e.printStackTrace();}}}} private static void compress(File sourceFile, ZipOutputStream zos, String name,boolean KeepDirStructure) throws Exception{byte[] buf = new byte[BUFFER_SIZE];if(sourceFile.isFile()){// 向zip輸出流中添加一個zip實體,構造器中name為zip實體的文件的名字zos.putNextEntry(new ZipEntry(name));// copy文件到zip輸出流中int len;FileInputStream in = new FileInputStream(sourceFile);while ((len = in.read(buf)) != -1){zos.write(buf, 0, len);}// Complete the entryzos.closeEntry();in.close();} else {//是文件夾File[] listFiles = sourceFile.listFiles();if(listFiles == null || listFiles.length == 0){// 需要保留原來的文件結構時,需要對空文件夾進行處理if(KeepDirStructure){// 空文件夾的處理zos.putNextEntry(new ZipEntry(name + "/"));// 沒有文件,不需要文件的copyzos.closeEntry();}}else {for (File file : listFiles) {// 判斷是否需要保留原來的文件結構if (KeepDirStructure) {// 注意:file.getName()前面需要帶上父文件夾的名字加一斜杠,// 不然最后壓縮包中就不能保留原來的文件結構,即:所有文件都跑到壓縮包根目錄下了compress(file, zos, name + "/" + file.getName(),KeepDirStructure);} else {compress(file, zos, file.getName(),KeepDirStructure);}}}}}}安利一門Python超級好課!
掃碼下單輸優惠碼【csdnfxzs】再減5元,比官網還便宜!
總結
以上是生活随笔為你收集整理的下载谷歌离线地图瓦片图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网络安全管理规章制度
- 下一篇: mysql 网页员工登记表_作业1:小型