MapComponent.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <template>
  2. <div id="mapContainer" ref="mapContainer" style="width: 100%;height: 100%;"></div>
  3. </template>
  4. <script>
  5. import 'ol/ol.css';
  6. import Map from 'ol/Map';
  7. import OSM from 'ol/source/OSM';
  8. import XYZ from 'ol/source/XYZ';
  9. import VectorSource from 'ol/source/Vector';
  10. import Tile from 'ol/layer/Tile';
  11. import VectorLayer from 'ol/layer/Vector';
  12. import Style from 'ol/style/Style';
  13. import Icon from 'ol/style/Icon';
  14. import View from 'ol/View';
  15. import Feature from 'ol/Feature';
  16. import Overlay from 'ol/Overlay';
  17. import {Point, LineString} from 'ol/geom';
  18. import {get as getProj, fromLonLat} from 'ol/proj';
  19. import {ZoomSlider, Zoom} from 'ol/control';
  20. import {containsCoordinate} from 'ol/extent';
  21. import {v4} from 'uuid'
  22. let olMap = null;
  23. export default {
  24. name: 'MapComponent',
  25. data() {
  26. return {
  27. };
  28. },
  29. created(){
  30. this.$nextTick(() => {
  31. setTimeout(()=>{
  32. this.init()
  33. }, 100)
  34. })
  35. },
  36. props: [],
  37. mounted () {
  38. },
  39. methods: {
  40. init(){
  41. let center = fromLonLat([116.41020, 39.915119]);
  42. if (mapParam.center) {
  43. center = fromLonLat(mapParam.center);
  44. }
  45. const view = new View({
  46. center: center,
  47. zoom: mapParam.zoom || 10,
  48. projection: this.projection,
  49. maxZoom: mapParam.maxZoom || 19,
  50. minZoom: mapParam.minZoom || 1,
  51. });
  52. let tileLayer = null;
  53. if (mapParam.tilesUrl) {
  54. tileLayer = new Tile({
  55. source: new XYZ({
  56. projection: getProj("EPSG:3857"),
  57. wrapX: false,
  58. tileSize: 256 || mapParam.tileSize,
  59. url: mapParam.tilesUrl
  60. })
  61. })
  62. }else {
  63. tileLayer = new Tile({
  64. preload: 4,
  65. source: new OSM(),
  66. })
  67. }
  68. olMap = new Map({
  69. target: this.$refs.mapContainer, // 容器ID
  70. layers: [tileLayer], // 默认图层
  71. view: view, // 视图
  72. controls:[ // 控件
  73. // new ZoomSlider(),
  74. new Zoom(),
  75. ] ,
  76. })
  77. console.log(3222)
  78. },
  79. setCenter(point){
  80. },
  81. zoomIn(zoom){
  82. },
  83. zoomOut(zoom){
  84. },
  85. centerAndZoom(point,zoom,callback){
  86. var zoom_ = olMap.getView().getZoom();
  87. zoom = zoom|| zoom_;
  88. var duration = 600;
  89. olMap.getView().setCenter(fromLonLat(point))
  90. olMap.getView().animate({
  91. zoom: zoom ,
  92. duration: duration
  93. });
  94. },
  95. panTo(point, zoom){
  96. let duration = 800;
  97. olMap.getView().cancelAnimations()
  98. olMap.getView().animate({
  99. center: fromLonLat(point),
  100. duration: duration
  101. });
  102. if (!containsCoordinate(olMap.getView().calculateExtent(), fromLonLat(point))) {
  103. olMap.getView().animate({
  104. zoom: olMap.getView().getZoom() - 1,
  105. duration: duration / 2
  106. }, {
  107. zoom: zoom || olMap.getView().getZoom(),
  108. duration: duration / 2
  109. });
  110. }
  111. },
  112. fit(layer){
  113. let extent = layer.getSource().getExtent();
  114. if (extent) {
  115. olMap.getView().fit(extent,{
  116. duration : 600,
  117. padding: [100, 100, 100, 100]
  118. });
  119. }
  120. },
  121. openInfoBox(position, content, offset){
  122. let id = v4()
  123. // let infoBox = document.createElement("div");
  124. // infoBox.innerHTML = content ;
  125. // infoBox.setAttribute("infoBoxId", id)
  126. let overlay = new Overlay({
  127. id:id,
  128. autoPan:true,
  129. autoPanAnimation:{
  130. duration: 250
  131. },
  132. element: content,
  133. positioning:"bottom-center",
  134. offset:offset,
  135. // className:overlayStyle.className
  136. });
  137. olMap.addOverlay(overlay);
  138. overlay.setPosition(fromLonLat(position));
  139. return id;
  140. },
  141. closeInfoBox(id){
  142. olMap.getOverlayById(id).setPosition(undefined)
  143. // olMap.removeOverlay(olMap.getOverlayById(id))
  144. },
  145. /**
  146. * 添加图层
  147. * @param data
  148. * [
  149. * {
  150. *
  151. * position: [119.1212,45,122],
  152. * image: {
  153. * src:"/images/123.png",
  154. * anchor: [0.5, 0.5]
  155. *
  156. * }
  157. * }
  158. *
  159. * ]
  160. */
  161. addLayer(data, clickEvent){
  162. let style = new Style();
  163. if (data.length > 0) {
  164. let features = [];
  165. for (let i = 0; i < data.length; i++) {
  166. let feature = new Feature(new Point(fromLonLat(data[i].position)));
  167. feature.customData = data[i].data;
  168. let cloneStyle = style.clone()
  169. cloneStyle.setImage(new Icon({
  170. anchor: data[i].image.anchor,
  171. crossOrigin: 'Anonymous',
  172. src: data[i].image.src,
  173. }))
  174. feature.setStyle(cloneStyle)
  175. features.push(feature);
  176. }
  177. let source = new VectorSource();
  178. source.addFeatures(features);
  179. let vectorLayer = new VectorLayer({
  180. source:source,
  181. style:style,
  182. renderMode:"image",
  183. declutter: false
  184. })
  185. olMap.addLayer(vectorLayer)
  186. if (typeof clickEvent == "function") {
  187. olMap.on("click", (event)=>{
  188. vectorLayer.getFeatures(event.pixel).then((features)=>{
  189. if (features.length > 0) {
  190. let items = []
  191. for (let i = 0; i < features.length; i++) {
  192. items.push(features[i].customData)
  193. }
  194. clickEvent(items)
  195. }
  196. })
  197. })
  198. }
  199. return vectorLayer;
  200. }
  201. },
  202. removeLayer(layer){
  203. olMap.removeLayer(layer)
  204. },
  205. addLineLayer(positions) {
  206. if (positions.length > 0) {
  207. let points = [];
  208. for (let i = 0; i < positions.length; i++) {
  209. points.push(fromLonLat(positions[i]));
  210. }
  211. let line = new LineString(points)
  212. let lineFeature = new Feature(line);
  213. let source = new VectorSource();
  214. source.addFeature(lineFeature);
  215. let vectorLayer = new VectorLayer({
  216. source: source,
  217. })
  218. olMap.addLayer(vectorLayer)
  219. return vectorLayer;
  220. }
  221. }
  222. },
  223. destroyed() {
  224. // if (this.jessibuca) {
  225. // this.jessibuca.destroy();
  226. // }
  227. // this.playing = false;
  228. // this.loaded = false;
  229. // this.performance = "";
  230. },
  231. }
  232. </script>
  233. <style>
  234. </style>