访问 Nuxt-leaflet 中的地图对象

问题描述 投票:0回答:2
leaflet nuxt.js vue2leaflet
2个回答
0
投票

我在 Nuxt 上也遇到了同样的问题。

<client-only>
  <LMap
    ref="map"
    @hook:mounted="mapInitialization"

    :zoom="1"
    :center="[640, 512]"
    :options="{ attributionControl: false, zoomControl: false }"
    :crs="crs"
  >
    <LImageOverlay :url="url" :bounds="bounds"></LImageOverlay>
    <LMarker :lat-lng="[69.5, 177.5]" :z-index="1"></LMarker>
  </LMap>
</client-only>
methods: {
  mapInitialization() {
    const map = this.$refs.map
    console.log('MAP FROM STUFF', map)
    map.mapObject.setView()
  }
}
Mounted 方法中的

$nextTick 对我来说无法正常工作,因为它需要一些勾选,并且在页面引用初始化时为空。


0
投票

有点晚了,但以下内容应该适用于最新版本的 nuxt3-leaflet :

<template>
  <div style="height:100vh; width:100vw">
    <LMap
      ref="map"
      :zoom="zoom"
      :max-zoom="18"
      :center="[47.21322, -1.559482]"
      @ready="mapInitialized"
    >
      <LTileLayer
        url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
        attribution="&amp;copy; <a href=&quot;https://www.openstreetmap.org/&quot;>OpenStreetMap</a> contributors"
        layer-type="base"
        name="OpenStreetMap"
      />
    </LMap>
  </div>
</template>

<script setup>
import { ref } from 'vue'
const zoom = ref(6)

const map = ref(null)

// When the map is ready
const mapInitialized = () => {
  console.log('Map is ready')
  console.log(map.value.maxZoom)
}
</script>
© www.soinside.com 2019 - 2024. All rights reserved.