Vue+OpenLayers?創(chuàng)建地圖并顯示鼠標(biāo)所在經(jīng)緯度(完整代碼)
1、效果

2、創(chuàng)建地圖
本文用的是高德地圖
頁面
<div class="map" id="map"></div>
<div id="mouse-position" class="position_coordinate"></div>初始化地圖
var gaodeLayer = new TileLayer({
title: "高德地圖",
source: new XYZ({
url: 'http://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}',
wrapX: false
})
});
this.map = new Map({
layers: [gaodeLayer],
target: 'map',
view: new View({
center: transform([123.23, 25.73], 'EPSG:4326', 'EPSG:3857'), //地圖初始中心點(diǎn)
projection: 'EPSG:3857',
zoom: 4,
minZoom: 1
}),
});3、添加經(jīng)緯度
var mousePositionControl = new MousePosition({
coordinateFormat: function (coordinate) {
return formatAxirs(coordinate, '經(jīng)度:{x} 緯度:{y}', 2);
},
projection: 'EPSG:4326',
className: "custom-mouse-position",
target: document.getElementById("mouse-position"), //將位置數(shù)據(jù)放到那里
undefinedHTML: "",
});
this.map.addControl(mousePositionControl);4、完整代碼
<script>
import { Map, View } from "ol";
import TileLayer from 'ol/layer/Tile.js';
import XYZ from 'ol/source/XYZ.js';
import { get as getProjection, transform } from 'ol/proj.js';
import MousePosition from "ol/control/MousePosition";
import { format as formatAxirs } from 'ol/coordinate';
export default {
data() {
return {
map: null,
draw: null,
};
},
mounted() {
this.initMap();
},
methods: {
//初始化地圖
initMap() {
var gaodeMapLayer = new TileLayer({
title: "高德地圖",
source: new XYZ({
url: 'http://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}',
wrapX: false
})
});
this.map = new Map({
layers: [gaodeMapLayer],
target: 'map',
view: new View({
center: transform([103.23, 35.33], 'EPSG:4326', 'EPSG:3857'), //地圖初始中心點(diǎn)
projection: 'EPSG:3857',
zoom: 4,
minZoom: 1
}),
});
// 獲取鼠標(biāo)在地圖的經(jīng)緯度
var mousePositionControl = new MousePosition({
coordinateFormat: function (coordinate) {
return formatAxirs(coordinate, '經(jīng)度:{x} 緯度:{y}', 2);
},
projection: 'EPSG:4326',
className: "custom-mouse-position",
target: document.getElementById("mouse-position"), //將位置數(shù)據(jù)放到那里
undefinedHTML: "",
});
this.map.addControl(mousePositionControl);
},
}
}附css代碼
.position_coordinate {
color: #6a6a6a;
position: absolute;
font-size: 14px;
bottom: 20px;
right: 20px;
z-index: 999;
text-align: center;
line-height: 30px;
}到此這篇關(guān)于Vue+OpenLayers 創(chuàng)建地圖并顯示鼠標(biāo)所在經(jīng)緯度的文章就介紹到這了,更多相關(guān)Vue OpenLayers 鼠標(biāo)經(jīng)緯度內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue.js通用應(yīng)用框架-Nuxt.js的上手教程
本篇文章主要介紹了Vue.js通用應(yīng)用框架-Nuxt.js的上手教程,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12
vue中如何實(shí)時(shí)監(jiān)聽本地存儲
這篇文章主要介紹了vue中如何實(shí)時(shí)監(jiān)聽本地存儲,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
利用Vue實(shí)現(xiàn)一個(gè)累加向上漂浮動畫
在不久之前,看到一個(gè)比較有意思的小程序,就是靜神木魚,可以實(shí)現(xiàn)在線敲木魚,自動敲木魚,手盤佛珠,靜心頌缽的,下面就來揭秘如何實(shí)現(xiàn)這個(gè)小程序中敲木魚的累加向上漂浮動畫,需要的可以參考一下2022-11-11
antd的select下拉框因?yàn)閿?shù)據(jù)量太大造成卡頓的解決方式
這篇文章主要介紹了antd的select下拉框因?yàn)閿?shù)據(jù)量太大造成卡頓的解決方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
vue3配置Element及element-plus/lib/theme-chalk/index.css報(bào)錯(cuò)的解決
這篇文章主要介紹了vue3配置Element及element-plus/lib/theme-chalk/index.css報(bào)錯(cuò)的解決,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
VSCode下檢查Vue項(xiàng)目中未使用的依賴的有效方法
在VSCode中檢查Vue項(xiàng)目未使用的依賴有多種方法,包括使用depcheck工具、npm-check工具、VSCode插件以及手動檢查,建議定期檢查并使用版本控制,以確保代碼安全,需要的朋友可以參考下2026-02-02

