Openlayer加载4326、3857坐标系的矢量切片服务

矢量切片(Vector Tile)

详细参考

常用坐标系

  1. EPSG:4326,等同于WGS84坐标系(约同于CGCS2000),单位:度(°);
  2. EPSG:3857,等同于900913,由Mercator投影而来,经常用于web地图,也称WebMercator,单位:米(m)。

Openlayer库引用

import { Map, View, Feature, Overlay } from "ol";
import TileLayer from "ol/layer/Tile";
import OSM from "ol/source/OSM";
import XYZ from "ol/source/XYZ";
import { ZoomSlider, MousePosition } from "ol/control";
import { VectorTile as VectorTileLayer } from "ol/layer";
import {
  Vector as VectorSource,
  VectorTile as VectorTileSource,
  TileDebug
} from "ol/source";
import { Circle as CircleStyle, Fill, Stroke, Style } from "ol/style";
import { GeoJSON, MVT } from "ol/format";
import Projection from "ol/proj/Projection";
import * as olProj from "ol/proj";
import { createStringXY } from "ol/coordinate";
import { createXYZ } from "ol/tilegrid";

EPSG:4326坐标系

    let country = new Style({
      stroke: new Stroke({
        color: "rgba(164, 162, 174)",
        width: 2
      }),
      fill: new Fill({
        color: "rgba(233, 229, 216)"
      })
    });
    
    let vector4326 = new VectorTileLayer({
      // 矢量切片的数据源
      source: new VectorTileSource({
        projection: "EPSG:4326",
        format: new MVT({
          idProperty: "iso_a3"
        }),
        tileGrid: createXYZ({
          extent: olProj.get("EPSG:4326").getExtent(),
          maxZoom: 22
        }),
        // 矢量切片服务地址
        tileUrlFunction: function(tileCoord) {
          const url =
            "https://ahocevar.com/geoserver/gwc/service/tms/1.0.0/" +
            "ne:ne_10m_admin_0_countries@EPSG%3A4326@pbf/" +
            (tileCoord[0] - 1) +
            "/" +
            tileCoord[1] +
            "/" +
            (Math.pow(2, tileCoord[0] - 1) - 1 - tileCoord[2]) +
            ".pbf";
          return url;
        }
      }),
      style: country
    });
    let map = new Map({
      target: "map",
      layers: [
        vector4326,
        // 加载瓦片网格图层
        // new TileLayer({
        //   //瓦片网格数据源
        //   source: new TileDebug({
        //     //获取瓦片网格信息
        //     // tileGrid: vector4326.getSource().getTileGrid()
        //   })
        // })
      ],
      controls: [
        new MousePosition({
          coordinateFormat: createStringXY(6) // 鼠标位置
        }) 
      ],
      view: new View({
        projection: "EPSG:4326", // pbf4326格式需要使用4326坐标系
        center: [114.1564839, 24.61548857],
        zoom: 2
      })
    });

效果图如下:
在这里插入图片描述

EPSG:3857坐标系

    let country = new Style({
      stroke: new Stroke({
        color: "rgba(164, 162, 174)",
        width: 2
      }),
      fill: new Fill({
        color: "rgba(233, 229, 216)"
      })
    });

    let vector3857 = new VectorTileLayer({
      declutter: true,
      source: new VectorTileSource({
        maxZoom: 22,
        format: new MVT({
          idProperty: "iso_a3"
        }),
        url:
          "https://ahocevar.com/geoserver/gwc/service/tms/1.0.0/" +
          "ne:ne_10m_admin_0_countries@EPSG%3A900913@pbf/{z}/{x}/{-y}.pbf"
      }),
      style: country
    });
  let map = new Map({
      target: "map",
      layers: [
        vector4326,
        // 加载瓦片网格图层
        // new TileLayer({
        //   //瓦片网格数据源
        //   source: new TileDebug({
        //     //获取瓦片网格信息
        //     tileGrid: vector3857.getSource().getTileGrid()
        //   })
        // })
      ],
      controls: [
        new MousePosition({
          coordinateFormat: createStringXY(6) // 鼠标位置
        }) 
      ],
      view: new View({
        center: [0, 0],
        zoom: 2,
      })
    });

效果图如下:
在这里插入图片描述

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值