OpenLayers画点、画圆、画线方法

准备工作

项目搭建

创建Vue项目

vue create OpenLayers 

安装openlayers包,在package,.json中添加

"dependencies": {
    "core-js": "^3.8.3",
    "vue": "^3.2.13",
    "vue-router": "^4.0.3",
    "ol": "^6.4.3" 👈添加这个
  },

命令行安装

npm i 

画地图

导入要用的包

import 'ol/ol.css';
import Map from 'ol/Map.js';
import OSM from 'ol/source/OSM';
import TileLayer from 'ol/layer/Tile';
import View from 'ol/View';

HTML代码

<template>
  <div class="mainDiv">
    <div id="map" class="map"></div>
    <div @click="add">让我在看你一</div>
</template>




JS代码

导包👇导包👇导包👇
import 'ol/ol.css';
import Map from 'ol/Map.js';
import OSM from 'ol/source/OSM';
import TileLayer from 'ol/layer/Tile';
import View from 'ol/View';

画地图👇画地图👇画地图👇
mounted() {
    this.map = new Map({
      layers: [
        new TileLayer({
          source: new OSM(),
        }),
      ],
      target: 'map',
      view: new View({
        projection: "EPSG:4326",// 👈 这里很关键 EPSG:4326 是我们常用的经纬度坐标 决定着之后画点 画图的位置
        center: [0, 0],
        zoom: 2,
      }),
    });
  },

CSS代码

.mainDiv {
  height: 600px;
  /*height: 1800px;*/
  width: 100%;
}

.map {
  width: 100%;
  height: 100%;
}

画圆

导包

import {Vector as VectorLayer} from "ol/layer";
import {Vector as SourceLayer} from "ol/source";
import Feature from 'ol/Feature';
import {Circle} from 'ol/geom';

新建图层

circleLayer: new VectorLayer({ // 源于"ol/layer"
  // softMove()内的时间容器
  source: new SourceLayer(), // 源于"ol/source"
})

开始画圆儿

var circle = new Circle([0, 0], 20);// 新建圆对象 
var feature = new Feature(circle);// 新建Feature对象 并将circle传入
this.circleLayer.getSource().addFeature(feature)// 将Feature对象填入图层源
this.map.addLayer(this.circleLayer) // 将图层添至地图对象

效果图

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-H7ewvLpr-1646275644071)(C:\Users\dancerHoan\AppData\Roaming\Typora\typora-user-images\image-20220303100335862.png)]

更改Style

import {Stroke, Style, Icon, Text, Fill} from "ol/style";
import {Vector as VectorLayer} from "ol/layer";
import {Vector as SourceLayer} from "ol/source";
import Feature from 'ol/Feature';
import {LineString} from 'ol/geom';
import {Circle} from 'ol/geom';
import {Point} from 'ol/geom';
import {Stroke, Style, Icon, Text, Fill} from "ol/style";


import myImg from "../assets/logo.png"
const Log = document.createElement("img");
Log.src = myImg;
// 👆导入图片源

var style = new Style({
  fill: new Fill({ //填充颜色
    color: "rgba(255,0,0,0.1)"
  }),
  stroke: new Stroke({ // 边缘颜色
    color: "rgba(255,152,0)",
    width: 5
  }),
  text: new Text({ // 设置字体
    fill: new Fill({ // 字体颜色
      color: "rgba(255,0,255)",
    }),
    font: "20px sans-serif", // 字体样式
    text: "让我再看你一眼,从南到北", // 字体内容
    backgroundStroke: new Stroke({ // 字体外框颜色
      width: .1,
    }),
    // backgroundFill: new Fill({
    //   color: "rgba(0, 0,0,0.5)",
    // }),
    scale: [0.7, 0.7],
    // padding: [1, 11, 11, 1],
    // offsetY: 15,
    // offsetX: -10,
  }),
  image: new Icon({// 图像 但是要注意 图像有些Feature不能用 如circle
    img: Log, // 图像的源
    imgSize: [500, 500],
    // scale: [1,1],//图的大小
    // rotateWithView: true,
    // opacity: 0.2,
    // offset: [-10, -10],// 偏离度
    // rotation: -this.rotation
  }),
});
feature.setStyle(style)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YyN5ycwW-1646275644072)(C:\Users\dancerHoan\AppData\Roaming\Typora\typora-user-images\image-20220303104037059.png)]

画点

导包

import {Vector as VectorLayer} from "ol/layer";
import {Vector as SourceLayer} from "ol/source";
import Feature from 'ol/Feature';
import {Point} from 'ol/geom';

画点

var point = new Point([20, 20]);
var feature1 = new Feature(point);
this.Layer.getSource().addFeature(feature1)
this.map.addLayer(this.Layer)

画线

导包

import {Vector as VectorLayer} from "ol/layer";
import {Vector as SourceLayer} from "ol/source";
import Feature from 'ol/Feature';
import {LineString} from 'ol/geom';

画线

var line = [[0, 0],[10,10],[20,20]]
var lineString = new LineString(line);
var feature2 = new Feature(lineString);
this.Layer.getSource().addFeature(feature2)
this.map.addLayer(this.Layer)

在这里插入图片描述

  • 4
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
OpenLayers是一个强大的开源JavaScript库,用于构建交互式的Web地图应用。要使用OpenLayers在地图上画一条线(Polyline),你可以按照以下步骤操作: 1. 引入OpenLayers库:首先在HTML文件中引入OpenLayers的CDN链接,或者本地包含其文件。 ```html <script src="https://cdn.jsdelivr.net/npm/openlayers@6.6.1/dist/OpenLayers.js"></script> ``` 2. 初始化地图:创建一个`<div>`容器,并设置地图的视口大小。 ```html <div id="map" style="width: 100%; height: 500px;"></div> ``` 3. 创建地图实例:在JavaScript中,初始化地图并设置投影和分辨率。 ```javascript const map = new ol.Map({ target: 'map', view: new ol.View({ center: [0, 0], // 设置中心点 zoom: 2, // 设置初始缩放级别 projection: 'EPSG:4326', // 使用WGS84坐标系 }), }); ``` 4. 添加在线层或地形数据:通常我们会添加一个WMS或TMS服务作为底图。 ```javascript const layer = new ol.layer.Tile({ source: new ol.source.OSM(), // 或者使用其他在线地图服务 }); map.addLayer(layer); ``` 5. 创建线条矢量层:使用`ol.format.GeoJSON`将线条数据转换为OpenLayers可以理解的格式。 ```javascript const lineFeature = { type: 'Feature', geometry: { type: 'LineString', coordinates: [[0, 0], [10, 10]], // 线条起点和终点坐标 }, }; const lineSource = new ol.source.Vector({ url: 'data/lines.geojson', // 如果是本地文件,路径改为本地 format: new ol.format.GeoJSON(), features: [ol.format.GeoJSON.writeFeature(lineFeature)], }); const lineLayer = new ol.layer.Vector({ source: lineSource, style: new ol.style.Stroke({ color: 'blue', width: 2, }), }); map.addLayer(lineLayer); ```
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值