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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
<template>
<view>
<view id="map" ref="rootmap"></view>
<view style="display: flex;">
<button @click="moveView">moveView</button>
<button @click="fitToChengdu">fitToChengdu</button>
</view>
</view>
</template>
<script module="ol" lang="renderjs">
import 'ol/ol.css';
import Map from 'ol/Map';
import View from 'ol/View';
import {Vector as VectorLayer,Tile as TileLayer} from 'ol/layer';
import {Vector as VectorSource,OSM,XYZ} from 'ol/source';
import {GeoJSON} from 'ol/format';
import {bbox} from 'ol/loadingstrategy'
import {Style,Stroke,Circle,Fill} from 'ol/style';
import {fromLonLat} from 'ol/proj'
var map = null
export default {
name: 'OlWFS',
data() {
return {
};
},
onReady() {
this.loadMap()
},
methods:{
loadMap(){
//创建wfs资源
let wfsVectorSource = new VectorSource({
format: new GeoJSON(),
projection: 'EPSG:4326',
url: '/api/maptest/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=maptest%3Aqunaer_data_final&maxFeatures=500&outputFormat=application%2Fjson',
strategy: bbox
});
let wfsVectorSourcePolygon = new VectorSource({
format: new GeoJSON(),
projection: 'EPSG:4326',
url: '/api/maptest/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=maptest%3Achina_province&maxFeatures=50&outputFormat=application%2Fjson',
strategy: bbox
});
//创建wfs图层,注意需要设置好描边样式,否则不展示效果出来
let wfsVectorLayer = new VectorLayer({
source: wfsVectorSource,
style: new Style({
image: new Circle({
radius: 5,
fill: new Fill({
color: "#3885ff",
opacity: 0.5
})
}),
stroke: new Stroke({
color: 'blue',
width: 5
})
}),
visible: true
});
//view设置
let view = new View({
// center: fromLonLat([105, 34],'EPSG:3857'),
center: [105, 34],
zoom: 5,
maxZoom: 18,
minZoom: 5,
projection: 'EPSG:4326'
});
//创建一个map
map = new Map({
layers: [
new TileLayer({
// source: new OSM() //这个会出现底图
source: new XYZ({
url: 'http://{a-c}.tile.openstreetmap.org/{z}/{x}/{y}.png'
})
}),
wfsVectorLayer
],
target: "map",
view: view
});
map.on('click', function(evt) {
console.log("evt:",evt.pixel)
// displayFeatureInfo(evt.pixel);
var feature = map.forEachFeatureAtPixel(evt.pixel, function(feature) {
return feature;
});
console.log("feature:",feature)
if(feature != null){
console.log("feature getId:",feature.getId())
console.log("feature getKeys:",feature.getKeys())
console.log("feature getProperties:",feature.getProperties())
}
});
},
// 向左移动地图
moveView(){
var view = map.getView();
var mapCenter = view.getCenter();
// 让地图中心的x值增加,即可使得地图向左移动,增加的值根据效果可自由设定
console.log("mapCenter:",mapCenter)
mapCenter[0] += 50000;
view.setCenter(mapCenter);
map.render();
},
fitToChengdu() {
// 让地图最大化完全地显示区域[104, 30.6, 104.12, 30.74]
map.getView().fit([104, 30.6, 104.12, 30.74], map.getSize());
},
}
};
</script>
<style>
#map{
width: 100%;
height: 90vh;
}
/*隐藏ol的一些自带元素*/
.ol-attribution,
.ol-zoom {
display: none;
}
</style>
|