离线瓦片下载代码分享

整理了下载天地图、OSM、谷歌、高德等常用地图的瓦片下载代码,可直接运行下载。可以作为学习使用,或在此基础上丰富功能。

package com.aerors.tiles;

import java.io.DataInputStream;  
import java.io.DataOutputStream;  
import java.io.File;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.net.HttpURLConnection;  
import java.net.URL;

public class tiandiTiles {
    /** 
     * 远程文件下载 
     * @param url 下载地址 
     * @param file 保存文件地址 
     */  
    public static boolean download(URL url, File file) throws IOException {  
        boolean flag = true;  
        DataOutputStream dos = null;  
        DataInputStream dis = null;  
        try {  
            if(!file.getParentFile().exists()) file.getParentFile().mkdirs();  
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
            dos = new DataOutputStream(new FileOutputStream(file));  
            dis = new DataInputStream(conn.getInputStream());  
            byte[] data = new byte[2048];  
            int i = 0;  
            while ((i = dis.read(data)) != -1) {  
                dos.write(data, 0, i);  
            }  
            dos.flush();  
        } catch (IOException e) {  
            flag = false;  
            throw e;  
        } finally {  
            if(dis != null) dis.close();  
            if(dos != null) dos.close();  
        }  
        return flag;  
    }  
  
    /** 
     * 计算分辨率 
     * @param maxLevel 最大级别 
     */  
    public static double[] getResolutions(int maxLevel){  
        double max = 360.0/256.0;  
        double[] resolutions = new double[maxLevel+1];  
        for(int z=0;z<=maxLevel;z++) resolutions[z] = max/Math.pow(2, z);  
        return resolutions;  
    }  
    
    /**
     * 根据经度获取切片规范下的行号
     * 
     * @param lon
     * @param zoom
     * @return
     */
    public static int getOSMTileXFromLongitude(double lon, int zoom) {
        return (int) (Math.floor((lon + 180) / 360 * Math.pow(2, zoom)));
    }

    /**
     * 根据纬度获取切片规范下的列号
     * 
     * @param lat
     * @param zoom
     * @return
     */
    public static int getOSMTileYFromLatitude(double lat, int zoom) {
        return (int) (Math.floor((1 - Math.log(Math.tan(lat * Math.PI / 180) + 1 / Math.cos(lat * Math.PI / 180)) / Math.PI) / 2 * Math.pow(2, zoom)));
    }
    
    public static void main(String[] arg) throws IOException{  
//      String type = "ArcGIS";
        String type = "CUSTOM";
        double[] extent = {107.54,33.59,109.26,35.20};  
        for(int z=0;z<14;z++){        
            //计算行列号(使用瓦片的中心点经纬度计算)
            //起始结束行 
            int minR = getOSMTileYFromLatitude(extent[3], z);
            int maxR = getOSMTileYFromLatitude(extent[1], z);
            //起始结束列 
            int minC = getOSMTileXFromLongitude(extent[0], z);
            int maxC = getOSMTileXFromLongitude(extent[2], z); 
            for(int y=minR;y<=maxR;y++){  
                for(int x=minC;x<=maxC;x++){  
//                    String urlstr = "http://t0.tianditu.com/DataServer?T=vec_w&x="+x+"&y="+y+"&l="+z; //天地图服务器t0-t8间选一个  
//                    String urlstr = "http://mt2.google.cn/vt/lyrs=m&scale=1&hl=zh-CN&gl=cn&x="+x+"&y="+y+"&z="+z; //谷歌地图服务器t0-t2间选一个  
//                    String urlstr = "http://online3.map.bdimg.com/onlinelabel/?qt=tile&x="+x+"&y="+y+"&z="+z+"&&styles=pl&udt=20170712&scaler=1&p=1"; //百度地图(加密过的)
//                    String urlstr = "http://c.tile.opencyclemap.org/cycle/"+z+"/"+x+"/"+y+".png"; //osm地图
                    String urlstr = "http://wprd04.is.autonavi.com/appmaptile?x="+x+"&y="+y+"&z="+z+"&lang=zh_cn&size=1&scl=1&style=8"; //高德地图(6:影像,7:矢量,8:影像路网)
                    System.out.println(urlstr);
                    
                    String path = null;
                    if(type.equals("ArcGIS")) {
                        //ArcGIS格式瓦片下载
                        path = getTDTilesForArcGISPath(x,y,z);
                    }else {
                        //一般格式瓦片下载
                        path = getTDTilesForCustomPath(x,y,z);
                    }
                    
                    File file = new File(path);  
                    URL url = new URL(urlstr);  
                    download(url,file);  
                }
            }  
        }  
    }
    
    public static String getTDTilesForArcGISPath(int x,int y,int z) {
        String l = "L" + String.format("%02d",z);
        String r = "R" + makePath(y);
        String c = "C" + makePath(x);
        String path = "D:/打包/"+l+File.separator+r+File.separator+c+".png";
        return path;
    }
    
    public static String getTDTilesForCustomPath(int x,int y,int z) {
        String path = "D:/打包/"+z+File.separator+y+File.separator+x+".png";
        return path;
    }
    
    private static String makePath(int num) {
            String str = Integer.toHexString(num);
            //ArcGIS行列都是8位长度
            while(str.length() < 8) {
                str = "0" + str;
            }
            return str;
    }
}

下载结果如下:


image.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,734评论 25 709
  • 因为偶然看到木心先生的这首诗,看后便爱上了。 从前慢 记得早先少年时大家诚诚恳恳说一句 是一句清早上火车站长街黑暗...
    刑素素阅读 11,838评论 0 8
  • 半夜一点,在潮热难耐中醒来。睡裙被汗水紧紧粘在身上,周围的空气闷热潮腻,电风扇还在徒劳的转着,试图搅动起一丝凉意。...
    兔娘阅读 2,187评论 0 0
  • 20180227亲爱的儿子,放学回家吃了点零食休息会就开始写作了。老师布置的作业早早就做完了,你说明早要默写英语单...
    简单的幸福_ceb3阅读 650评论 0 1
  • 原谅无能更不守信的自己,前进…… 哪怕强行记忆灌输,读书写字看动漫 追寻自己的二次元…… 不做一定比做错后悔,好比...
    乐不思命阅读 994评论 0 0