前端学习openLayers配合vue3(偏移动画效果,限制范围)
我们原来的偏移感觉比较生硬,我们来学习一下偏移的动画,先列一下这节的知识点
限制经纬度范围和缩放范围(view层)
view = new View({ center:[114.305469,30.592876], zoom: 10, projection:'EPSG:4326', extent:[113.999999,30.25,114.666667,30.833333],//限制地图的范围(限制某个城市) //限制最大和最小的缩放级别 minZoom:4, maxZoom:14 })
经纬度的偏移动画(比如我们偏移到北京)
// 设置北京的经纬度 const beijing=[116.46,39.92] const view =map.getView() view.animate({ center:beijing, zoom:10, projection:'EPSG:4356' })
完整代码
<script setup> import { onMounted, reactive, ref } from "vue"; import { Map, View } from "ol"; import TileLayer from "ol/layer/Tile"; import { XYZ } from "ol/source"; import { fromLonLat } from "ol/proj"; defineProps({ msg: String, }); let map = reactive(""); let view = reactive(""); onMounted(() => { initMap(); }); let initMap = () => { (view = new View({ center:[114.305469,30.592876], zoom: 10, projection:'EPSG:4326', extent:[113.999999,30.25,114.666667,30.833333],//限制地图的范围(限制某个城市) //限制最大和最小的缩放级别 minZoom:4, maxZoom:14 })), (map = new Map({ target: "map", //挂载视图的容器 layers: [ new TileLayer({ source: new XYZ({ url: "http://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}", wrapX: false, }), }), //图层 ], //视图 view: view, })); }; let move = () => { // 设置北京的经纬度 const beijing=[116.46,39.92] const view =map.getView() view.animate({ center:beijing, zoom:10, projection:'EPSG:4356' }) }; </script> <template> <div id="map"> <div class="btns"> <button @click="move">中心点移动到北京市</button> </div> </div> </template> <style scoped> .btns { display: flex; position: fixed; left: 20px; bottom: 20px; z-index: 999; } .btns div { width: 100px; height: 40px; display: flex; justify-content: center; align-items: center; cursor: pointer; } .read-the-docs { color: #888; } #map { margin: 0; width: 100vw; height: 100vh; } </style>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
2024-01-03 大文件上传(分片上传)