• openlayers 实例教程分享


    OpenLayers3示例   

    记录一个openlayers 实例教程网址

    1、WebGIS简介 · 语雀

    记录一个openlayers入门教程

    地图坐标系转换 - Online Tools 

    记录一个国家2000坐标转换工具的方法

    记录一次我使用openlayers 开发地图的实例,实现了打点,弹窗,聚合

    1. <script>
    2. import 'ol/ol.css'
    3. import TileLayer from 'ol/layer/Tile'
    4. import VectorLayer from 'ol/layer/Vector'
    5. import VectorSource from 'ol/source/Vector'
    6. import XYZ from 'ol/source/XYZ'
    7. import { Map, View, Feature, Overlay } from 'ol'
    8. import { Style, Fill, Stroke, Icon, Text } from 'ol/style'
    9. import { Point } from 'ol/geom'
    10. import { defaults as defaultControls, ZoomSlider } from 'ol/control'
    11. import { fromLonLat, toLonLat } from 'ol/proj'
    12. import doumen from '@/assets/mapdata/doumen.json'
    13. import liuchuan from '@/assets/mapdata/zong_pipe.json'
    14. import allMap from '@/assets/mapdata/allMap.json'
    15. import douqu from '@/assets/mapdata/zong_canal.json'
    16. import branch_pipe from '@/assets/mapdata/branch_pipe.json'
    17. import main_pipe from '@/assets/mapdata/main_pipe.json'
    18. import shank_pipe from '@/assets/mapdata/shank_pipe.json'
    19. import side_pipe from '@/assets/mapdata/side_pipe.json'
    20. import zhiqu from '@/assets/mapdata/shushuiquxi_zhiqu.json'
    21. import douguan from '@/assets/mapdata/douguan.json'
    22. import zhiguan from '@/assets/mapdata/guandao_zhiguan.json';
    23. import ganguan from '@/assets/mapdata/guandao_ganguan.json';
    24. import shushuidouqu from '@/assets/mapdata/shushuiquxi_douqu.json'
    25. import { benzhanList, szyInfo, modbusInfo } from '@/api/first/benzhan'
    26. import GeoJSON from 'ol/format/GeoJSON'
    27. import Cluster from 'ol/source/Cluster.js'
    28. // 边界json数据
    29. export default {
    30. data() {
    31. return {
    32. map: null,
    33. zmName: '',
    34. ssLiuLiang: '',
    35. leiJi: '',
    36. zhaMen: '',
    37. pointLayer: null,
    38. pointLayer1: null,
    39. featuresArr: [],
    40. featuresArr1: [],
    41. shopPopup: false,
    42. popup: null,
    43. atzoom: '', // 当前缩放比例
    44. clusterSource: null // 聚合要素
    45. }
    46. },
    47. methods: {
    48. /**
    49. * 初始化地图
    50. */
    51. initMap() {
    52. this.map = new Map({
    53. target: 'Map',
    54. controls: defaultControls({
    55. zoom: true
    56. // 添加鹰眼图的控件
    57. }).extend([]),
    58. layers: [
    59. new TileLayer({
    60. source: new XYZ({
    61. url:
    62. 'http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}'
    63. })
    64. })
    65. ],
    66. view: new View({
    67. center: fromLonLat([104.4143, 36.6604]),
    68. zoom: 13,
    69. maxZoom: 19,
    70. minZoom: 4
    71. })
    72. })
    73. this.map.addControl(new ZoomSlider())
    74. this.map.on("singleclick", evt => {
    75. var position = evt.coordinate;
    76. console.log(position);
    77. this.clickMap(evt);
    78. })
    79. },
    80. /**
    81. * 白银市地理数据
    82. */
    83. setBackColor() {
    84. const features = (new GeoJSON({ featureProjection: 'EPSG:3857' })).readFeatures(allMap)
    85. const vectorSource = new VectorSource({ features: features })
    86. // 白银市地理数据
    87. const lineLayer = new VectorLayer({
    88. zIndex: 0,
    89. source: vectorSource
    90. })
    91. lineLayer.setStyle(
    92. new Style({
    93. fill: new Fill({ color: 'rgba(38,60,217,0.5)' }),
    94. stroke: new Stroke({
    95. width: 1,
    96. color: [71, 137, 227, 1]
    97. })
    98. })
    99. )
    100. this.map.addLayer(lineLayer)
    101. },
    102. /**
    103. * 加载刘川灌区
    104. */
    105. // 加载刘川地理数据
    106. addGeoJson() {
    107. const features = (new GeoJSON({ featureProjection: 'EPSG:3857' })).readFeatures(liuchuan)
    108. const vectorSource = new VectorSource({ features: features })
    109. // 设置矢量数据源
    110. const lineLayer = new VectorLayer({
    111. zIndex: 1,
    112. source: vectorSource
    113. })
    114. const style = new Style({
    115. stroke: new Stroke({
    116. width: 2,
    117. color: '#fff'
    118. }),
    119. fill: new Fill({
    120. color: '#B9D9EB44'
    121. })
    122. })
    123. lineLayer.setStyle(style)
    124. this.map.addLayer(lineLayer) // 把图层添加到地图
    125. },
    126. /**
    127. * 加载刘川斗渠数据
    128. */
    129. addGeoJsonone() {
    130. const features = (new GeoJSON({ featureProjection: 'EPSG:3857' })).readFeatures(douqu)
    131. // 设置矢量数据源
    132. const vectorSource = new VectorSource({ features: features })
    133. const lineLayer = new VectorLayer({
    134. zIndex: 1,
    135. source: vectorSource
    136. })
    137. this.map.addLayer(lineLayer) // 把图层添加到地图
    138. // 循环添加feature
    139. const source = lineLayer.getSource().getFeatures()
    140. for (let i = 0; i < source.length; i++) {
    141. source[i].setStyle(this.setText(source[i].values_.NAME))
    142. }
    143. },
    144. /**
    145. * 加载支渠
    146. */
    147. addZhiqu() {
    148. const features = (new GeoJSON({ featureProjection: 'EPSG:3857' })).readFeatures(zhiqu)
    149. // 设置矢量数据源
    150. const vectorSource = new VectorSource({ features: features })
    151. const lineLayer = new VectorLayer({
    152. zIndex: 1,
    153. source: vectorSource
    154. })
    155. this.map.addLayer(lineLayer) // 把图层添加到地图
    156. // 循环添加feature
    157. const source = lineLayer.getSource().getFeatures()
    158. for (let i = 0; i < source.length; i++) {
    159. source[i].setStyle(this.setText(source[i].values_.NAME))
    160. }
    161. },
    162. /**
    163. * 加载斗管
    164. */
    165. addDouguan() {
    166. const features = (new GeoJSON({ featureProjection: 'EPSG:3857' })).readFeatures(douguan)
    167. // 设置矢量数据源
    168. const vectorSource = new VectorSource({ features: features })
    169. const lineLayer = new VectorLayer({
    170. zIndex: 1,
    171. source: vectorSource
    172. })
    173. this.map.addLayer(lineLayer) // 把图层添加到地图
    174. // 循环添加feature
    175. const source = lineLayer.getSource().getFeatures()
    176. for (let i = 0; i < source.length; i++) {
    177. source[i].setStyle(this.setText(source[i].values_.NAME))
    178. }
    179. },
    180. /**
    181. * 加载只管
    182. */
    183. addZhiguan() {
    184. const features = (new GeoJSON({ featureProjection: 'EPSG:3857' })).readFeatures(zhiguan)
    185. // 设置矢量数据源
    186. const vectorSource = new VectorSource({ features: features })
    187. const lineLayer = new VectorLayer({
    188. zIndex: 1,
    189. source: vectorSource
    190. })
    191. this.map.addLayer(lineLayer) // 把图层添加到地图
    192. // 循环添加feature
    193. const source = lineLayer.getSource().getFeatures()
    194. for (let i = 0; i < source.length; i++) {
    195. source[i].setStyle(this.setText(source[i].values_.NAME))
    196. }
    197. },
    198. /**
    199. * 加载干管
    200. */
    201. addGanGuan() {
    202. const features = (new GeoJSON({ featureProjection: 'EPSG:3857' })).readFeatures(ganguan)
    203. // 设置矢量数据源
    204. const vectorSource = new VectorSource({ features: features })
    205. const lineLayer = new VectorLayer({
    206. zIndex: 1,
    207. source: vectorSource
    208. })
    209. this.map.addLayer(lineLayer) // 把图层添加到地图
    210. // 循环添加feature
    211. const source = lineLayer.getSource().getFeatures()
    212. for (let i = 0; i < source.length; i++) {
    213. source[i].setStyle(this.setText(source[i].values_.NAME))
    214. }
    215. },
    216. // 加载输水斗渠
    217. addShushuiDouqu() {
    218. const features = (new GeoJSON({ featureProjection: 'EPSG:3857' })).readFeatures(shushuidouqu)
    219. // 设置矢量数据源
    220. const vectorSource = new VectorSource({ features: features })
    221. const lineLayer = new VectorLayer({
    222. zIndex: 1,
    223. source: vectorSource
    224. })
    225. this.map.addLayer(lineLayer) // 把图层添加到地图
    226. // 循环添加feature
    227. const source = lineLayer.getSource().getFeatures()
    228. for (let i = 0; i < source.length; i++) {
    229. source[i].setStyle(this.setText(source[i].values_.NAME))
    230. }
    231. },
    232. //
    233. addGeoone() {
    234. const features = (new GeoJSON({ featureProjection: 'EPSG:3857' })).readFeatures(branch_pipe)
    235. // 设置矢量数据源
    236. const vectorSource = new VectorSource({ features: features })
    237. const lineLayer = new VectorLayer({
    238. zIndex: 1,
    239. source: vectorSource
    240. })
    241. this.map.addLayer(lineLayer) // 把图层添加到地图
    242. // 循环添加feature
    243. const source = lineLayer.getSource().getFeatures()
    244. for (let i = 0; i < source.length; i++) {
    245. source[i].setStyle(this.setText(source[i].values_.NAME))
    246. }
    247. },
    248. main_pipe() {
    249. const features = (new GeoJSON({ featureProjection: 'EPSG:3857' })).readFeatures(main_pipe)
    250. // 设置矢量数据源
    251. const vectorSource = new VectorSource({ features: features })
    252. const lineLayer = new VectorLayer({
    253. zIndex: 1,
    254. source: vectorSource
    255. })
    256. this.map.addLayer(lineLayer) // 把图层添加到地图
    257. // 循环添加feature
    258. const source = lineLayer.getSource().getFeatures()
    259. for (let i = 0; i < source.length; i++) {
    260. source[i].setStyle(this.setText(source[i].values_.NAME))
    261. }
    262. },
    263. side_pipe() {
    264. const features = (new GeoJSON({ featureProjection: 'EPSG:3857' })).readFeatures(side_pipe)
    265. // 设置矢量数据源
    266. const vectorSource = new VectorSource({ features: features })
    267. const lineLayer = new VectorLayer({
    268. zIndex: 1,
    269. source: vectorSource
    270. })
    271. this.map.addLayer(lineLayer) // 把图层添加到地图
    272. // 循环添加feature
    273. const source = lineLayer.getSource().getFeatures()
    274. for (let i = 0; i < source.length; i++) {
    275. source[i].setStyle(this.setText(source[i].values_.NAME))
    276. }
    277. },
    278. shank_pipe() {
    279. const features = (new GeoJSON({ featureProjection: 'EPSG:3857' })).readFeatures(shank_pipe)
    280. // 设置矢量数据源
    281. const vectorSource = new VectorSource({ features: features })
    282. const lineLayer = new VectorLayer({
    283. zIndex: 1,
    284. source: vectorSource
    285. })
    286. this.map.addLayer(lineLayer) // 把图层添加到地图
    287. // 循环添加feature
    288. const source = lineLayer.getSource().getFeatures()
    289. for (let i = 0; i < source.length; i++) {
    290. source[i].setStyle(this.setText(source[i].values_.NAME))
    291. }
    292. },
    293. // 文字标注的样式
    294. setText(name) {
    295. const style = new Style({
    296. stroke: new Stroke({
    297. width: 3,
    298. color: '#FFEBEE'
    299. }),
    300. text: new Text({
    301. font: '20px',
    302. // 对其方式
    303. textAlign: 'center',
    304. // 基准线
    305. textBaseline: 'middle',
    306. offsetY: -35,
    307. offsetX: 0,
    308. // backgroundFill: new Stroke({
    309. // color: 'rgba(0,123,255,0.8)'
    310. // }),
    311. // 文本填充样式
    312. fill: new Fill({
    313. color: 'rgba(236,218,20,1)'
    314. }),
    315. padding: [5, 5, 5, 5],
    316. text: `${name}`
    317. })
    318. })
    319. return style
    320. },
    321. /**
    322. * 批量根据经纬度坐标打点 斗门
    323. */
    324. addPoints(coordinates) {
    325. console.log('打点坐标调试', coordinates);
    326. // 创建一个 VectorSource 对象
    327. const source = new VectorSource()
    328. // 创建一个 Cluster 对象
    329. const clusters = new Cluster({
    330. distance: 60,
    331. source: source
    332. })
    333. // 循环添加 feature
    334. for (let i = 0; i < coordinates.length; i++) {
    335. // 创建 feature,一个 feature 就是一个点坐标信息
    336. const feature = new Feature({
    337. geometry: new Point(fromLonLat([coordinates[i].x, coordinates[i].y])),
    338. })
    339. feature.set("moreInfo", coordinates[i].moreInfo)
    340. // 设置图层
    341. this.pointLayer = new VectorLayer({
    342. source: clusters, // 使用之前创建的 VectorSource 对象作为图层的源
    343. zIndex: 1,
    344. style: this.getIcon(coordinates[i].id, coordinates[i].name)
    345. })
    346. //console.log('本站名', coordinates[i].name);
    347. // 将 feature 添加到 source 中
    348. source.addFeature(feature)
    349. }
    350. this.map.addLayer(this.pointLayer)
    351. },
    352. /**
    353. * 添加图标和文字
    354. */
    355. getIcon(type, name) {
    356. let src = ''
    357. type > 95
    358. ? (src = require('../../assets/zhamen_run.png'))
    359. : (src = require('../../assets/zhamen_stop.png'))
    360. const styleIcon = new Style({
    361. // 设置图片效果
    362. image: new Icon({
    363. // scale: 0.15 * (this.atzoom - 8),
    364. width: 25,
    365. src: src,
    366. anchor: [1, 1]
    367. }),
    368. text: new Text({
    369. // 对其方式
    370. textAlign: 'center',
    371. // 基准线
    372. textBaseline: 'middle',
    373. offsetY: -35,
    374. offsetX: 0,
    375. backgroundFill: new Stroke({
    376. color: 'rgba(0,0,255,0.4)'
    377. }),
    378. // 文本填充样式
    379. fill: new Fill({
    380. color: 'rgba(236,218,20,1)'
    381. }),
    382. padding: [5, 5, 5, 5],
    383. })
    384. })
    385. return styleIcon
    386. },
    387. /**
    388. * 弹窗
    389. */
    390. addOverlay() {
    391. // 创建Overlay, 覆盖层
    392. const elPopup = this.$refs.popup
    393. this.popup = new Overlay({
    394. element: elPopup,
    395. autoPan: true,
    396. positioning: 'bottom-center',
    397. stopEvent: false,
    398. offset: [-10, -25],
    399. autoPanAnimation: {
    400. duration: 250
    401. }
    402. })
    403. this.map.addOverlay(this.popup)
    404. },
    405. clickMap(e) {
    406. console.log('坐标信息', e);
    407. var pixel = this.map.getEventPixel(e.originalEvent)
    408. const feature = this.map.forEachFeatureAtPixel(
    409. pixel,
    410. (feature) => feature
    411. )
    412. console.log('ffffff', feature);
    413. //feature && feature.values_.moreInfo && this.map.hasFeatureAtPixel(e.pixel)
    414. if (feature && feature.values_.features && feature.values_.features.length > 0) {
    415. this.shopPopup = true
    416. // 设置弹窗位置
    417. const coordinates = feature.getGeometry().getCoordinates()
    418. console.log('弹窗位置', coordinates);
    419. this.popup.setPosition(coordinates)
    420. // 从geoJson中获取信息
    421. const valuesObj = feature.values_.features[0].values_.moreInfo
    422. console.log(valuesObj);
    423. if (valuesObj) {
    424. // 查询本站实时数据
    425. //const { NAME, ANGLE, REMAKE, OBJECTID } = valuesObj
    426. this.zmName = valuesObj.position
    427. if (valuesObj.protocolType == "1") {
    428. modbusInfo(valuesObj.addr).then(res => {
    429. console.log('modbus', res);
    430. this.ssLiuLiang = res.data.instantaneousFlow
    431. this.leiJi = res.data.cumulativeFlow
    432. this.zhaMen = res.data.aperture
    433. })
    434. }
    435. if (valuesObj.protocolType == "2") {
    436. szyInfo(valuesObj.addr).then(res => {
    437. console.log('szy', res)
    438. this.ssLiuLiang = "ANGLE"
    439. this.leiJi = "REMAKE"
    440. this.OBJECTID = "OBJECTID"
    441. })
    442. }
    443. }
    444. } else {
    445. this.shopPopup = false
    446. }
    447. },
    448. /**
    449. * 鼠标移动的事件
    450. */
    451. pointermove() {
    452. this.map.on('pointermove', (e) => {
    453. if (this.map.hasFeatureAtPixel(e.pixel)) {
    454. this.map.getViewport().style.cursor = 'pointer'
    455. } else {
    456. this.map.getViewport().style.cursor = 'inherit'
    457. }
    458. })
    459. },
    460. getBenzhan() {
    461. benzhanList().then(res => {
    462. console.log('泵站查到了', res);
    463. const bzList = res.data
    464. const arr = []
    465. bzList.forEach(item => {
    466. arr.push({
    467. y: item.dimensionality,
    468. x: item.longitude,
    469. name: item.position,
    470. id: item.id,
    471. moreInfo: item
    472. })
    473. })
    474. this.addPoints(arr)
    475. })
    476. },
    477. close() {
    478. this.popup.setPosition(undefined)
    479. return false;
    480. }
    481. },
    482. mounted() {
    483. this.initMap()// 初始化地图方法
    484. this.setBackColor()
    485. this.addGeoJson()
    486. this.addGeoJsonone()
    487. this.addOverlay() // 弹框
    488. this.pointermove()
    489. this.addGeoone()
    490. this.main_pipe()
    491. this.shank_pipe()
    492. this.addZhiqu()
    493. this.addZhiguan()
    494. this.addDouguan()
    495. this.addGanGuan()
    496. this.addShushuiDouqu()
    497. this.getBenzhan()
    498. this.map.on('moveend', (e) => {
    499. const zoom = this.map.getView().getZoom()
    500. this.atzoom = zoom
    501. console.log(zoom)
    502. })
    503. // setInterval(() => {
    504. // this.addPoints(arr)// 根据坐标点批量打点
    505. // }, 1000)
    506. }
    507. }
    508. script>
    509. <style lang="scss" scoped>
    510. #Map {
    511. width: 100%;
    512. height: 100vh;
    513. }
    514. .popupbox {
    515. width: 270px;
    516. height: 210px;
    517. background: url(../../assets/bg@2x.png) no-repeat;
    518. background-size: contain;
    519. display: flex;
    520. flex-direction: column;
    521. text-align: center;
    522. position: relative;
    523. .close {
    524. position: absolute;
    525. right: 10px;
    526. top: 10px;
    527. }
    528. .title {
    529. margin-top: 9px;
    530. }
    531. .info {
    532. font-size: 16px;
    533. text-align: left;
    534. margin-left: 25px;
    535. ul {
    536. margin: 0;
    537. padding: 0;
    538. list-style: none;
    539. }
    540. span.spc {
    541. color: white;
    542. }
    543. }
    544. }
    545. style>

  • 相关阅读:
    Spark系列之SparkSubmit提交任务到YARN
    使用 Java 操作 Redis
    [附源码]java毕业设计社区生鲜电商平台
    【FAQ】华为健康数据唤起授权后出现 907135702错误
    跳转微信小程序提示页面不存在或者页面空白的解决办法【图文教程】
    SSM框架介绍
    NBA赛事直播超清画质背后:阿里云视频云「窄带高清2.0」技术深度解读
    安装Ubuntu系统并搭建C语言环境(超详细教程)
    vivo 海量微服务架构最新实践
    Pulsar Meetup 深圳 2024 会务介绍
  • 原文地址:https://blog.csdn.net/taotaobaobei/article/details/133807951