Created
February 16, 2025 12:11
-
-
Save sadegh19b/aa71ebfcfb8ac4ced7a45cd71fcb91f9 to your computer and use it in GitHub Desktop.
Leaflet Map With Centeral Marker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" /> | |
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script> | |
<style> | |
#map { | |
z-index: 1; | |
height: 400px; | |
} | |
.map-container { | |
position: relative; | |
} | |
.map-marker-shadow-centered { | |
background-image: url("https://unpkg.com/[email protected]/dist/images/marker-shadow.png"); | |
width: 41px; | |
height: 41px; | |
position: absolute; | |
z-index: 2; | |
left: calc(50% - 12px); | |
top: calc(50% - 41px); | |
transition: all 0.4s ease; | |
} | |
.map-marker-centered { | |
background-image: url("https://unpkg.com/[email protected]/dist/images/marker-icon.png"); | |
width: 25px; | |
height: 41px; | |
position: absolute; | |
z-index: 3; | |
left: calc(50% - 12px); | |
top: calc(50% - 41px); | |
transition: all 0.4s ease; | |
} | |
</style> | |
<div class="map-container"> | |
<div class="map-marker-shadow-centered"></div> | |
<div class="map-marker-centered"></div> | |
<div wire:ignore x-data="mapHandler()" x-init="initMap()" id="map"></div> | |
</div> | |
<script> | |
function mapHandler() { | |
return { | |
lat: 35.6895, | |
lng: 51.3890, | |
map: null, | |
initMap() { | |
const recordLat = this.$wire.get('data.lat') ?? this.lat; | |
const recordLng = this.$wire.get('data.lng') ?? this.lng; | |
this.map = L.map('map', { | |
center: [recordLat, recordLng], | |
zoom: 13 | |
}); | |
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { | |
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors' | |
}).addTo(this.map); | |
this.map.on('moveend', (e) => { | |
this.lat = this.map.getCenter().lat; | |
this.lng = this.map.getCenter().lng; | |
this.$wire.set('data.lat', this.lat); | |
this.$wire.set('data.lng', this.lng); | |
}); | |
} | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment