地图下载器代码结构设计及功能实现
- jcef包引入
![](https://img-blog.csdnimg.cn/img_convert/b8015ee24dcb8589e3b48d11b5e979f7.png)
![](https://img-blog.csdnimg.cn/img_convert/2c50d4b3f49a2a2e9645a8b19d415d63.png)
- 表结构设计
![](https://img-blog.csdnimg.cn/img_convert/f361475a6cacc63ec1fcdc4baa4c81f8.png)
![](https://img-blog.csdnimg.cn/img_convert/dc88675b05803d0bc67265821cc7e96a.png)
![](https://img-blog.csdnimg.cn/img_convert/d2a94454d400067ca843b91e3335c5c3.png)
![](https://img-blog.csdnimg.cn/img_convert/165285c5c2f97f7e93b588382e08a5bd.png)
- 后台关键代码结构
![](https://img-blog.csdnimg.cn/img_convert/d655753f63d0d5b2aa5077cc6b564419.png)
![](https://img-blog.csdnimg.cn/img_convert/8cde990e60c05a1a7fde8b203bb5ebd8.png)
- 前端关键代码结构
![](https://img-blog.csdnimg.cn/img_convert/4991569cae59fe7ebf4875012b9ef212.png)
![](https://img-blog.csdnimg.cn/img_convert/29bc1c79058d458abc4ee0e5ba422c43.png)
- 功能展示
- 启动页
![](https://img-blog.csdnimg.cn/img_convert/ad51a7ca7db77bedf288c2702403323a.png)
- 底图切换
![](https://img-blog.csdnimg.cn/img_convert/2c0fa0faf200b32beb413a0fa8ec1c0b.png)
![](https://img-blog.csdnimg.cn/img_convert/6e3364ba0488a4de4e228e9dfe6875e4.png)
![](https://img-blog.csdnimg.cn/img_convert/0651fc51620b462f507426f457537751.png)
![](https://img-blog.csdnimg.cn/img_convert/1334d3e35973a7b36af32eac156d1104.png)
![](https://img-blog.csdnimg.cn/img_convert/5949397604ff30e291b5d6a4aa08f43a.png)
![](https://img-blog.csdnimg.cn/img_convert/ac05c9056cfa03260291af176e2b2a91.png)
- 绘制选择下载区域
![](https://img-blog.csdnimg.cn/img_convert/d067ff98830ae02375b158a4136afa43.png)
![](https://img-blog.csdnimg.cn/img_convert/0539c580f4f8c042a3349cb12351a63f.png)
- 行政区划切换选择下载区域
![](https://img-blog.csdnimg.cn/img_convert/cb76dcccea8ad2c8b59e7689aa243bd3.png)
![](https://img-blog.csdnimg.cn/img_convert/49499fd417121d9808ea3ba501cc2a0b.png)
- 下载
![](https://img-blog.csdnimg.cn/img_convert/6bf21549fa9e8aed8fbee5ba0356bff5.png)
![](https://img-blog.csdnimg.cn/img_convert/1ad1649af721b2ffb8e18871ddda22aa.png)
![](https://img-blog.csdnimg.cn/img_convert/820575983fa1e8138e9817da7bf1dc7e.png)
![](https://img-blog.csdnimg.cn/img_convert/92fd0df0a74e2c183d21fc858a00cc9a.png)
![](https://img-blog.csdnimg.cn/img_convert/8d7dce2c175e20ccfb43dddc7044aeca.png)
![](https://img-blog.csdnimg.cn/img_convert/e009b92aaa5c20376d50ac007294c0a8.png)
- 关键代码
import { InnerMqClient } from '../../rx/inner-mq.service';
import { SubmitService } from '../../service/submit.service';
import { MapBase } from '../../map/map-base';
import { CommonUtil } from '../../util/common-util';
import { MapPage } from '../../view/page/map/map.page';
import { MapDraw } from '../../map/draw/map-draw';
import { MapWrap } from '../../map/draw/map-wrap';
import { GeoUtil } from "../../map/geo-util";
import { Point } from "../../map/entity/Point";
export class MapMessageProcessor {
constructor(
private mqClient: InnerMqClient,
private submitService: SubmitService,
private mapBase: MapBase,
private mapPage: MapPage,
) {
/** 放大 */
mqClient.sub('ZoomIn').subscribe((res) => {
mapBase.zoomIn();
});
/** 缩小 */
mqClient.sub('ZoomOut').subscribe((res) => {
mapBase.zoomOut();
});
/** 拖动 */
mqClient.sub('Pan').subscribe((res) => {
mapBase.pan();
});
/** 显示网格 */
mqClient.sub('GridSwitch').subscribe((res) => {
let update;
if (mapBase.getGridVisible()) {
mapBase.closeGrid();
update = false;
} else {
mapBase.showGrid();
update = true;
}
let config = mapBase.getMapConfig();
if (config) {
config.grid = update;
CommonUtil.setConfigCache(config);
mapBase.setMapConfig(config);
}
});
/** 切换图层源 */
mqClient.sub('SwitchResource').subscribe((res) => {
// 切换图层
debugger
let lastType = mapBase.getCurrentCoordinateType();
mapBase.switchMapResource(res);
let currentType = mapBase.getCurrentCoordinateType();
// 保存设置
let config = mapBase.getMapConfig();
if (config) {
config.layer = res;
CommonUtil.setConfigCache(config);
mapBase.setMapConfig(config);
}
// 检查坐标类型
if (lastType != currentType) {
if (lastType == 'wgs84' && currentType == 'gcj02') {
mapBase.turnMapFeaturesFromWgs84ToGcj02();
} else if (lastType == 'gcj02' && currentType == 'wgs84') {
mapBase.turnMapFeaturesFromGcj02ToWgs84();
}
}
// 回调
setTimeout(() => {
mapPage.updateShowInfo();
});
});
/** 绘制类型切换 - */
mqClient.sub('SwitchDrawType').subscribe((res) => {
mapBase.setDrawType(res);
});
/** 绘制 - */
mqClient.sub('OpenDraw').subscribe((res) => {
mapBase.pan();
mapBase.removeDrawedFeatures();
mapBase.openDraw({
drawEnd: () => {
setTimeout(() => {
mapBase.removeDrawInteraction();
})
},
modifyEnd: () => {
}
});
});
/** 绘制指定多边形并定位 - */
mqClient.sub('DrawPolygonAndPositioning').subscribe((res) => {
mapBase.pan();
mapBase.removeDrawedFeatures();
let blocks = JSON.parse(res);
for (let i = 0; i < blocks.length; i++) {
let points: Array<Point> = [];
for (let j = 0; j < blocks[i].length; j++) {
let point = new Point(blocks[i][j].lng, blocks[i][j].lat);
if (mapBase.getCurrentCoordinateType() == 'wgs84') {
points.push(GeoUtil.gcj02_To_wgs84(point));
} else {
points.push(point);
}
}
let feature = MapDraw.createPolygonFeature(points);
MapWrap.addFeature(mapBase, mapBase.drawLayerName, feature);
}
mapBase.setFitviewFromDrawLayer();
});
/** fitview - */
mqClient.sub('Fitview').subscribe((res) => {
mapBase.setFitviewFromDrawLayer();
});
/** 删除绘制 - */
mqClient.sub('RemoveDrawedShape').subscribe((res) => {
mapBase.removeDrawedFeatures();
});
/** 提交区块下载 - */
mqClient.sub('SubmitBlockDownload').subscribe((res) => {
let data = {
tileName: this.mapBase?.getCurrentXyzName(),
mapType: CommonUtil.getMapType(this.mapBase?.getCurrentXyzName()),
tileUrl: this.mapBase?.getCurrentXyzUrlResources(),
points: this.mapBase?.getDrawedPoints(),
};
this.submitService.blockDownload(data).then((r) => {
});
});
/** 提交世界下载 - */
mqClient.sub('SubmitWorldDownload').subscribe((res) => {
let data = {
tileName: this.mapBase?.getCurrentXyzName(),
mapType: CommonUtil.getMapType(this.mapBase?.getCurrentXyzName()),
tileUrl: this.mapBase?.getCurrentXyzUrlResources()
};
this.submitService.worldDownload(data).then((r) => {
});
});
}
}
package com.jmd.http;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.jmd.entity.result.DownloadResult;
import com.jmd.inst.DownloadAmountInstance;
import com.jmd.util.CommonUtils;
@Component
public class HttpDownload {
@Autowired
private HttpClient http;
@Autowired
private DownloadAmountInstance downloadAmountInstance;
/** 通过URL下载文件 */
public DownloadResult downloadTile(String url, HashMap<String, String> headers, int imgType, String path,
int retry) {
DownloadResult result = new DownloadResult();
boolean success = false;
byte[] bytes = http.getFileBytes(url, headers);
if (null != bytes) {
byte[] imgData = imageSwitch(imgType, bytes);
try {
if (null != imgData) {
// result.setImgData(imgData);
File file = new File(path);
FileUtils.writeByteArrayToFile(file, imgData);
if (file.exists() && file.isFile()) {
downloadAmountInstance.add(file.length());
success = true;
}
}
} catch (IOException e) {
success = false;
e.printStackTrace();
}
}
if (success) {
result.setSuccess(true);
} else if (Thread.currentThread().isInterrupted()) {
result.setSuccess(false);
} else {
retry = retry - 1;
if (retry >= 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
return downloadTile(url, headers, imgType, path, retry);
} else {
result.setSuccess(false);
}
}
return result;
}
/** 下载的图片进行转码 */
private byte[] imageSwitch(int imgType, byte[] imgData) {
if (imgType == 0) {
// 保持PNG
return imgData;
} else if (imgType == 1 || imgType == 2 || imgType == 3) {
// 转换为JPG
float quality = 0.9f;
switch (imgType) {
case 1:
quality = 0.2f;
break;
case 2:
quality = 0.6f;
break;
case 3:
quality = 0.9f;
break;
default:
break;
}
try {
return CommonUtils.png2jpg(imgData, quality);
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}
如果对您有帮助
感谢支持技术分享,请扫码点赞支持:
技术合作交流qq:2401315930
![](https://img-blog.csdnimg.cn/img_convert/7b13dfe8f851ec2a4159c938a8c73059.png)
![](https://img-blog.csdnimg.cn/img_convert/7d1df224cbff7d6542a28d86cf312ebe.gif)
编辑
![](https://img-blog.csdnimg.cn/img_convert/7f1df2e8654e7be15abe610494e3c28f.png)
![](https://img-blog.csdnimg.cn/img_convert/f59a48c176788870825babf1778ae5f2.gif)
编辑
![](https://img-blog.csdnimg.cn/img_convert/2b3792036bdcc0fd3d8378fb7bd130ea.jpeg)
![](https://img-blog.csdnimg.cn/img_convert/a24dc942e9a97a096b6d74eed9f4cb10.gif)
编辑
兴诚: 博主QQ2401315930
兴诚: QQ2401315930
Kwan的解忧杂货铺: 博主的博客每次都能让我豁然开朗,明白了很多不懂的问题,真的非常感谢你的贡献。每篇博文都是知识的瑰宝,我真的很喜欢你的风格,博主的文章总是内容丰富,通俗易懂,每次阅读都是一次启发,期待你的下一篇佳作。感谢精彩分享。
2401_85111936: 写的很详细,感谢博主的分享。【我也写了一些相关领域的文章,希望能够得到博主的指导,共同进步!】
Kwan的解忧杂货铺: 博主的博客每一篇博文都如一本启发书,我从中汲取灵感,希望博主继续为我们献上这些珍宝。是我的学习良师,每篇文章都充满见解,让我茅塞顿开,你的文章总是让我对复杂的主题有更深入的理解,非常感激你的专业分享。期待更多分享。