我们原来的偏移感觉比较生硬,我们来学习一下偏移的动画,先列一下这节的知识点
限制经纬度范围和缩放范围(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>
标签:map,center,import,new,偏移,vue3,openLayers,view From: https://www.cnblogs.com/wcq520/p/18649688