1 Star 2 Fork 0

houzw / openlayers5-demos

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Openlayers 实例

https://openlayers.org/en/latest/doc/tutorials/bundle.html

第三方库

项目初始化

  • 新建文件夹,并进入: mkdir ol-demos && cd ol-demos
  • 初始化项目,自动创建:package.json文件
  • 安装最新版 ol: npm i -S ol

推荐使用淘宝源

  • 安装项目打包工具:npm i -D parcel-bundler

parcel-bundler 的功能与webpack类似,都是资源打包压缩、代码转换、热模块替换等等

编写代码

  • index.js
  • index.html

编译运行

  • 修改package.json 中内容。
    "scripts": {
      "test": "echo \"Error: no test specified\" && exit 1",
      "start": "parcel index.html",
      "build": "parcel build --public-url . index.html"
    },
  • 命令行中运行:npm start 启动开发服务器
  • 浏览器中访问地址:http://localhost:1234
  • 命令行中使用快捷键ctrl+c结束开发服务器

交互式绘制矢量图形

普通的点线面图形,通过设置type为相应的要素类型,如Polygon,即可由用户自行绘制(ol/interaction/Draw)。 规则图形:https://openlayers.org/en/latest/examples/draw-shapes.html?q=freehand 使用DrawgeometryFunction 选项。

  • 正方形是边为4的正多边形。因此,绘制正方形时,Draw 的 type选项为Circle,同时 geometryFunction = createRegularPolygon(4);;
  • 绘制长方形时,Draw 的 type选项同样为Circle,但 geometryFunction = createBox();;
  • 其他的特殊多边形,例如五角星等,需要根据其与circle 的中心坐标及角度来关系绘制。 按住Shift键自由绘制图形

坐标与投影

ProjectionLike字符串:类似"EPSG:XXXX"

ol.proj模块

主要提供坐标系的转换等操作,默认是4326和3857的转换。

  • proj.addCoordinateTransforms():添加地理转换方法
  • proj.fromLonLat():将经纬度坐标转换到目标坐标系的坐标(默认4326到3857)
  • proj.get():根据给定ProjectionLike字符串获取Projection对象
  • proj.getTransform():获取地理转换方法
  • proj.toLonLat():和fromLonLat是反运算
  • proj.transform():转换坐标值,根据给定的原坐标系和目标坐标系,有可能需要地理转换
  • proj.transformExtent():转换矩形边界框,参数同transform

转换要素

feature.getGeometry().transform('EPSG:3857', 'EPSG:4326')

转换坐标

import {Projection,transform} from 'ol/proj'
//transform(coordinate, source, destination)
transform([x, y], new Projection("EPSG:4326"), new Projection("EPSG:3857"));

转换范围的投影

transformExtent(extent, source, destination)
MIT License Copyright (c) 2020 houzw Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

练习 openlayers 5 展开 收起
JavaScript
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
JavaScript
1
https://gitee.com/vtech/openlayers5-demos.git
git@gitee.com:vtech/openlayers5-demos.git
vtech
openlayers5-demos
openlayers5-demos
master

搜索帮助