場景
Vue+Openlayers實現地圖上繪制線:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/121252960
在上面實現的效果是這樣的
此地圖的顯示時正常的,如果地圖是傾斜的,想要把地圖進行一個旋轉。
注:
博客:
https://blog.csdn.net/badao_liumang_qizhi
關注公眾號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。
實現
1、修改map對象的view中的rotation屬性
this.map = new Map({ layers: [this.layers, this.wzLayer, this.lineVector,this.roomLayer,this.lightLayer, this.carLayer], target: "map", view: new View({ projection: projection, center: window.config.center, zoom: window.config.zoom, maxZoom: 19, minZoom: 14.5, rotation: 0.76 }), });
2、修改之后的效果
3、完整示例代碼
<template> <div id="map" class="map"></div> </template> <script> //導入基本模塊 import "ol/ol.css"; import Map from "ol/Map"; import View from "ol/View"; import { Point,LineString } from "ol/geom"; import Feature from "ol/Feature"; import { Icon,Style,Stroke} from "ol/style"; //導入相關模塊 import { Tile as TileLayer , Vector as VectorLayer } from 'ol/layer' import { TileWMS ,Vector as VectorSource } from 'ol/source' export default { name: "olMapImageWMSMulLayers", data() { return { map: null, // map地圖 layer:null, //地圖圖層 lightLayer:null, //燈圖層 houseLayer:null, //房子圖層 lineLayer:null, //線圖層 lineSource:null, //線數據源 //紅綠燈數據 lightData:[ {x:"987798.93778", y:"213885.81024"}, {x:"987710.93778", y:"213810.81024"}, ], //房子數據 houseData:[ {x:"986610.93778", y:"213885.81024"}, {x:"986510.93778", y:"213810.81024"}, ], //線的數據 lineData:[ [986434.4063822062, 215782.0959711917], [989701.5290279881,217149.84072807242], [990613.3107184113,215946.4192185118], ], }; }, mounted() { this.initMap(); setInterval(() => { this.initLightData(); }, 1000) }, methods: { //初始化紅綠燈數據 initLightData(){ this.lightLayer.getSource().clear(); this.lightData.forEach((item, index) => { var feature = new Feature({ geometry: new Point([Number(item.x), Number(item.y)]), }); let url = "images/light.png"; const zoom = this.map.getView().getZoom(); let style = new Style({ image: new Icon({ scale: 0.15 * (zoom -13) , src: url, anchor: [0.48, 0.52], }), }); feature.setStyle(style); this.lightLayer.getSource().addFeature(feature); }); }, //初始化房子數據 initHouseData(){ this.houseLayer.getSource().clear(); this.houseData.forEach((item, index) => { var feature = new Feature({ geometry: new Point([Number(item.x), Number(item.y)]), }); let url = "images/house.png"; let style = new Style({ image: new Icon({ scale: 0.3, src: url, anchor: [0.48, 0.52], }), }); feature.setStyle(style); this.houseLayer.getSource().addFeature(feature); }); }, //畫線 drawLine(){ let pointData = this.lineData; // 所有點位信息 //下邊來添加一線feature var feature = new Feature({ type: "lineStyle", geometry: new LineString( pointData // 線的坐標 ), }); let color = 'green'; let lineStyle = new Style({ stroke: new Stroke({ color: color, width: 4, }), }); // 添加線的樣式 feature.setStyle(lineStyle); // 添加線的fature this.lineSource.addFeature(feature); }, initMap() { //地圖圖層 this.layer = new TileLayer({ source: new TileWMS({ //不能設置為0,否則地圖不展示。 ratio: 1, url: "http://localhost:8000/geoserver/nyc/wms", params: { LAYERS: "nyc:nyc_roads", STYLES: "", VERSION: "1.1.1", tiled: true }, serverType: "geoserver", }), }); // 紅綠燈的圖層 this.lightLayer = new VectorLayer({ source: new VectorSource(), }); //房子的圖層 this.houseLayer = new VectorLayer({ source: new VectorSource(), }); //線的圖層 this.lineSource = new VectorSource({ wrapX: false }); this.lineLayer = new VectorLayer({ source: this.lineSource, }); this.map = new Map({ //地圖容器ID target: "map", //引入地圖 layers: [this.layer,this.lightLayer,this.houseLayer,this.lineLayer], view: new View({ //地圖中心點 center: [987777.93778, 213834.81024], zoom: 12, minZoom:6, // 地圖縮放最小級別 maxZoom:19, rotation: 0.76 }), }); this.initLightData(); this.initHouseData(); this.drawLine(); //獲取點的監聽方法設置 this.onPoint() }, // 獲取點 onPoint() { // 監聽singleclick事件 this.map.on('singleclick', function(e) { console.log(e.coordinate) }) } }, }; </script> <style scoped> .map { width: 100%; height: 800px; } </style>