Vue3+OpenLayers的簡(jiǎn)單使用詳解
在前端開發(fā)中,地圖展示是一個(gè)常見的需求。OpenLayers 是一個(gè)功能強(qiáng)大的開源地圖庫,可以幫助我們?cè)?Web 應(yīng)用程序中展示地圖并且進(jìn)行交互。
本文將介紹如何在 Vue 3 項(xiàng)目中使用 OpenLayers,讓您能夠輕松實(shí)現(xiàn)地圖展示功能
步驟一:安裝 OpenLayers
首先,我們需要在 Vue 3 項(xiàng)目中安裝 OpenLayers:
npm install ol
步驟二:創(chuàng)建地圖組件
在 Vue 3 項(xiàng)目中創(chuàng)建一個(gè)地圖組件,例如Map.vue:
在vue文件中創(chuàng)建兩個(gè)圖層,顯示地圖和地點(diǎn)
const tdtVectorLayer = new TileLayer({
title: "天地圖矢量圖層",
source: new XYZ({
url: "http://t0.tianditu.com/DataServer?T=vec_w&x={x}&y={y}&l={z}&tk=天地圖申請(qǐng)的key",
})
});
const tdtVectorLabelLayer = new TileLayer({
title: "天地圖矢量注記圖層",
source: new XYZ({
url: "http://t0.tianditu.com/DataServer?T=cva_w&x={x}&y={y}&l={z}&tk=天地圖申請(qǐng)的key",
})
});
然后將圖層添加到openlayers提供map方法中
map = new Map({
layers: [tdtVectorLayer,tdtVectorLabelLayer],
target: mapCon.value,
view: new View({
center: beijing,
minZoom: 4,
zoom: 8,
}),
});
openlayers提供有修改地圖樣式的方法:
geoMarker.setStyle(styles.geoMarker)
const iconFeature = new Feature({
geometry: new Point(beijing),
});
iconFeature.setStyle(createLabelStyle(iconFeature));
完整的代碼:
<template>
<div ref="mapCon" id="mapCon"></div>
</template>
<script>
import "ol/ol.css";
import { ref, onMounted } from "vue";
import { Map, View } from "ol";
import TileLayer from "ol/layer/Tile";
import XYZ from "ol/source/XYZ";
import { fromLonLat } from "ol/proj";
import VectorLayer from "ol/layer/Vector";
import VectorSource from "ol/source/Vector";
import Feature from "ol/Feature";
import {LineString,Point } from 'ol/geom';
import { Icon, Style,Stroke,Circle,Fill } from "ol/style";
import yzm from '@/assets/stationicon.png'
export default {
setup() {
const mapCon = ref(null);
const speed = ref(null);
const animating = ref(false);
let routeCoords;
let routeLength;
const beijing = fromLonLat([116.28, 39.54]);
let map;
let clickedPoints = [fromLonLat([116.28, 39.54])];
const styles = {
geoMarker: new Style({
image: new Circle({
radius: 7,
snapToPixel: false,
fill: new Fill({ color: 'black' }),
stroke: new Stroke({
color: 'white',
width: 2
})
})
}),
};
const geoMarker = new Feature({
type: 'geoMarker',
geometry: new Point(beijing)
});
onMounted(() => {
const tdtVectorLayer = new TileLayer({
title: "天地圖矢量圖層",
source: new XYZ({
url: "http://t0.tianditu.com/DataServer?T=vec_w&x={x}&y={y}&l={z}&tk=天地圖申請(qǐng)的key",
})
});
const tdtVectorLabelLayer = new TileLayer({
title: "天地圖矢量注記圖層",
source: new XYZ({
url: "http://t0.tianditu.com/DataServer?T=cva_w&x={x}&y={y}&l={z}&tk=天地圖申請(qǐng)的key",
})
});
map = new Map({
layers: [tdtVectorLayer,tdtVectorLabelLayer],
target: mapCon.value,
view: new View({
center: beijing,
minZoom: 4,
zoom: 8,
}),
});
const createLabelStyle = function (feature) {
return new Style({
image: new Icon({
anchor: [0.5, 60],
anchorOrigin: "top-right",
anchorXUnits: "fraction",
anchorYUnits: "pixels",
offsetOrigin: "top-right",
opacity: 0.75,
src: yzm,
}),
});
};
geoMarker.setStyle(styles.geoMarker)
const iconFeature = new Feature({
geometry: new Point(beijing),
});
iconFeature.setStyle(createLabelStyle(iconFeature));
const vectorSource = new VectorSource({
features: [iconFeature,geoMarker],
});
const vectorLayer = new VectorLayer({
source: vectorSource,
});
map.addLayer(vectorLayer);
map.on("click", function (evt) {
const point = evt.coordinate;
addVectorLabel(point);
});
function addVectorLabel(coordinate) {
const newFeature = new Feature({
geometry: new Point(coordinate),
});
newFeature.setStyle(createLabelStyle(newFeature));
vectorSource.addFeature(newFeature);
clickedPoints.push(coordinate);
const route = new LineString([clickedPoints[clickedPoints.length - 2], coordinate]);
routeCoords = route.getCoordinates();
routeLength = routeCoords.length;
if (clickedPoints.length > 1) {
// 創(chuàng)建折線特征,連接點(diǎn)擊的點(diǎn)
const lineStyle = new Style({
stroke: new Stroke({
width: 6,
color: [237, 212, 0, 0.8]
}),
});
const lineFeature = new Feature({
geometry: route,
});
lineFeature.setStyle(lineStyle);
vectorSource.addFeature(lineFeature);
}
}
});
return { mapCon,animating,startAnimation };
},
};
</script>
<style>
#menu{
margin-bottom: 20px;
}
#mapCon {
width: 70vw;
height: 500px;
}
</style>
本段代碼主要實(shí)現(xiàn)在地圖點(diǎn)擊時(shí),添加標(biāo)注并給兩點(diǎn)間連線
總結(jié)
通過以上步驟,我們成功地在 Vue 3 項(xiàng)目中使用 OpenLayers 來展示地圖。
您也可以根據(jù)自己的需求來配置地圖的樣式和功能,OpenLayers 提供了豐富的 API 和組件來滿足各種地圖展示的需求。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue.js使用v-show和v-if的注意事項(xiàng)
這篇文章一開始先對(duì)Vue.js中v-show和v-if兩者的區(qū)別進(jìn)行了簡(jiǎn)單的介紹,而后通過圖文詳細(xì)給大家介紹了Vue.js使用v-show和v-if注意的事項(xiàng),有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-12-12
解決vue2.0路由跳轉(zhuǎn)未匹配相應(yīng)用路由避免出現(xiàn)空白頁面的問題
今天小編就為大家分享一篇解決vue2.0路由跳轉(zhuǎn)未匹配相應(yīng)用路由避免出現(xiàn)空白頁面的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08
Vue3響應(yīng)式對(duì)象數(shù)組不能實(shí)時(shí)DOM更新問題解決辦法
在寫大文件上傳時(shí),碰到關(guān)于 vue2 跟 vue3 對(duì)在循環(huán)中使用異步,并動(dòng)態(tài)把普通對(duì)象添加進(jìn)響應(yīng)式數(shù)據(jù),在異步前后修改該普通對(duì)象的某個(gè)屬性,導(dǎo)致 vue2 跟 vue3 的視圖更新不一致,引發(fā)一系列的思考,所以本文介紹了Vue3響應(yīng)式對(duì)象數(shù)組不能實(shí)時(shí)DOM更新問題解決辦法2024-07-07
Vue打包優(yōu)化之生產(chǎn)環(huán)境刪除console日志配置
這篇文章主要為大家介紹了Vue打包優(yōu)化之生產(chǎn)環(huán)境刪除console日志配置詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
vue+scss+element-ui實(shí)現(xiàn)表格表頭斜杠一分為三方式
這篇文章主要介紹了vue+scss+element-ui實(shí)現(xiàn)表格表頭斜杠一分為三方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
Vue技巧Element Table二次封裝實(shí)戰(zhàn)示例
這篇文章主要為大家介紹了Vue技巧Element Table二次封裝實(shí)戰(zhàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
elementUi vue el-radio 監(jiān)聽選中變化的實(shí)例代碼
這篇文章主要介紹了elementUi vue el-radio 監(jiān)聽選中變化,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06

