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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue.js學(xué)習(xí)筆記:如何加載本地json文件
這篇文章主要介紹了vue.js學(xué)習(xí)筆記:如何加載本地json文件,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧。2017-01-01
vue router學(xué)習(xí)之動(dòng)態(tài)路由和嵌套路由詳解
本篇文章主要介紹了vue router 動(dòng)態(tài)路由和嵌套路由,詳細(xì)的介紹了動(dòng)態(tài)路由和嵌套路由的使用方法,有興趣的可以了解一下2017-09-09
如何在vue3+ts項(xiàng)目中使用query和params傳參
Vue3中的路由傳參有兩種方式:query和params,下面這篇文章主要給大家介紹了關(guān)于如何在vue3+ts項(xiàng)目中使用query和params傳參的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04
vue composition-api 封裝組合式函數(shù)的操作方法
在 Vue 應(yīng)用的概念中,“組合式函數(shù)”(Composables) 是一個(gè)利用 Vue 的組合式 API 來封裝和復(fù)用有狀態(tài)邏輯的函數(shù),這篇文章主要介紹了vue composition-api 封裝組合式函數(shù)的操作方法,需要的朋友可以參考下2022-10-10
Vue3父子組件傳參有關(guān)sync修飾符的用法詳解
這篇文章主要給大家介紹關(guān)于前端Vue3父子組件傳參有關(guān)sync修飾符的用法詳細(xì)解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-09-09
在VUE中使用lodash的debounce和throttle操作
這篇文章主要介紹了在VUE中使用lodash的debounce和throttle操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11
解決vue.js 數(shù)據(jù)渲染成功仍報(bào)錯(cuò)的問題
今天小編就為大家分享一篇解決vue.js 數(shù)據(jù)渲染成功仍報(bào)錯(cuò)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08

