效果图:
代码实现:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>MapBoxGL</title>
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no">
<link href="./lib/mapboxdev/mapbox-gl.css" rel="stylesheet" />
<script src="lib/mapboxdev/mapbox-gl.js"></script>
<script src="js/GridLayer.js"></script>
<style>
body {
margin: 0;
padding: 0;
}
#map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
.mapboxgl-canvas {
outline: none;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
let localhost = window.location.origin;
let sources = {
"osm-tiles1": {
"type": "raster",
'tiles': [
"http://t0.tianditu.gov.cn/DataServer?T=img_w&x={x}&y={y}&l={z}&tk=ebf64362215c081f8317203220f133eb",
],
'tileSize': 256
},
"osm-tiles2": {
"type": "raster",
'tiles': [
"http://t0.tianditu.gov.cn/DataServer?T=cia_w&x={x}&y={y}&l={z}&tk=ebf64362215c081f8317203220f133eb",
],
'tileSize': 256
}
};
let layers = [{
"id": "simple-tiles1",
"type": "raster",
"source": "osm-tiles1",
},
{
"id": "simple-tiles2",
"type": "raster",
"source": "osm-tiles2",
}
];
var map = new mapboxgl.Map({
container: 'map',
style: {
"version": 8,
"sprite": localhost + "/MapBoxGL/css/sprite",
"glyphs": localhost + "/MapBoxGL/css/font/{fontstack}/{range}.pbf",
"sources": sources,
"layers": layers,
},
center: [117.110805, 36.663046],
zoom: 4
});
window.map = map;
map.on('load', function() {
//加载网格线
let style = {
line: {
"line-dasharray": [2, 2, 2, 2],
//设置线颜色
"line-color": "#A9A9A9",
//设置线宽度,像素单位
"line-width": 1.5
},
point: {
"text-color": "#FFFF00",
"text-halo-width": 2,
}
}
mapGridLayer.initStyle(style)
mapGridLayer.initProperty();
mapGridLayer.addSourceData([]);
mapGridLayer.addLayerData()
mapGridLayer.initGrid();
});
map.on('moveend', function(e) {
mapGridLayer.initProperty();
mapGridLayer.initGrid();
});
map.on('zoomend', function(e) {
mapGridLayer.initProperty();
mapGridLayer.initGrid();
});
</script>
</body>
</html>
组件GridLayer.js
var mapGridLayer = {
map: null,
bounds: null,//当前地图的四个顶点
style:null,
initStyle(style){
this.style = style;
},
addSourceData(Lines,Points) {
if(this.map.getSource('gridLine')||this.map.getSource('gridPoint')){
if(this.map.getLayer('Linegrid') || this.map.getLayer('Pointgrid')){
this.map.removeLayer("Linegrid");
this.map.removeLayer("Pointgrid");
}
this.map.removeSource("gridLine");
this.map.removeSource("gridPoint");
this.map.addSource('gridLine', {
'type': 'geojson',
'data': {
'type': 'FeatureCollection',
'features': Lines
}
});
this.map.addSource('gridPoint', {
'type': 'geojson',
'data': {
'type': 'FeatureCollection',
'features': Points
}
});
this.addLayerData();
}else{
this.map.addSource('gridLine', {
'type': 'geojson',
'data': {
'type': 'FeatureCollection',
'features': []
}
});
this.map.addSource('gridPoint', {
'type': 'geojson',
'data': {
'type': 'FeatureCollection',
'features': []
}
});
}
},
addLayerData(style){
//将线添加到一个图层中,在地图中显示
this.map.addLayer({
//此id可随意设置,但是要唯一
"id": "Linegrid",
//指定类型为线
"type": "line",
//设置数据来源
"source": 'gridLine',
//设置线型
"layout": {
//线条末端样式
'line-cap': 'round',
//线条相交处样式
'line-join': 'round'
},
//设置绘制参数
"paint": this.style.line
});
this.map.addLayer({
"id": "Pointgrid",
"type": "symbol",
"source": "gridPoint",
'layout': {
'text-field': ['get', 'name'],
'text-font': [
'Microsoft YaHei Regular'
],
'text-offset': [0, 1.25],
'text-anchor': 'bottom',
"text-size": [
"interpolate",
["linear"],
["zoom"],
4, 9,
6, 12
]
},
"paint": this.style.point
})
},
initProperty: function () {//初始化当前地图的状态
this.map = window.map;
// this.level = this.map.getZoom();
// this.initCenter = map.getCenter();
this.bounds = {
x1: Math.round(this.map.getBounds().getSouthWest().lng),
y1: Math.round(this.map.getBounds().getSouthWest().lat),
x2: Math.round(this.map.getBounds().getNorthEast().lng),
y2: Math.round(this.map.getBounds().getNorthEast().lat)
};
},
initGrid: function () {//初始化网格
//获取当前网格跨度
//初始化地图上的网格
let points = [];
let geometryLine = [];
let geometryPoint = [];
this.XY = this.bounds;
for (var i = this.XY.x1; i < this.XY.x2; i = i + 1) {//0.0063
points = [];
points.push([i, this.XY.y1], [i, this.XY.y2]);
geometryLine.push({
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": points
}
});
geometryPoint.push({
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": points[0]
},
'properties': {
'name': ""+i+"° 0'0'E"
}
});
geometryPoint.push({
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": points[1]
},
'properties': {
'name': ""+i+"° 0'0'E"
}
});
}
for (var i = this.XY.y1; i < this.XY.y2; i = i + 1) { // 0.0048
points = [];
points.push([this.XY.x1, i], [this.XY.x2, i])
geometryLine.push({
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": points
}
});
geometryPoint.push({
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": points[0]
},
'properties': {
'name': ""+i+"° 0'0'N"
}
});
geometryPoint.push({
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": points[1]
},
'properties': {
'name': ""+i+"° 0'0'N"
}
});
}
this.addSourceData(geometryLine,geometryPoint);
}
}