最近搞地图搞的多,因为项目要求需要做个离线地图来添加覆盖物,网上有篇博客教我们怎么下载瓦片的,对应的是java代码,但是每次操作去看console里的层级和范围超级麻烦,而网上有提供软件下载,感觉有点慢,所以我觉得两者结合使用更加给力一点:
先给个java代码:
地址:https://blog.csdn.net/u012963373/article/details/75155435
package usi.distance.method;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class OutlineMap {
public static void main(String[] args)
throws Exception
{
String link =
"http://online3.map.bdimg.com/onlinelabel/?qt=tile&x={x}&y={y}&z={z}&styles=pl&udt=20180403&scaler=1&p=1";
int z = 16;//层级
int xmin = 12489;//x最小值
int xmax = 13008;//x最大值
int ymin = 3325;//y最小值
int ymax = 3997;//y最大值
int c = 0;//成功数
int fail = 0;//失败数量
for (int i = xmin; i <= xmax; i++)
{
for (int j = ymin; j <= ymax; j++)
{
try
{
URL url = new URL(link.replace("{x}", i + "").replace("{y}", j + "").replace("{z}", z + ""));
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout(100);
conn.connect();
InputStream in = conn.getInputStream();
File dir = new File("d:/mybaidumapdownload1/tiles/" + z + "/" + i);
if (!dir.exists())
{
dir.mkdirs();
}
File file = new File("d:/mybaidumapdownload1/tiles/" + z + "/" + i + "/" + j + ".png");
if (!file.exists())
{
file.createNewFile();
}
OutputStream out = new FileOutputStream(file);
byte[] bytes = new byte[1024 * 20];
int len = 0;
while ((len = in.read(bytes)) != -1)
{
out.write(bytes, 0, len);
}
out.close();
in.close();
// System.out.println("已成功下载:" + z + "_" + i + "_" + j + ".jpg");
c++;
}
catch (Exception e)
{
System.out.println(e.getMessage());
fail++;
}
}
}
System.out.println("共下载: " + c + " 张");
System.out.println("失败: " + fail + " 张");
}
}
前面的用下载器比较方便,如果到了15级特别多有时候软件会卡死,所以用代码,改变一下参数来下载更加实惠一点
涉及百度版权,请勿商用,后果自负!