vue使用openlayers創(chuàng)建地圖
vue項目中使用openlayers創(chuàng)建地圖,供大家參考,具體內(nèi)容如下
前期準備
- 安裝node環(huán)境
- 安裝cnpm
- 安裝vue-cli
以上步驟網(wǎng)上都有很多教程
搭建vue項目
vue create vue-ol
按照提示一步步搭建vue項目
cd vue-ol npm run serve
瀏覽器打開 http://localhost:8080/ 就可以看到初始化的vue項目頁面
vue項目安裝openlayers
cnpm i ol --s
main.js中引入ol.css
import 'ol/ol.css';
創(chuàng)建地圖組件MapContainer.vue
<template> ? ? <div class="map" id="map"></div> </template> <script> import Map from 'ol/Map'; import OSM from 'ol/source/OSM'; import TileLayer from 'ol/layer/Tile'; import View from 'ol/View'; import { fromLonLat } from 'ol/proj'; export default { ? ? name: "MapContainer", ?? ?methods:{ ?? ??? ?createMap(){ ?? ??? ??? ?let map = new Map({ ?? ??? ??? ??? ?layers: [new TileLayer({ ?? ??? ??? ??? ??? ?source: new OSM(), ?? ??? ??? ??? ?}) ], ?? ??? ??? ??? ?target: 'map', ?? ??? ??? ??? ?view: new View({ ?? ??? ??? ??? ??? ?maxZoom: 18, ?? ??? ??? ??? ??? ?center: fromLonLat([108.92,34.28]), ?? ??? ??? ??? ??? ?zoom: 10, ?? ??? ??? ??? ?}), ?? ??? ??? ?}); ?? ??? ?} ?? ?}, ?? ?mounted(){ ?? ??? ?this.createMap() ?? ?} }; </script> <style scoped> .map { ? ? height: 100%; ? ? margin: 0; ? ? padding: 0; ? ? overflow: hidden; ? ? position: relative; ?? ?background: #1f3064; } </style>
在home.vue中引入地圖組件
<template> ? <div class="home"> ? ? <MapContainer msg="Welcome to Your Vue.js App"/> ? </div> </template> <script> import MapContainer from '@/components/MapContainer.vue' export default { ? name: 'Home', ? components: { ? ? MapContainer ? } } </script> <style> .home{ ?? ?height: 100%; ?? ?position: relative; } </style>
頁面截圖如下:
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
vue動態(tài)刪除從數(shù)據(jù)庫倒入列表的某一條方法
今天小編就為大家分享一篇vue動態(tài)刪除從數(shù)據(jù)庫倒入列表的某一條方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09Vue+Element UI+Lumen實現(xiàn)通用表格分頁功能
這篇文章主要介紹了Vue+Element UI+Lumen實現(xiàn)通用表格分頁功能,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-02-02vue 使用 sortable 實現(xiàn) el-table 拖拽排序功能
這篇文章主要介紹了vue 使用 sortable 實現(xiàn) el-table 拖拽排序功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12