vue+openlayers繪制省市邊界線
本文實(shí)例為大家分享了vue+openlayers繪制省市邊界線的具體代碼,供大家參考,具體內(nèi)容如下
1、創(chuàng)建項(xiàng)目
vue init webpack ol_vue
2、安裝ol依賴包
npm install ol
3、引入axios
npm install axios --save
文件目錄:src/main.js
import Vue from 'vue' import router from './router' import App from './App' import axios from "axios"; //添加實(shí)例屬性:不想污染全局作用域,在原型上定義它們使其在每個(gè) Vue 的實(shí)例中可用。prototype向?qū)ο筇砑訉傩院头椒ā? // $ 是在 Vue 所有實(shí)例中都可用的屬性的一個(gè)簡(jiǎn)單約定。 Vue.prototype.$axios = axios //阻止啟動(dòng)生產(chǎn)消息。 Vue.config.productionTip = false //開(kāi)啟debug模式 //Vue.config.debug = true //禁用ESLint進(jìn)行檢測(cè) /* eslint-disable no-new */ new Vue({ el: '#app', router, components: { App }, template: '<App/>' })
4、api
文件目錄:static/js/api.js
const host = 'https://api.obtdata.com/'; export default { 'searchcity': host + 'standard/searchcity' }
5、實(shí)現(xiàn)代碼
<template> <div> <div id="map"></div> </div> </template> <script> import Map from 'ol/Map' import View from 'ol/View' import TileLayer from 'ol/layer/Tile' import GeoJSON from 'ol/format/geoJson' import Feature from 'ol/Feature' import vectorLayer from 'ol/layer/Vector' import SourceVector from 'ol/source/Vector' import {Style,Stroke} from 'ol/style' import OSM from 'ol/source/OSM' import {fromLonLat} from 'ol/proj.js' import api from '../static/js/api' export default { name: "app", data () { return { map: null, source:null, resData:null, vector: null } }, mounted () { //ol.source.Vector,提供矢量圖層數(shù)據(jù) var source = new SourceVector({ wrapX: false, code: 'EPSG:4326', }); //ol.layer.Vector用于顯示在客戶端渲染的矢量數(shù)據(jù)。 this.vector = new vectorLayer({ source: source, }); this.map = new Map({ target: 'map', layers: [ new TileLayer({ source: new OSM() }), this.vector ], view: new View({ center: fromLonLat([110.850881285943,30.1253920380122]),//湖南省 zoom: 5 }) }); this.searchCity() }, methods:{ searchCity() { //axios獲取數(shù)據(jù) this.$axios .get(api.searchcity, { params: { code: '43' } }) .then((res) => { this.resData = res.data.geom; //console.log(this.resData) //ol.format.GeoJSON:以GeoJSON格式讀取和寫(xiě)入數(shù)據(jù) //readGeometry (source,opt_options)閱讀幾何圖形 //dataProjection投影我們正在閱讀的數(shù)據(jù) //featureProjection投影格式閱讀器創(chuàng)建的要素幾何 var geom=(new GeoJSON()).readGeometry(this.resData,{ dataProjection:'EPSG:4326', featureProjection:'EPSG:3857' }); //ol.Feature具有幾何和其他屬性屬性的地理要素的矢量對(duì)象 var feature=new Feature(geom); //ol.source.Vector提供矢量圖層的要素源 //features特征。如果提供為module:ol/Collection,則源和集合中的功能將保持同步。 //wrapX水平包裹世界。對(duì)于橫跨-180°和180°子午線的矢量編輯以正常工作,應(yīng)將其設(shè)置為false。 var source=new SourceVector({ features:[feature], wrapX:false }); //getFeatures以隨機(jī)順序獲取源上的所有功能。 //getGeometry獲取要素的默認(rèn)幾何體。 var polygons=(source.getFeatures()[0].getGeometry()); var size=(this.map.getSize()); //addFeature向源添加單個(gè)功能。 this.vector.getSource().addFeature(feature); //fit(geometryOrExtent,opt_options)根據(jù)給定的地圖大小和邊框擬合給定的幾何或范圍。 this.map.getView().fit(polygons,size); }) } } } </script> <style scoped> </style>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
van-dialog彈窗異步關(guān)閉功能-校驗(yàn)表單實(shí)現(xiàn)
有時(shí)候我們需要通過(guò)彈窗去處理表單數(shù)據(jù),在原生微信小程序配合vant組件中有多種方式實(shí)現(xiàn),其中UI美觀度最高的就是通過(guò)van-dialog嵌套表單實(shí)現(xiàn),這篇文章主要介紹了van-dialog彈窗異步關(guān)閉-校驗(yàn)表單,需要的朋友可以參考下2023-11-11在vue2.x里面簡(jiǎn)單使用socketio問(wèn)題
這篇文章主要介紹了在vue2.x里面簡(jiǎn)單使用socketio問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10Vue select 綁定動(dòng)態(tài)變量的實(shí)例講解
這篇文章主要介紹了Vue select 綁定動(dòng)態(tài)變量的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10解決Vue.js 2.0 有時(shí)雙向綁定img src屬性失敗的問(wèn)題
下面小編就為大家分享一篇解決Vue.js 2.0 有時(shí)雙向綁定img src屬性失敗的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03vite+vue3+element-plus搭建項(xiàng)目的踩坑記錄
這篇文章主要介紹了vite+vue3+element-plus搭建項(xiàng)目的踩坑記錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10