[openlayers07]_加载天地图为底图并展示GeoJSON

[openlayers07]_加载天地图为底图并展示GeoJSON

1. 加载底图(得申请key)

天地图

image-20230207195245905

2. 创建地图、设置center和投影

// 创建地图
const center = [114.1692, 30.494]; //EPSG:4326
const view = new View({
  center: center, //EPSG:4326
   projection: 'EPSG:4326',
  zoom: 10
})
const map =new Map({
  target: 'map-container',
  view: view,
});

3. 以天地图为底图——影像底图(图层:img_w)

// 以天地图为底图——影像底图(地图瓦片获取)
let img_w_url = "http://t0.tianditu.gov.cn/img_w/wmts?" +
    "SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles" +
    "&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=你的key";
let img_w = new TileLayer({
    source: new XYZ({
        url: img_w_url
    }) 
});
  • 记得替换key

4. 展示GeoJSON数据(图层:layerGeoJson)

// ---------------------------------
// 矢量源
const sourceGeoJson = new VectorSource({
  format: new GeoJSON(),
  url:'./data/hubei.geojson'
});
// 根据矢量源构建图层  layerGeoJson
const layerGeoJson = new VectorLayer({
  source:sourceGeoJson,
});

5. 将两个图层加入map

map.addLayer(img_w); //底图
map.addLayer(layerGeoJson); //由geojson数据构建的图层
  • 注意顺序

6. 效果

image-20230207195742375

完整代码:

main.js

import OSM from 'ol/source/OSM';
import XYZ from 'ol/source/XYZ';
import TileLayer from 'ol/layer/Tile';
import {Map, View} from 'ol';
import VectorLayer from 'ol/layer/vector';
import VectorSource from 'ol/source/Vector';
import GeoJSON from 'ol/format/GeoJSON.js';

// 创建地图
const center = [114.1692, 30.494]; //EPSG:4326
const view = new View({
  center: center, //EPSG:4326
   projection: 'EPSG:4326',
  zoom: 10
})
const map =new Map({
  target: 'map-container',
  view: view,
});

// 以天地图为底图——影像底图(地图瓦片获取)
let img_w_url = "http://t0.tianditu.gov.cn/img_w/wmts?" +
    "SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles" +
    "&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=xxxx";
let img_w = new TileLayer({
    source: new XYZ({
        url: img_w_url
    }) //
});
// ---------------------------------
// 矢量源
const sourceGeoJson = new VectorSource({
  format: new GeoJSON(),
  url:'./data/hubei.geojson'
});
// 根据矢量源构建图层  layerGeoJson
const layerGeoJson = new VectorLayer({
  source:sourceGeoJson,
});


map.addLayer(img_w); //底图
map.addLayer(layerGeoJson); //由geojson数据构建的图层

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>OpenLayers</title>
    <style>
      @import "node_modules/ol/ol.css";
    </style>
    <style>
      html, body, #map-container {
        margin: 0;
        height: 100%;
        width: 100%;
        font-family: sans-serif;
      }
    </style>
  </head>
  <body>
    <div id="map-container"></div>
    <script src="./main.js" type="module"></script>
  </body>
</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值