openlayers [二] 初始化map 以及map的一些参数

map 参数

controls 地图的控件
pixelRatio 设备上物理像素与设备无关像素(dip)之间的比率。
interactions 地图的互动
keyboardEventTarget 监听键盘事件的元素。这决定了KeyboardPan和 KeyboardZoom互动的触发时间。例如,如果将此选项设置为 document键盘,则交互将始终触发。如果未指定此选项,则库在其上侦听键盘事件的元素是地图目标(即用户为地图提供的div)。如果不是 document,则需要集中目标元素以发射关键事件,这要求目标元素具有tabindex属性。
layers 图层
maxTilesLoading 同时加载的最大瓦片数。
overlays 叠加层最初添加到地图中。默认情况下,不添加任何覆盖。
target 地图的容器
view  地图的视图

效果图

在这里插入图片描述

代码

<template>
    <div class="container">
        <h3>加载OpenStreetMap地图</h3>
        <p>超爷openlayer</p>
        <div id="vue-openlayers"></div>
        <a href="https://xiehao.blog.csdn.net/article/details/105272407">
            详情地址
        </a>
    </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import "ol/ol.css";
import { Map, View } from "ol";
import { Tile as TileLayer } from "ol/layer";
import { OSM } from "ol/source";
const map = ref(null);
const initMap = () => {
    let osmLayer = new TileLayer({
        source: new OSM(),
        zIndex: 1,
    }); 
    map.value = new Map({
        target: "vue-openlayers", //跟页面元素的 id 绑定来进行渲染
        layers: [osmLayer],
        view: new View({
            //配置地图显示的options配置(坐标系,中心点,缩放级别等)
            projection: "EPSG:4326",
            center: [116.389, 39.903], //地图中心坐标
            zoom: 8, //缩放级别
        }),
    });
};
onMounted(() => {
    initMap();
});
</script>
<style scoped>
.container {
    width: 840px;
    height: 570px;
    margin: 50px auto;
    border: 1px solid #42b983;
}
#vue-openlayers {
    width: 800px;
    height: 450px;
    margin: 0 auto;
    border: 1px solid #42b983;
    position: relative;
}
</style>

分析

1 首先页面需要先创建一个 div,用来绑定 map 地图上

<div id="vue-openlayers"></div>

2 下面是一个初始化地图的方法

const initMap = () => {
    let osmLayer = new TileLayer({
        source: new OSM(),
        zIndex: 1,
    }); 
    map.value = new Map({
        target: "vue-openlayers", //跟页面元素的 id 绑定来进行渲染
        layers: [osmLayer],
        view: new View({
            //配置地图显示的options配置(坐标系,中心点,缩放级别等)
            projection: "EPSG:4326",
            center: [116.389, 39.903], //地图中心坐标
            zoom: 8, //缩放级别
        }),
    });
};
前面的声明了三个变量分别是 容器(target),图层(layers),view(视图)。
通过实例化了一个OpenLayers的Map对象,于是就显示了地图!Map是什么
它是OpenLayers中最主要的对象!要初始化一幅地图,需要一个target,view,layers。
  • target 主要是用来跟页面的元素进行绑定显示
  • layers 我们看到,他其实是一个数组形式,那就说明它可以存在多个图层,这也是openlayers强大之处,非常实用。通过new Tile() 创建了一个图层,但是单单创建图层也是不行,图层里面必须要有数据,于是就有了 source: new OSM() 创建了一个OpenStreetMap提供的切片数据
  • 同理,通过new View() 创建了一个视图对象
    • center 设置默认地图中心点位置
    • zoom 设置缩放等级
  • 53
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是使用Vue和OpenLayers初始化VectorLayer的示例代码: ```vue <template> <div id="map"></div> </template> <script> import 'ol/ol.css'; import { Map, View } from 'ol'; import { Tile as TileLayer, Vector as VectorLayer } from 'ol/layer'; import { OSM, Vector as VectorSource } from 'ol/source'; import { fromLonLat } from 'ol/proj'; import Feature from 'ol/Feature'; import Point from 'ol/geom/Point'; import { Icon, Style } from 'ol/style'; export default { name: 'Map', mounted() { const map = new Map({ target: 'map', layers: [ new TileLayer({ source: new OSM(), }), ], view: new View({ center: fromLonLat([0, 0]), zoom: 2, }), }); const pointLayer = new VectorLayer({ source: new VectorSource(), }); map.addLayer(pointLayer); const coordinates = [ [0, 0], [10, 10], [20, 20], ]; const featuresArr = []; coordinates.forEach((coordinate) => { const feature = new Feature({ geometry: new Point(fromLonLat(coordinate)), }); feature.setStyle( new Style({ image: new Icon({ src: 'https://openlayers.org/en/latest/examples/data/icon.png', }), }) ); featuresArr.push(feature); }); pointLayer.getSource().addFeatures(featuresArr); }, }; </script> <style> #map { height: 400px; } </style> ``` 上述代码中,我们首先在Vue组件中引入了OpenLayers相关的模块,然后在mounted钩子函数中初始化了一个地图,并添加了一个OSM图层。接着,我们创建了一个VectorLayer矢量图层,并将其添加到地图中。然后,我们定义了一组经纬度坐标数据,并循环遍历这些坐标数据,创建了一个点要素Feature,并设置了其样式。最后,我们将所有的要素信息添加到了pointLayer图层的数据源中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱玩亚索的程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值