亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Vue3 openlayers加載瓦片地圖并手動(dòng)標(biāo)記坐標(biāo)點(diǎn)功能

 更新時(shí)間:2024年04月30日 15:20:01   作者:我向往自由  
這篇文章主要介紹了 Vue3 openlayers加載瓦片地圖并手動(dòng)標(biāo)記坐標(biāo)點(diǎn)功能,我們這里用vue/cli創(chuàng)建,我用的node版本是18.12.1,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下

一、創(chuàng)建Vue3項(xiàng)目

注:不用創(chuàng)建項(xiàng)目的可以直接跳過(guò)這塊。

我們這里用vue/cli創(chuàng)建,我用的node版本是18.12.1。

創(chuàng)建前可以先配置下鏡像源:npm config set registry https://registry.npmmirror.com

按下面的步驟創(chuàng)建即可:

## 查看@vue/cli版本,確保@vue/cli版本在4.5.0以上
vue --version
## 安裝或者升級(jí)你的@vue/cli 
npm install -g @vue/cli
## 執(zhí)行創(chuàng)建命令
vue create vue_test
##  隨后選擇3.x
##  Choose a version of Vue.js that you want to start the project with (Use arrow keys)
##  > 3.x
##    2.x
## 啟動(dòng)
cd vue_test
npm run serve

二、openlayers加載瓦片地圖(引js文件版)

2.1 將以下的文件復(fù)制到public下

2.2 index.html引入ol腳本

2.3 刪除項(xiàng)目自帶的HelloWorld.vue,創(chuàng)建Map.vue

2.4 編碼Map.vue

<template>
    <div class="map" id="map" ref="mapContainer"></div>
</template>
<script>
    import { onMounted, ref } from 'vue';
    export default {
        name: 'MapComponent',
        setup() {
            const mapContainer = ref(null);
            onMounted(() => {
                var BaseMapLayer = function(options) {
                    var layer = new ol.layer.Tile({
                        source: new ol.source.XYZ({
                            url: options.url,
                            tilePixelRatio: 1,
                            minZoom:2,
                            maxZoom:17
                        })
                    });
                    return layer;
                };
                var view = new ol.View({
                    // 這兩個(gè)參數(shù)是你地圖地點(diǎn)的中心點(diǎn)經(jīng)緯度坐標(biāo)
                    center: ol.proj.fromLonLat([120, 17]),
                    zoom: 13,
                    minZoom: 13,
                    maxZoom: 17
                });
                var sateliteopt = {
                    url: '/tiles/{z}/{x}/{y}.png'
                };
                var sate = new ol.layer.Group({
                    layers: [
                        new BaseMapLayer(sateliteopt)
                    ]
                });
                // 添加標(biāo)注層
                var markerLayer = new ol.layer.Vector({
                    source: new ol.source.Vector(),
                    style: new ol.style.Style({
                        image: new ol.style.Icon({
                            anchor: [0.5, 46],
                            anchorXUnits: 'fraction',
                            anchorYUnits: 'pixels',
                            src: '/marker.png' // 標(biāo)注圖標(biāo)的路徑
                        })
                    })
                });
                // 創(chuàng)建標(biāo)注
                var marker = new ol.Overlay({
                    element: document.getElementById('marker'),
                    positioning: 'center-center',
                    stopEvent: false,
                    offset: [0, -23]
                });
                var map = new ol.Map({
                    view: view,
                    layers: [
                        sate,
                        markerLayer // 添加標(biāo)注層到地圖
                    ],
                    overlays: [marker], // 添加標(biāo)注到地圖
                    target: 'map'
                });
                // 監(jiān)聽(tīng)點(diǎn)擊事件
                map.on('click', function(event) {
                    // 將點(diǎn)擊的經(jīng)緯度轉(zhuǎn)換為地圖的像素坐標(biāo)
                    var feature = new ol.Feature({
                        geometry: new ol.geom.Point(event.coordinate)
                    });
                    // 將標(biāo)注添加到標(biāo)注層
                    markerLayer.getSource().addFeature(feature);
                    // 將標(biāo)注移動(dòng)到點(diǎn)擊的位置
                    marker.setPosition(event.coordinate);
                });
            });
            return {
                mapContainer
            };
        }
    };
</script>
<style>
    .map {
        height: 100%;
        width: 100%;
    }
</style>

2.5 修改App.vue

<template>
  <MapComponent/>
</template>
<script>
import MapComponent from './components/Map.vue'
export default {
  name: 'App',
  components: {
    MapComponent
  }
}
</script>
<style>
</style>

2.6 啟動(dòng)項(xiàng)目測(cè)試

三、openlayers加載瓦片地圖(npm安裝依賴(lài)版)

一般來(lái)說(shuō),引js文件這種方式不太實(shí)用,既然都用了vue3了就老老實(shí)實(shí)按規(guī)則走吧...

3.1 安裝openlayers依賴(lài)

先把package.json里加上"ol": "^7.5.2",然后安裝ol:

npm install ol

注意:這里不知道為什么node18.12.0安裝不上ol,我就先用nvm切換到17.1.0版本安裝的ol,安裝完再切回18.12.0版本。

3.2 編寫(xiě)Map.vue代碼

<template>
    <div class="map" id="map" ref="mapContainer"></div>
</template>
<script>
    import "ol/ol.css";
    import { onMounted, ref } from 'vue';
    import { Icon, Style }from "ol/style";
    import Map from "ol/Map";
    import Overlay from "ol/Overlay";
    import View from "ol/View";
    import Point from "ol/geom/Point.js";
    import Feature from "ol/Feature.js";
    import TileLayer from "ol/layer/Tile";
    import XYZ from "ol/source/XYZ";
    import VectorLayer from "ol/layer/Vector";
    import {fromLonLat } from "ol/proj";
    import Group from "ol/layer/Group";
    import VectorSource from "ol/source/Vector";
    export default {
        name: 'MapComponent',
        setup() {
            const mapContainer = ref(null);
            onMounted(() => {
                var BaseMapLayer = function(options) {
                    var layer = new TileLayer({
                        source: new XYZ({
                            url: options.url,
                            tilePixelRatio: 1,
                            minZoom:2,
                            maxZoom:17
                        })
                    });
                    return layer;
                };
                var view = new View({
                    center: fromLonLat([200, 18.1]),
                    zoom: 13,
                    minZoom: 13,
                    maxZoom: 17
                });
                var sateliteopt = {
                    url: 'tiles/{z}/{x}/{y}.png'
                };
                var sate = new Group({
                    layers: [
                        new BaseMapLayer(sateliteopt)
                    ]
                });
                // 添加標(biāo)注層
                var markerLayer = new VectorLayer({
                    source: new VectorSource(),
                    style: new Style({
                        image: new Icon({
                            anchor: [0.5, 46],
                            anchorXUnits: 'fraction',
                            anchorYUnits: 'pixels',
                            src: 'marker.png' // 標(biāo)注圖標(biāo)的路徑
                        })
                    })
                });
                // 創(chuàng)建標(biāo)注
                var marker = new Overlay({
                    element: document.getElementById('marker'),
                    positioning: 'center-center',
                    stopEvent: false,
                    offset: [0, -23]
                });
                var map = new Map({
                    view: view,
                    layers: [
                        sate,
                        markerLayer // 添加標(biāo)注層到地圖
                    ],
                    overlays: [marker], // 添加標(biāo)注到地圖
                    target: 'map'
                });
                // 監(jiān)聽(tīng)點(diǎn)擊事件
                map.on('click', function(event) {
                    // 將點(diǎn)擊的經(jīng)緯度轉(zhuǎn)換為地圖的像素坐標(biāo)
                    var feature = new Feature({
                        geometry: new Point(event.coordinate)
                    });
                    // 將標(biāo)注添加到標(biāo)注層
                    markerLayer.getSource().addFeature(feature);
                    // 將標(biāo)注移動(dòng)到點(diǎn)擊的位置
                    marker.setPosition(event.coordinate);
                });
            });
            return {
                mapContainer
            };
        }
    };
</script>
<style>
    .map {
        height: 800px;
        width: 2000px;
    }
</style>

3.3 啟動(dòng)項(xiàng)目測(cè)試即可

到此這篇關(guān)于 Vue3 openlayers加載瓦片地圖并手動(dòng)標(biāo)記坐標(biāo)點(diǎn)功能的文章就介紹到這了,更多相關(guān) Vue3 openlayers加載瓦片地圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論