前言

记上一篇文章[openlayers系列之绘制标记]

按需引入依赖

1
2
import { Point,LineString  } from "ol/geom";
import { Style, Icon ,Stroke} from "ol/style";

创建线

  1. 需要对应线的数据
1
let lineData = [[113.56762000, 34.82407000], [113.56763000, 34.82408000], [113.56764000, 34.82409000], [113.56765000, 34.82410000]]
  1. 因为openlayers用的是EPSG:3857 需要转换坐标
1
2
3
4
let newlineData = []
lineData.forEach(el => {
                newlineData.push(fromLonLat(el))
            })
  1. 创建feature
1
2
3
 let lineFeature = new Feature({
                geometry: new LineString(newlineData)
            })
  1. 设置线的样式
1
2
3
4
5
6
7
8
9
 let color = 'green';
            let lineStyle = new Style({
                stroke: new Stroke({
                    color: color,
                    width: 4,
                }),
            });
            // 添加线的样式
            lineFeature.setStyle(lineStyle);
  1. 创建source
1
2
3
 let lineSource = new VectorSource({
                features: [lineFeature]
            })
  1. 创建layer
1
2
3
 let lineLayer = new VectorLayer({
                source: lineSource
            })
  1. 加入地图
1
this.map.addLayer(lineLayer);

具体方法

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 addLine() {
            let lineData = [[113.56762000, 34.82407000], [113.56763000, 34.82408000], [113.56764000, 34.82409000], [113.56765000, 34.82410000]]
            let newlineData = []
            lineData.forEach(el => {
                newlineData.push(fromLonLat(el))
            })

            let lineFeature = new Feature({
                geometry: new LineString(newlineData)
            })
            let color = 'green';
            let lineStyle = new Style({
                stroke: new Stroke({
                    color: color,
                    width: 4,
                }),
            });
            // 添加线的样式
            lineFeature.setStyle(lineStyle);

            let lineSource = new VectorSource({
                features: [lineFeature]
            })
            let lineLayer = new VectorLayer({
                source: lineSource
            })

            this.map.addLayer(lineLayer);
        }

实现效果

整体代码如下

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<template>
    <div>
        <div id="map">
        </div>
    </div>
</template>

<script>
import Map from 'ol/Map';
import View from 'ol/View';
import XYZ from 'ol/source/XYZ';
import { Tile as TileLayer } from 'ol/layer';
import { fromLonLat, transform } from "ol/proj";
import { Point, LineString } from "ol/geom";
import { Feature } from "ol";
import { Style, Icon ,Stroke} from "ol/style";
import VectorLayer from "ol/layer/Vector";
import { Vector as VectorSource } from "ol/source";
import { lineString } from '@turf/turf';
export default {
    name: 'HelloWorld',
    data() {
        return {
            map: null,

        }
    },
    mounted() {

        this.init();
    },
    methods: {
        init() {
            let baseLayer = new TileLayer({
                visible: true,
                name: "电子图",
                source: new XYZ({
                    url: 'https://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}',
                    crossOrigin: "anonymous",
                }),
            });
            this.map = new Map({
                target: 'map',
                layers: [baseLayer],
                view: new View({
                    center: fromLonLat([113.56762000, 34.82407000]),
                    zoom: 16
                })
            });

            this.addLine()
        },
        addLine() {
            let lineData = [[113.56762000, 34.82407000], [113.56763000, 34.82408000], [113.56764000, 34.82409000], [113.56765000, 34.82410000]]
            let newlineData = []
            lineData.forEach(el => {
                newlineData.push(fromLonLat(el))
            })

            let lineFeature = new Feature({
                geometry: new LineString(newlineData)
            })
            let color = 'green';
            let lineStyle = new Style({
                stroke: new Stroke({
                    color: color,
                    width: 4,
                }),
            });
            // 添加线的样式
            lineFeature.setStyle(lineStyle);

            let lineSource = new VectorSource({
                features: [lineFeature]
            })
            let lineLayer = new VectorLayer({
                source: lineSource
            })

            this.map.addLayer(lineLayer);
        },
        setPoint() {
            const iconFeature = new Feature({
                geometry: new Point(fromLonLat([113.56762000, 34.82407000])),
            });

            const iconStyle = new Style({
                image: new Icon({
                    anchor: [0.5, 46],
                    anchorXUnits: "fraction",
                    anchorYUnits: "pixels",
                    src: require("@/assets/marker-icon.png"),
                }),
            });

            iconFeature.setStyle(iconStyle);

            let pointLayer = new VectorLayer({
                source: new VectorSource({
                    features: [iconFeature],
                }),
            });

            this.map.addLayer(pointLayer);


        },
        addMark() {
            let feature = new Feature({
                geometry: new Point(fromLonLat([113.56762000, 34.82407000]))
            })

            let mark = new VectorLayer({
                source: new VectorSource({
                    features: [feature]
                }),

            })
            this.map.addLayer(mark)
        }
    }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
#map {
    width: 1920px;
    height: 1080px;
}
</style>