vue利用openlayers加載天地圖和高德地圖
一、天地圖部分
1、在vue中安裝openlayers
npm i --save ol
這里說的vue是基于腳手架構(gòu)建的。 新建個頁面,也就是vue文件,配置好路由。接著就是可以直接放入我的代碼運行顯示了。
vue利用openlayers加載天地圖和高德地圖
<template>
<div class="wrapper">
<div>天地圖</div>
<div class="map" id="olMap"></div>
</div>
</template>
<script>
import "ol/ol.css";
import {
Tile as TileLayer } from "ol/layer";
import XYZ from "ol/source/XYZ";
import {
defaults as defaultControls } from "ol/control";
import Map from "ol/Map.js";
import View from "ol/View.js";
export default {
data() {
return {
map: null,
parser: null,
};
},
mounted() {
this.initMap();
},
methods: {
initMap() {
const map = new Map({
target: "olMap",
view: new View({
center: [0, 0], //中心點經(jīng)緯度
zoom: 4, //圖層縮放大小
projection: "EPSG:4326",
}),
controls: defaultControls({
zoom: true,
attribution: false,
rotate: false,
}),
});
this.map = map;
// 添加地圖
let url = "http://t{0-7}.tianditu.com/DataServer?x={x}&y={y}&l={z}";
url = `${
url}&T=vec_c&tk=替代你的key`;
const source = new XYZ({
url: url,
projection: "EPSG:4326",
});
const tdtLayer = new TileLayer({
source: source,
});
this.map.addLayer(tdtLayer);
// 添加注記
url = "http://t{0-7}.tianditu.com/DataServer?x={x}&y={y}&l={z}";
url = `${
url}&T=cva_c&tk=替代你的key`;
const sourceCVA = new XYZ({
url: url,
projection: "EPSG:4326",
});
const tdtcvaLayer = new TileLayer({
source: sourceCVA,
});
this.map.addLayer(tdtcvaLayer);
},
},
};
</script>
<style scoped>
.map {
width: 100%;
height: 100vh;
}
</style>
天地圖就可以顯示出來了。
效果圖:

二、高德地圖部分
相對于天地圖,高德地圖就容易多了,直接上代碼
<template>
<div class="wrapper">
<div>高德地圖</div>
<div id="map"></div>
</div>
</template>
<script>
import {
Map,View,Feature} from 'ol'
import * as olProj from 'ol/proj'
import {
Point} from 'ol/geom'
import {
Style, Fill, Stroke, Circle as sCircle } from "ol/style";
// 添加圖層
import Tilelayer from 'ol/layer/Tile'
import {
Vector as VectorLayer} from 'ol/layer'
import {
XYZ,Vector as VectorSource} from 'ol/source'
//引入樣式文件
import "ol/ol.css"
export default {
data() {
return {
map:null
}
},
mounted() {
this.init();
this.setMarker();
},
methods: {
init(){
this.map=new Map({
target:'map',
layers:[new Tilelayer({
source: new XYZ({
url:'https://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}',
})
})
],
view:new View({
// 將西安作為地圖中心
// olProj.fromLonLat方法將經(jīng)緯度轉(zhuǎn)換成對應(yīng)的坐標(biāo)
center:olProj.fromLonLat([108.945951, 34.465262]),
zoom:2
}),
})
},
setMarker(){
let _style = new Style({
image:new sCircle({
radius:10,
stroke:new Stroke({
color:"#fff",
}),
fill: new Fill({
color:'#3399CC',
}),
}),
});
let _feature = new Feature({
geometry:new Point(olProj.fromLonLat([108.945951, 34.465262])),
});
_feature.setStyle(_style);
let _marker = new VectorLayer({
source: new VectorSource({
feature:[_feature],
}),
});
this.map.addLayer(_marker);
},
},
}
</script>
<style scoped>
#map{
/* 屏幕寬高 */
width: 100vw;
height: 100vh;
}
</style>到此這篇關(guān)于 vue利用openlayers加載天地圖和高德地圖的文章就介紹到這了,更多相關(guān)openlayers加載天地圖和高德地圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
uniapp中使用lottie實現(xiàn)JSON動畫的操作步驟
這篇文章主要介紹了如何在項目中使用JSON動畫組件,包括創(chuàng)建目錄結(jié)構(gòu)、下載JSON文件、編寫自定義組件代碼以及組件的使用方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-01-01
vue 動態(tài)添加/刪除dom元素節(jié)點的操作代碼
這篇文章主要介紹了vue 動態(tài)添加/刪除dom元素,需要在點擊添加時,增加一行key/value的輸入框;點擊垃圾桶圖標(biāo)時,刪除對應(yīng)行,本文結(jié)合實例代碼給大家講解的非常詳細(xì),需要的朋友可以參考下2022-12-12
Vue中EpicEditor和vue-quill-editor的使用詳解
EpicEditor和Vue-quill-editor都是基于Quill.js的富文本編輯器,并且都提供了許多強大的功能,下面我們就來介紹一下二者的具體使用,感興趣的小伙伴可以了解一下2023-11-11
Vue中l(wèi)ocalStorage那些你不知道的知識分享
在Vue.js中,?Vuex是一個強大的狀態(tài)管理工具,而localStorage則是一種用于存儲和獲取本地數(shù)據(jù)的機制,雖然這兩個東西都可以用來存儲數(shù)據(jù),但它們之間還是有很大的區(qū)別,本文就來簡單說說吧2023-05-05

