leaflet 加载腾讯地图作为底图

问题点:因为腾讯地图是用到加密的GCJ02火星坐标系,跟WGS84是有20-700m的偏移的,所以要用到gcoord纠偏

代码如下:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Leaflet Tencent Map</title>
<style>
#map {
height: 1000px;
}
</style>
</head>
<body>
<button onclick="shpbuffer()" style="z-index: 99;">范围搜索</button>
<div id="map"></div>
 
<!-- 引入腾讯地图 JavaScript API -->
<script src="https://map.qq.com/api/js?v=2.exp&key=腾讯密钥&libraries=convertor"></script>
 
<!-- 引入 Leaflet 库 -->
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.8.0/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet/dist/leaflet.js"></script>
 
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.7.0/proj4.js"></script>
<!-- 引入 gcoord 库 -->
<script src="https://unpkg.com/gcoord@0.3.2/dist/gcoord.js"></script>
 
<script>
//var wgs84 = '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs';
//var gcj02 = '+proj=tmerc +lat_0=0 +lon_0=105 +k=1 +x_0=500000 +y_0=0 +ellps=GRS80 +units=m +no_defs';
//自定义 腾讯底图的layer
L.TileLayer.TencentTileLayer = L.TileLayer.extend({
initialize: function (param, options) {
var templateImgUrl = "//p{s}.map.gtimg.com/sateTiles/{z}/{sx}/{sy}/{x}_{ry}.jpg"
var templateDemUrl = "//p{s}.map.gtimg.com/demTiles/{z}/{sx}/{sy}/{x}_{ry}.jpg"
var templateUrl = "//rt{s}.map.gtimg.com/tile?z={z}&x={x}&y={ry}&{p}"
var myUrl = (param === "img" ? templateImgUrl : (param === "dem" ? templateDemUrl : templateUrl))
options = L.extend({// 位运算符:a >> b 等价于 Math.floor(a / Math.pow(2, b)),a << b 等价于 Math.floor(a * Math.pow(2, b))
getUrlArgs: (o) => { return { x: o.x, ry: (1 << o.z) - o.y - 1, z: o.z, sx: o.x >> 4, sy: ((1 << o.z) - o.y - 1) >> 4 } },
p: param, subdomains: "0123", minZoom: 0, maxZoom: 23, minNativeZoom: 1, maxNativeZoom: param === "dem" ? 15 : 18
}, options)
L.TileLayer.prototype.initialize.call(this, myUrl, options)
},
getTileUrl: function (coords) {
if (this.options.getUrlArgs) {
return L.Util.template(this._url, L.extend({ s: this._getSubdomain(coords), r: L.Browser.retina ? '@2x' : '' }, this.options.getUrlArgs(coords), this.options))
} else {
return L.TileLayer.prototype.getTileUrl.call(this, coords)
}
},
_setZoomTransform: function (level, center, zoom) {
center = L.latLng(gcoord.transform([center.lng, center.lat], gcoord.WGS84, gcoord.GCJ02).reverse()) // 采用 gcoord 库进行纠偏
 
L.TileLayer.prototype._setZoomTransform.call(this, level, center, zoom)
},
_getTiledPixelBounds: function (center) {
center = L.latLng(gcoord.transform([center.lng, center.lat], gcoord.WGS84, gcoord.GCJ02).reverse()) // 采用 gcoord 库进行纠偏
return L.TileLayer.prototype._getTiledPixelBounds.call(this, center)
},
})
L.tileLayer.tencentTileLayer = function (param, options) { return new L.TileLayer.TencentTileLayer(param, options) }
 
 
var map;
function initMap() {
//leaflet
map = L.map("map", { center: [29.708050, 118.321499], zoom: 15, zoomControl: false, attributionControl: false, doubleClickZoom: false })
tvs1_Layer = L.tileLayer.tencentTileLayer("type=vector&styleid=1")
map.addLayer(tvs1_Layer)
}
// 初始化地图
initMap();
function shpbuffer(){
var geometryData = {
 
};
axios.post('http://127.0.0../api/gis/tbuffer', geometryData)
.then(function (response) {
// Handle response from the backend
console.log(response);
response.data.features.forEach((res)=>{
var pointarr = res.geometry.coordinates[0]
var polygon = L.polygon(pointarr, {
color: 'red', // 多边形边线颜色
fillColor: 'blue', // 多边形填充颜色
fillOpacity: 0.3 // 多边形填充透明度
}).addTo(map);
})
map.flyTo([36.665646062393186, 117.31700511264272], 13, {
duration: 3 // 动画持续时间,单位为秒
});
})
.catch(function (error) {
// Handle error
console.error(error);
});
}
 
</script>
</body>
</html>

  可参考链接:https://blog.csdn.net/jingjing_n/article/details/127323669

posted @   小鱼写代码的过往  阅读(285)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示