前端学习openLayers配合vue3(加载线上数据源)
现在我们学习一下加载网上的线上数据再加上点矢量图层,紧接着上一步
关键代码
layers: [ //瓦片图层source第三方,或者自带的,地图的底层 new TileLayer({ // source: new OSM(),//内置的国外地址,需要代理 source: new XYZ({ url: "http://wprd0{1-4}.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=1&scl=1&style=7", }), //国内第三方数据源 }), // 矢量图层 new VectorLayer({ source: new VectorSource({ url: "https://geo.datav.aliyun.com/areas_v3/bound/100000_full.json", format: new GeoJSON(), }), }), ],
完整代码
<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 VectorLayer from "ol/layer/Vector"; import VectorSource from "ol/source/Vector"; import GeoJSON from "ol/format/GeoJSON"; 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", })), (map = new Map({ target: "map", //挂载视图的容器 layers: [ //瓦片图层source第三方,或者自带的,地图的底层 new TileLayer({ // source: new OSM(),//内置的国外地址,需要代理 source: new XYZ({ url: "http://wprd0{1-4}.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=1&scl=1&style=7", }), //国内第三方数据源 }), // 矢量图层 new VectorLayer({ source: new VectorSource({ url: "https://geo.datav.aliyun.com/areas_v3/bound/100000_full.json", format: new GeoJSON(), }), }), ], //视图 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 大文件上传(分片上传)