code
Snippet
language
CDN
package_2
NPM
content_copy
Copy
open_in_new
Open
const map = L . map ( 'map' ). setView ([ 50.0755 , 14.4378 ], 5 );
L . tileLayer ( 'https://api.maptiler.com/maps/streets-v4/256/{z}/{x}/{y}.png?key=YOUR_MAPTILER_KEY' , {
tileSize: 256 ,
attribution: '© <a href="https://www.maptiler.com/">MapTiler</a> © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap contributors</a>'
}). addTo (map);
map. locate ({ setView: true , maxZoom: 16 });
map. on ( 'locationfound' , ( e ) => {
L . marker (e.latlng). addTo (map). bindPopup ( 'You are here' ). openPopup ();
L . circle (e.latlng, { radius: e.accuracy, color: '#2196F3' , fillOpacity: 0.15 }). addTo (map);
});
map. on ( 'locationerror' , ( e ) => {
alert ( 'Location access denied or unavailable.' );
}); content_copy
Copy
open_in_new
Open
<! doctype html >
< html lang = "en" >
< head >
< meta charset = "utf-8" >
< meta name = "viewport" content = "width=device-width, initial-scale=1.0" >
< title >Show User Location on a Leaflet Map</ title >
< link rel = "stylesheet" href = "https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" >
< script src = "https://unpkg.com/leaflet@1.9.4/dist/leaflet.js" ></ script >
< style >
body { margin : 0 ; padding : 0 ; }
#map { width : 100 % ; height : 100 vh ; }
</ style >
</ head >
< body >
< div id = "map" ></ div >
< script >
const map = L . map ( 'map' ). setView ([ 50.0755 , 14.4378 ], 5 );
L . tileLayer ( 'https://api.maptiler.com/maps/streets-v4/256/{z}/{x}/{y}.png?key=YOUR_MAPTILER_KEY' , {
tileSize: 256 ,
attribution: '© <a href="https://www.maptiler.com/">MapTiler</a> © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap contributors</a>'
}). addTo (map);
map. locate ({ setView: true , maxZoom: 16 });
map. on ( 'locationfound' , ( e ) => {
L . marker (e.latlng). addTo (map). bindPopup ( 'You are here' ). openPopup ();
L . circle (e.latlng, { radius: e.accuracy, color: '#2196F3' , fillOpacity: 0.15 }). addTo (map);
});
map. on ( 'locationerror' , ( e ) => {
alert ( 'Location access denied or unavailable.' );
});
</ script >
</ body >
</ html > content_copy
Copy
open_in_new
Open
import L from 'leaflet' ;
import 'leaflet/dist/leaflet.css' ;
import markerIcon2x from 'leaflet/dist/images/marker-icon-2x.png' ;
import markerIcon from 'leaflet/dist/images/marker-icon.png' ;
import markerShadow from 'leaflet/dist/images/marker-shadow.png' ;
delete L .Icon.Default. prototype ._getIconUrl;
L .Icon.Default. mergeOptions ({ iconRetinaUrl: markerIcon2x, iconUrl: markerIcon, shadowUrl: markerShadow });
const map = L . map ( 'map' ). setView ([ 50.0755 , 14.4378 ], 5 );
L . tileLayer ( 'https://api.maptiler.com/maps/streets-v4/256/{z}/{x}/{y}.png?key=YOUR_MAPTILER_KEY' , {
tileSize: 256 ,
attribution: '© <a href="https://www.maptiler.com/">MapTiler</a> © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap contributors</a>'
}). addTo (map);
map. locate ({ setView: true , maxZoom: 16 });
map. on ( 'locationfound' , ( e ) => {
L . marker (e.latlng). addTo (map). bindPopup ( 'You are here' ). openPopup ();
L . circle (e.latlng, { radius: e.accuracy, color: '#2196F3' , fillOpacity: 0.15 }). addTo (map);
});
map. on ( 'locationerror' , ( e ) => {
alert ( 'Location access denied or unavailable.' );
});
const map = L.map('map').setView([50.0755, 14.4378], 5);
L.tileLayer('https://api.maptiler.com/maps/streets-v4/256/{z}/{x}/{y}.png?key=YOUR_MAPTILER_KEY', {
tileSize: 256,
attribution: '© <a href="https://www.maptiler.com/">MapTiler</a> © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap contributors</a>'
}).addTo(map);
map.locate({ setView: true, maxZoom: 16 });
map.on('locationfound', (e) => {
L.marker(e.latlng).addTo(map).bindPopup('You are here').openPopup();
L.circle(e.latlng, { radius: e.accuracy, color: '#2196F3', fillOpacity: 0.15 }).addTo(map);
});
map.on('locationerror', (e) => {
alert('Location access denied or unavailable.');
}); <!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Show User Location on a Leaflet Map</title>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css">
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
<style>
body { margin: 0; padding: 0; }
#map { width: 100%; height: 100vh; }
</style>
</head>
<body>
<div id="map"></div>
<script>
const map = L.map('map').setView([50.0755, 14.4378], 5);
L.tileLayer('https://api.maptiler.com/maps/streets-v4/256/{z}/{x}/{y}.png?key=YOUR_MAPTILER_KEY', {
tileSize: 256,
attribution: '© <a href="https://www.maptiler.com/">MapTiler</a> © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap contributors</a>'
}).addTo(map);
map.locate({ setView: true, maxZoom: 16 });
map.on('locationfound', (e) => {
L.marker(e.latlng).addTo(map).bindPopup('You are here').openPopup();
L.circle(e.latlng, { radius: e.accuracy, color: '#2196F3', fillOpacity: 0.15 }).addTo(map);
});
map.on('locationerror', (e) => {
alert('Location access denied or unavailable.');
});
</script>
</body>
</html> import L from 'leaflet';
import 'leaflet/dist/leaflet.css';
import markerIcon2x from 'leaflet/dist/images/marker-icon-2x.png';
import markerIcon from 'leaflet/dist/images/marker-icon.png';
import markerShadow from 'leaflet/dist/images/marker-shadow.png';
delete L.Icon.Default.prototype._getIconUrl;
L.Icon.Default.mergeOptions({ iconRetinaUrl: markerIcon2x, iconUrl: markerIcon, shadowUrl: markerShadow });
const map = L.map('map').setView([50.0755, 14.4378], 5);
L.tileLayer('https://api.maptiler.com/maps/streets-v4/256/{z}/{x}/{y}.png?key=YOUR_MAPTILER_KEY', {
tileSize: 256,
attribution: '© <a href="https://www.maptiler.com/">MapTiler</a> © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap contributors</a>'
}).addTo(map);
map.locate({ setView: true, maxZoom: 16 });
map.on('locationfound', (e) => {
L.marker(e.latlng).addTo(map).bindPopup('You are here').openPopup();
L.circle(e.latlng, { radius: e.accuracy, color: '#2196F3', fillOpacity: 0.15 }).addTo(map);
});
map.on('locationerror', (e) => {
alert('Location access denied or unavailable.');
});
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Show User Location on a Leaflet Map</title>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css">
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
<style>
body { margin: 0; padding: 0; }
#map { width: 100%; height: 100vh; }
</style>
</head>
<body>
<div id="map"></div>
<script>
const map = L.map('map').setView([50.0755, 14.4378], 5);
L.tileLayer('https://api.maptiler.com/maps/streets-v4/256/{z}/{x}/{y}.png?key=LKv9WPh4aJggV1CHU4Y8', {
tileSize: 256,
attribution: '© <a href="https://www.maptiler.com/">MapTiler</a> © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap contributors</a>'
}).addTo(map);
map.locate({ setView: true, maxZoom: 16 });
map.on('locationfound', (e) => {
L.marker(e.latlng).addTo(map).bindPopup('You are here').openPopup();
L.circle(e.latlng, { radius: e.accuracy, color: '#2196F3', fillOpacity: 0.15 }).addTo(map);
});
map.on('locationerror', (e) => {
alert('Location access denied or unavailable.');
});
</script>
</body>
</html>