MapBoxGL 加载显示经纬度的网格

效果图:

代码实现:

<!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);
  }
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一碗老面i

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值