现在我们学习一下加载网上的线上数据再加上点矢量图层,紧接着上一步
关键代码
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>