vue利用openlayers加载天地图和高德地图
程序员文章站
2022-11-20 14:38:16
目录一、天地图部分1、在vue中安装openlayers二、高德地图部分一、天地图部分1、在vue中安装openlayersnpm i --save ol这里说的vue是基于脚手架构建的。 新建个页面...
一、天地图部分
1、在vue中安装openlayers
npm i --save ol
这里说的vue
是基于脚手架构建的。 新建个页面,也就是vue
文件,配置好路由。接着就是可以直接放入我的代码运行显示了。
<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], //中心点经纬度 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方法将经纬度转换成对应的坐标 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>
到此这篇关于 vue利用openlayers加载天地图和高德地图的文章就介绍到这了,更多相关openlayers加载天地图和高德地图内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!