NuxtN
Nuxt11mo ago
Yassaaa

LeafLet Map container is already initialized

Someone help me fix this issue?
I tried looking around and I also tried using copilot to fix the issue but no luck.
here is the code:
<template>
  <div class="w-[42rem] h-[32rem] place-self-center z-10">
    <LMap :zoom="zoom" :center="center" :use-global-leaflet="false" style="height: 100%; width: 100%;">
      <LTileLayer :url="tileUrl" :attribution="tileAttribution" layer-type="base" name="OpenStreetMap" />
      <LGeoJson :geojson="geojson" :options="regionStyle" @add="fitBounds" />
    </LMap>
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import getGeoJson from '../../utils/leafletRegions'

// Map settings
const zoom = ref(6)
const center = ref([52.5200, 13.4050]) // Centered on Berlin

const tileUrl = 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'
const tileAttribution =
  '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'

// Define the styles for the regions
const regionStyle = {
  color: '#0000ff',
  weight: 1,
  opacity: 0.65,
  fillOpacity: 0.1,
}

const geojson = ref(getGeoJson())

// Function to fit the map bounds to the added regions
const fitBounds = (e) => {
  const map = e.target._map
  map.fitBounds(e.target.getBounds())
}
</script>

<style>
/* Custom styles for the map */
.leaflet-container {
  border-radius: 0.5rem;
}
</style>
Was this page helpful?