OpenLayers的基础操作

介绍

OpenLayers是一个开源的JavaScript库,专门用于在Web网页上浏览和交互地图。它提供了丰富的地图功能和交互工具,使得开发者能够轻松地创建出功能强大的WebGIS系统。OpenLayers的核心概念包括Map, View, Control, Layer和Source。Map代表地图的容器,View负责地图的视图管理,Controls是地图控件的集合,Layer代表了地图上的不同图层,Source则是图层的数据源。

图层管理
import TileLayer from "ol/layer/WebGLTile.js";
import XYZ from "ol/source/XYZ";
// 创建图层
const layer = new TileLayer({
        source: new XYZ({
          url: "http://localhost:8080/map/{z}/{x}/{y}.jpg",
        }),
        name: "map",
      });
// 切换图层
function toggleLayer(layer, isVisible) {
        layer.setVisible(isVisible);
      }
初始化
import TileLayer from "ol/layer/WebGLTile.js";
import XYZ from "ol/source/XYZ";
import Map from "ol/Map.js";
import View from "ol/View";
import { fromLonLat, toLonLat } from "ol/proj";
import {
  Control,
  defaults,
  FullScreen,
  MousePosition,
  ScaleLine,
  ZoomSlider,
} from "ol/control";
import { createStringXY } from "ol/coordinate";
// 创建图层
const layer = new TileLayer({
        source: new XYZ({
          url: "http://localhost:8080/map/{z}/{x}/{y}.jpg",
        }),
        name: "map",
      });
// 创建地图容器
const map = new Map({
        target: "map",
        layers: [layer],
        view: new View({
          center: fromLonLat([110.92433, 35.96646]),
          zoom: 5,
        }),
      });
地图控件
import TileLayer from "ol/layer/WebGLTile.js";
import XYZ from "ol/source/XYZ";
import Map from "ol/Map.js";
import View from "ol/View";
import { fromLonLat, toLonLat } from "ol/proj";
import {
  Control,
  defaults,
  FullScreen,
  MousePosition,
  ScaleLine,
  ZoomSlider,
} from "ol/control";
import { createStringXY } from "ol/coordinate";
// 创建图层
const layer = new TileLayer({
        source: new XYZ({
          url: "http://localhost:8080/map/{z}/{x}/{y}.jpg",
        }),
        name: "map",
      });
// 创建地图容器
const map = new Map({
        target: "map",
        layers: [layer],
        view: new View({
          center: fromLonLat([110.92433, 35.96646]),
          zoom: 5,
        }),
        // 添加地图控件
        controls: defaults({ zoom: false }).extend([
          new FullScreen(),
          new ZoomSlider(),
          new ScaleLine(),
          new MousePosition({
            coordinateFormat: createStringXY(4),
            projection: "EPSG:4326", // 设置显示坐标的投影为经纬度
            // target: document.getElementById("mouse-position"), // 设置控件的目标容器
            undefinedHTML: " ",
          }),
        ]),
      });
获取坐标
// 获取地图坐标
map.on("click", (event) => {
        const coordinate = event.coordinate; // 点击位置的地理坐标
        const lonlat = toLonLat(coordinate); // 转换为经纬度坐标
        console.log("Clicked Coordinate:", lonlat);
      });
跳到中心点
// 移动到中心点
jumpCenter() {
      var targetCenter = fromLonLat([139.840345625, 35.73493231197516]);
      map.getView().animate({
        center: targetCenter,
        duration: 1000,
        zoom: 6,
      });
    },
完整代码 
import TileLayer from "ol/layer/WebGLTile.js";
import XYZ from "ol/source/XYZ";
import Map from "ol/Map.js";
import View from "ol/View";
import { fromLonLat, toLonLat } from "ol/proj";
import {
  Control,
  defaults,
  FullScreen,
  MousePosition,
  ScaleLine,
  ZoomSlider,
} from "ol/control";
import { createStringXY } from "ol/coordinate";
// 创建图层
const layer = new TileLayer({
        source: new XYZ({
          url: "http://localhost:8080/map/{z}/{x}/{y}.jpg",
        }),
        name: "map",
      });
// 创建地图容器
const map = new Map({
        target: "map",
        layers: [layer],
        view: new View({
          center: fromLonLat([110.92433, 35.96646]),
          zoom: 5,
        }),
        // 添加地图控件
        controls: defaults({ zoom: false }).extend([
          new FullScreen(),
          new ZoomSlider(),
          new ScaleLine(),
          new MousePosition({
            coordinateFormat: createStringXY(4),
            projection: "EPSG:4326", // 设置显示坐标的投影为经纬度
            // target: document.getElementById("mouse-position"), // 设置控件的目标容器
            undefinedHTML: " ",
          }),
        ]),
      });
// 切换图层
function toggleLayer(layer, isVisible) {
        layer.setVisible(isVisible);
      }
// 获取地图坐标
map.on("click", (event) => {
        const coordinate = event.coordinate; // 点击位置的地理坐标
        const lonlat = toLonLat(coordinate); // 转换为经纬度坐标
        console.log("Clicked Coordinate:", lonlat);
      });
// 移动到中心点
jumpCenter() {
      var targetCenter = fromLonLat([139.840345625, 35.73493231197516]);
      map.getView().animate({
        center: targetCenter,
        duration: 1000,
        zoom: 6,
      });
    },
  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
OpenLayers是一个在web上使用JavaScript编写的开源地图库,用于在网站或应用程序中显示、交互和分析地理信息。它可以用于绘制各种地图类型,包括基础底图、矢量数据和栅格图像等。OpenLayers提供了丰富的功能和工具,使用户能够自定义地图显示方式,并与地图进行交互。 在OpenLayers中进行绘图,可以通过使用Vector图层来实现。Vector图层可以用于绘制各种几何要素,包括点、线和面。它支持绘制、编辑、删除和选择要素等操作。用户可以使用鼠标或触摸屏进行绘制操作,并可以设置不同的样式和符号来表示要素的外观。 绘图的过程中,用户可以选择不同的绘制工具来绘制不同类型的要素。例如,可以使用点工具在地图上添加点要素,线工具绘制直线或折线,面工具绘制多边形等。除了基本的绘图功能,OpenLayers还提供了一些高级功能,如绘制方向箭头、文本标注、海量点等。 在绘图过程中,用户还可以对已绘制的要素进行编辑和删除操作。编辑功能包括移动、旋转、缩放和修改要素的形状等。删除操作可以将不需要的要素从地图上移除。 总之,OpenLayers提供了强大的绘图功能,可用于在web地图中添加各种几何要素。它不仅可以满足用户在地图展示和分析方面的需求,还可以根据具体应用场景进行定制和扩展。对于开发人员和地理信息系统专业人员来说,OpenLayers是一个非常有用且灵活的工具。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值