当前位置:   article > 正文

vue+openlayers实现地图打点_vue2使用openlayers地图打点

vue2使用openlayers地图打点


前言

openlayers的使用

一、使用步骤

1.引入库

代码如下(示例):npm install ol

  1. import "ol/ol.css"; //样式
  2. import Map from "ol/Map"; //地图对象
  3. import Feature from "ol/Feature"; //标记
  4. import Overlay from "ol/Overlay";
  5. import View from "ol/View"; //视图
  6. import { Point, LineString } from "ol/geom"; //标点,画线
  7. import {
  8. OSM,
  9. XYZ,
  10. TileArcGISRest,
  11. Vector as VectorSource,
  12. WMTS,
  13. } from "ol/source"; //图源
  14. import { Vector as VectorLayer, Tile as TileLayer } from "ol/layer"; //图层
  15. import { fromLonLat, transform } from "ol/proj"; //坐标系维度转换
  16. import {
  17. Circle as CircleStyle,
  18. Fill,
  19. Icon,
  20. Stroke,
  21. Style,
  22. RegularShape,
  23. } from "ol/style"; //填充图标描边等之类用来标记打点使用

2.方法里使用

  1. <template>
  2. <div>
  3. <div id="container">
  4. <audio
  5. controls="controls"
  6. ref="audiod"
  7. style="display: none"
  8. src="../audio/alert.mp3"
  9. ></audio>
  10. </div>
  11. </div>
  12. </template>
  13. props: ["shipData"],
  14. mounted() {
  15. this.initMap();
  16. },
  17. watch: {
  18. checkmsgModal() {},
  19. shipData: {
  20. deep: true,
  21. handler() {
  22. this.vectorSource.clear();
  23. this.shipData.forEach((item, index) => {
  24. item.customSortId = index;
  25. this.setMarker(item);
  26. });
  27. },
  28. },
  29. },
  30. methods:{
  31. initMap() {
  32. let that = this;
  33. //地图瓦片
  34. let tileLayer = new TileLayer({
  35. source: new XYZ({
  36. url: "写入地图瓦片地址",
  37. }),
  38. });
  39. // 绘制点
  40. let featureLayer = new VectorLayer({
  41. source: that.vectorSource,
  42. });
  43. // 绘制线
  44. that.vectorLineSource = new VectorSource({ wrapX: false });
  45. let featureLineLayer = new VectorLayer({
  46. source: that.vectorLineSource,
  47. });
  48. let sourceSatelliteMark = new TileLayer({
  49. source: new XYZ({
  50. url: "瓦片地址",
  51. }),
  52. });
  53. this.map = new Map({
  54. //olMap为map的地图容器
  55. target: "container", // DOM容器
  56. // 设置地图图层
  57. layers: [tileLayer, featureLayer, featureLineLayer], //地图源的瓦片图层
  58. // 设置显示地图的视图
  59. view: new View({
  60. // projection: 'EPSG:4326',
  61. center: transform(this.jwd, "EPSG:4326", "EPSG:3857"), //地图中心点 经纬度
  62. zoom: this.zoom, // 缩放级别-显示层级
  63. maxZoom: 16,
  64. }),
  65. });
  66. // this.map.addLayer(tileLayer);
  67. //这个是点击出现弹窗
  68. let overlayForm = new Overlay({
  69. // 创建一个图层
  70. element: this.$refs.msgForm.$el, // 图层绑定我们上边的弹窗
  71. autoPan: true,
  72. autoPanAnimation: {
  73. duration: 250,
  74. },
  75. autoPanMargin: 100,
  76. positioning: "center-center",
  77. });
  78. overlayForm.setPosition(undefined); // 设置弹窗位置,刚开始我们不让他显示,就是undefined就行
  79. this.map.addOverlay(overlayForm); // 然后把弹窗的图层加载到地图上
  80. this.map.on("click", (ev) => {
  81. let pixel = ev.pixel; // 鼠标点击的位置,这个应该是像素
  82. // let mouse = ev.coordinate // 鼠标点击的坐标位置
  83. let mouse = ev.coordinate; // 鼠标点击的坐标位置
  84. const hdms = transform(mouse, "EPSG:3857", "EPSG:4326"); // 转换坐标格式
  85. let feature = this.map.forEachFeatureAtPixel(pixel, (feature) => {
  86. return feature; // 查找出点击的哪个坐标
  87. });
  88. console.log(feature);
  89. // console.log(feature)
  90. if (feature) {
  91. this.buoyData.forEach((item) => {
  92. // console.log(item.buoyId);
  93. if (item.buoyId == feature.values_.idName) {
  94. overlayForm.setPosition(mouse); // 设置弹窗的位置
  95. });
  96. }
  97. });
  98. }
  99. // else {
  100. // this.msgModal = false;
  101. // overlayForm.setPosition(undefined); // 如果没有点击坐标点,就隐藏弹窗
  102. // }
  103. });
  104. },
  105. //标记
  106. setMarker(item) {
  107. let feature = new Feature({
  108. id: item.customSortId,
  109. geometry: new Point(
  110. transform(
  111. [item.target.position.lon, item.target.position.lat],
  112. "EPSG:4326",
  113. "EPSG:3857"
  114. )
  115. ),
  116. });
  117. feature.setStyle(
  118. new Style({
  119. image: new Icon({
  120. src: require("../components/screenHeader/images/greenShip.svg"),
  121. scale: 1.1,
  122. // rotation: (item.heading * Math.PI) / 180,
  123. }),
  124. })
  125. );
  126. this.vectorSource.addFeature(feature);
  127. // 使用Overlay添加GIF动态图标点位信息 (我这个要打动图,试了下还是加个图层,方法在下面,用了iview组件,里面的一个通知提醒,有预警的话就弹出,由于里面的内容不好改,就使用了render函数)
  128. addGif(lon, lat, id) {
  129. const that = this;
  130. let mapDom = that.$refs.container;
  131. var oDiv = document.createElement("div");
  132. oDiv.id = id;
  133. oDiv.style.width = "40px";
  134. oDiv.style.height = "40px";
  135. oDiv.style.backgroundImage =
  136. "url( " + require("../components/screenHeader/images/run.gif") + ")";
  137. oDiv.style.backgroundSize = "100%";
  138. mapDom.appendChild(oDiv);
  139. this.markerPoint = new Overlay({
  140. position: fromLonLat([lon, lat]),
  141. positioning: "center-center",
  142. element: document.getElementById(id),
  143. id: id,
  144. stopEvent: true,
  145. });
  146. this.map.addOverlay(this.markerPoint);
  147. mapDom.removeChild(oDiv);
  148. },
  149. }
  150. //弹出预警信息
  151. warning() {
  152. const that = this;
  153. this.$refs.audiod.play();//弹出预警伴随有声音提醒
  154. this.$Notice.open({
  155. render: (h) => {
  156. return h(
  157. "div",
  158. {
  159. style: {
  160. width: "100%",
  161. fontSize: "14px",
  162. },
  163. },
  164. [
  165. h(
  166. "Button",
  167. {
  168. style: {
  169. marginTop: "10px",
  170. marginLeft: "10px",
  171. width: "21%",
  172. },
  173. on: {
  174. click: () => {
  175. that.map.removeOverlay(that.markerPoint);
  176. this.addGif(lon, lat, that.warnList.warnId);
  177. },
  178. },
  179. },
  180. ["定位"]
  181. ),
  182. },
  183. duration: 0,
  184. onClose() {
  185. that.map.removeOverlay(that.markerPoint);//关闭时清除gif图层
  186. },
  187. });
  188. // var div = document.querySelector(".ivu-notice");
  189. // console.log(div, "111");
  190. },

3.图片:


总结

完成!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/607337
推荐阅读
相关标签
  

闽ICP备14008679号