openlayers01——使用openlayers展示GeoJSON数据

使用openlayers展示GeoJSON数据

  • 文中包含geojson数据获取、选取坐标、并将坐标作为center和展示geojson

1. 获取数据

1.1 从阿里云获取数据

1.2 将数据保存为GeoJSON数据


2. 选取地图中心坐标

2.1 从osm拾取坐标

  • 这个坐标是WGS 1984,EPSG:4326

说明:OpenLayers中,默认的投影是 SpherealMercator (EPSG: 3857) ,以米作为映射单位。如果想要将拾取的坐标30.4940,114.1692作为center,可以将坐标从 'EPSG:4326'转成'EPSG:3857'

2.2 'EPSG:4326'转成'EPSG:3857'并设为center

import { transform } from 'ol/proj';
const center = [114.1692, 30.494]; //EPSG:4326
const transformedCenter = transform(center, 'EPSG:4326', 'EPSG:3857');
const view = new View({
  center: transformedCenter,
  zoom: 10
});

2.3 在地图上显示GeoJSON中的边界信息

const map = new Map({
  target: 'map',
  layers: [
    new VectorLayer({
        source: new VectorSource({
            format: new GeoJSON(),
            url:'./data/hubei.geojson'
        })
    })
  ],
  view: view
});

完整代码

import './style.css';
import Map from 'ol/Map.js';
import GeoJSON from 'ol/format/GeoJSON.js';
import View from 'ol/View.js';
import VectorLayer from 'ol/layer/vector';
import VectorSource from 'ol/source/Vector';
import { transform } from 'ol/proj';

const center = [114.1692, 30.494]; //EPSG:4326
const transformedCenter = transform(center, 'EPSG:4326', 'EPSG:3857');

const view = new View({
  center: transformedCenter,
  zoom: 10
});

const map = new Map({
  target: 'map',
  layers: [
    new VectorLayer({
        source: new VectorSource({
            format: new GeoJSON(),
            url:'./data/hubei.geojson'
        })
    })
  ],
  view: view
});

最后展示效果

posted @   sheyueyu  阅读(777)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示