index.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <template>
  2. <div>
  3. <div id="map" ref="rootmap"></div>
  4. </div>
  5. </template>
  6. <script>
  7. import 'ol/ol.css';
  8. import Map from 'ol/Map';
  9. import TileLayer from 'ol/layer/Tile';
  10. import View from 'ol/View';
  11. import WMTS from 'ol/source/WMTS';
  12. import WMTSTileGrid from 'ol/tilegrid/WMTS';
  13. import { fromLonLat, get as getProjection, transform } from 'ol/proj';
  14. import { getWidth } from 'ol/extent';
  15. import { Vector as SourceVec, OSM } from 'ol/source';
  16. import { Feature } from 'ol';
  17. import { Point, LineString } from 'ol/geom';
  18. import { Style, Icon, Stroke } from 'ol/style';
  19. import { Vector as LayerVec } from 'ol/layer';
  20. let ispro = process.env.NODE_ENV === 'production';
  21. // ispro = false;
  22. export default {
  23. props: {
  24. src: {
  25. type: String,
  26. required: false,
  27. },
  28. },
  29. data() {
  30. return {
  31. map: null,
  32. };
  33. },
  34. mounted() {
  35. var that = this;
  36. this.initmap();
  37. this.map.on('load', function () {
  38. // 增加自定义数据源、自定义图层
  39. that.map.on('click', function (e) {
  40. console.log(e);
  41. });
  42. });
  43. this.addpoint();
  44. this.addline();
  45. },
  46. methods: {
  47. initmap: function () {
  48. this.map = new minemap.Map({
  49. container: 'map',
  50. style: 'https://tocc.jtj.suqian.gov.cn:21100/service/solu/style/id/12886' /*底图样式*/,
  51. center: [118.29564, 33.97441] /*地图中心点*/,
  52. zoom: 14 /*地图默认缩放等级*/,
  53. pitch: 0 /*地图俯仰角度*/,
  54. maxZoom: 17 /*地图最大缩放等级*/,
  55. minZoom: 3 /*地图最小缩放等级*/,
  56. projection: 'MERCATOR',
  57. });
  58. },
  59. addline: function () {
  60. var that = this;
  61. function addSources() {
  62. var center = that.map.getCenter();
  63. var jsonData = {
  64. type: 'FeatureCollection',
  65. features: [
  66. {
  67. type: 'Feature',
  68. geometry: {
  69. type: 'LineString',
  70. coordinates: [
  71. [center.lng - 0.005, center.lat + 0.005],
  72. [center.lng - 0.005, center.lat - 0.005],
  73. ],
  74. },
  75. properties: {
  76. title: '路线一',
  77. kind: 1,
  78. },
  79. },
  80. {
  81. type: 'Feature',
  82. geometry: {
  83. type: 'LineString',
  84. coordinates: [
  85. [center.lng - 0.004, center.lat],
  86. [center.lng, center.lat],
  87. [center.lng + 0.008, center.lat],
  88. ],
  89. },
  90. properties: {
  91. title: '路线二',
  92. kind: 2,
  93. },
  94. },
  95. ],
  96. };
  97. that.map.addSource('lineSource', {
  98. type: 'geojson',
  99. data: jsonData,
  100. });
  101. }
  102. function addLayers() {
  103. that.map.addLayer({
  104. id: 'lineLayer',
  105. type: 'line',
  106. source: 'lineSource',
  107. layout: {
  108. 'line-join': 'round',
  109. 'line-cap': 'round',
  110. 'border-visibility': 'visible', //是否开启线边框
  111. },
  112. paint: {
  113. 'line-width': 6,
  114. 'line-color': {
  115. type: 'categorical',
  116. property: 'kind',
  117. stops: [
  118. [1, '#ff0000'],
  119. [2, '#00ff00'],
  120. ],
  121. default: '#ff0000',
  122. },
  123. 'line-border-width': 2, //设置线边框宽度
  124. 'line-border-opacity': 1, //设置线边框透明度
  125. 'line-border-color': 'blue', //设置线边框颜色
  126. },
  127. minzoom: 7,
  128. maxzoom: 17.5,
  129. });
  130. that.map.addLayer({
  131. id: 'symbolLayer',
  132. type: 'symbol',
  133. source: 'lineSource',
  134. layout: {
  135. 'text-field': '{title}',
  136. 'text-size': 10,
  137. 'symbol-placement': 'line',
  138. },
  139. paint: {
  140. 'text-color': '#000000',
  141. 'text-halo-color': '#ffffff',
  142. 'text-halo-width': 0.8,
  143. },
  144. minzoom: 7,
  145. maxzoom: 17.5,
  146. });
  147. }
  148. // addSources();
  149. // addLayers();
  150. this.map.on('load', function () {
  151. // 增加自定义数据源、自定义图层
  152. addSources();
  153. addLayers();
  154. });
  155. },
  156. addpoint: function () {
  157. var markers = [];
  158. var lnglat = this.map.getCenter();
  159. if (this.map) {
  160. var _marker = new minemap.Marker({
  161. draggable: true, //可以在初始化的时候决定是否可以拖拽
  162. anchor: 'top-left', //锚点位置(默认值"top-left"),可选值有`'center'`, `'top'`, `'bottom'`, `'left'`, `'right'`, `'top-left'`, `'top-right'`, `'bottom-left'`, and `'bottom-right'`
  163. color: '#111', //默认marker标记的颜色
  164. rotation: 0, //marker的旋转角度,以锚点作为旋转中心点,顺时针为正
  165. pitchAlignment: 'map', //倾斜对齐参数(默认值是‘auto’),此值决定marker标记是贴在地图平面上,还是立在地图平面上,当值为`map`时,marker标记贴在地图平面上,当值为`viewport`时,marker标记立在地图平面上,当值为 `auto`时,会根据 `rotationAlignment`参数的值,自动决定.
  166. rotationAlignment: 'map', //旋转对齐参数(默认值是‘auto’),此值决定地图在旋转的时候,marker标记是否跟随旋转,当值为`map` 时,marker标记会固定在地图平面上,不会跟随地图的旋转而旋转;当值为`viewport`时,marker标记会跟随地图的旋转而旋转,保持正向面对观察者;当值为`auto`时,相当于值`viewport`.
  167. scale: 1.5, //只有默认标记有这个参数,将默认标记放大1.5倍
  168. })
  169. .setLngLat([lnglat.lng + 0.005, lnglat.lat])
  170. .addTo(this.map);
  171. markers.push(_marker);
  172. }
  173. },
  174. },
  175. };
  176. </script>
  177. <style>
  178. #map {
  179. height: 100%;
  180. }
  181. /*隐藏ol的一些自带元素*/
  182. .ol-attribution,
  183. .ol-zoom {
  184. display: none;
  185. }
  186. </style>