实例介绍
【实例简介】openlayers地图底图切换
最近领导让研究,用openlayers实现自定义上传背景图及不同坐标点的显示 的例子,目前还没研究出来,但是通过网上搜索实现了地图底图切换的例子,所以在这里记录一下。
首先呢,先把div写出来
<div class="tianditu"> <div class="ol-map" id="olMap"></div> <button @click="handleClick" style="position:relative">切换</button> </div>
div这块没有难度,接着我们引入ol相关代码:
import Map from "ol/Map";
import View from "ol/View";
import { Tile as TileLayer, Vector as VectorLayer } from "ol/layer";
import { XYZ, OSM, Vector as VectorSource } from "ol/source";
import { fromLonLat } from "ol/proj";
import {defaults as defaultInteractions, DragRotateAndZoom,} from "ol/interaction";
import Feature from "ol/Feature";
import Point from "ol/geom/Point";
import { Style, Icon } from "ol/style";
下来附上我们的js
data() {
return {
layers: [],
config: [],
num: 0,
map: null
};
},
mounted() {
this.inits();
},
methods: {
inits() {
var imageURL = "https://webrd01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}";
const imageURL2 = "http://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}"; //高德地图
const imageURL3 = "http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}"; //ChinaOnlineStreetPurplishBlue
var lay = new TileLayer({source: new XYZ({url: imageURL})});
var lay2 = new TileLayer({source: new XYZ({url: imageURL2})});
var lay3 = new TileLayer({source: new XYZ({url: imageURL3})});
this.layers = [lay2, lay3, lay];
this.config = [this.layers[this.num]]
this.map = new Map({
target: 'olMap', // DOM容器
view: new View({
// projection: 'EPSG:4326',
// center: [120.771441, 30.756433],
center: fromLonLat([108.938832,34.346721]),
zoom: 3, // 缩放级别
minZoom: 3.7, // 最小缩放级别
maxZoom: 18, // 最大缩放级别
}),
layers: this.config,
// layers: [
// new TileLayer({
// source: new OSM(),
// }),
// ],
interactions: defaultInteractions().extend([new DragRotateAndZoom()]), //按住shift键时通过鼠标来进行旋转地图。
});
// 实例化要素
let feature = new Feature({
geometry: new Point(fromLonLat([108.938832,34.346721])), // 地理几何图形选用点几何
});
// 设置样式,这里就是显示一张图片icon
feature.setStyle([
new Style({
image: new Icon({
anchor: [1, 1], // 设置图标位置
src: require("../../assets/images/ship.png"),
// size: [32, 32],// 尺寸
})
}),
]);
// 实例化的时候也可以不添加feature,后续通过方法添加:source.addFeatures([feature])
// 清空feature:source.clear()
// 设置图层
let vector = new VectorLayer({
source: new VectorSource({
features: [feature],
})
});
this.map.addLayer(vector);
},
handleClick(){
if(this.num === 0){
this.num = 1;
this.inits();
}else if(this.num === 1){
this.num = 2;
this.inits();
}else if(this.num === 2){
this.num = 0;
this.inits();
}
}
},
最后怎么能少了样式呢
<style lang="scss" scoped>
.ol-map {
/* width: 70%; */
height: 500px;
/deep/ :last-child{
display: block !important;
}
}
/deep/ .ol-viewport{
display: none;
}
</style>
以上就是我写的一个简单的底图切换的例子,代码可能不是特别的规范,但是实现是没问题的,在这里还是希望大佬提出点意见,我好像你们靠拢哦。
最最最最重要的是希望能碰见大佬帮我解答实现自定义上传背景图并设置标注点的问题哦,如果有小伙伴用了我的例子感觉没什么问题或者有问题的都可以留言哦,我很期待你们的留言
【实例截图】
标签: OpenLayers
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论