code Snippetlanguage CDNpackage_2 NPM
content_copy Copyopen_in_new Open
const map = L . map ( 'map' ). setView ([ 48.8566 , 2.3522 ], 13 );
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);
const fence = [[ 48.865 , 2.335 ], [ 48.865 , 2.365 ], [ 48.845 , 2.365 ], [ 48.845 , 2.335 ]];
L . polygon (fence, { color: '#e63946' , fillOpacity: 0.15 , weight: 2 }). addTo (map). bindPopup ( 'Geofence zone' );
function pointInPolygon ( lat , lng , poly ) {
let inside = false ;
for ( let i = 0 , j = poly. length - 1 ; i < poly. length ; j = i ++ ) {
const [ yi , xi ] = poly[i], [ yj , xj ] = poly[j];
if ((yi > lng) !== (yj > lng) && lat < (xj - xi) * (lng - yi) / (yj - yi) + xi) inside = ! inside;
}
return inside;
}
map. on ( 'click' , ( e ) => {
const inside = pointInPolygon (e.latlng.lat, e.latlng.lng, fence);
L . popup ()
. setLatLng (e.latlng)
. setContent (inside ? '<b style="color:#4CAF50">Inside zone</b>' : '<b style="color:#e63946">Outside zone</b>' )
. openOn (map);
}); content_copy Copyopen_in_new Open
<! DOCTYPE html >
< html >
< head >
< meta charset = "utf-8" />
< meta name = "viewport" content = "width=device-width, initial-scale=1.0" />
< 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 ([ 48.8566 , 2.3522 ], 13 );
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);
const fence = [[ 48.865 , 2.335 ], [ 48.865 , 2.365 ], [ 48.845 , 2.365 ], [ 48.845 , 2.335 ]];
L . polygon (fence, { color: '#e63946' , fillOpacity: 0.15 , weight: 2 }). addTo (map). bindPopup ( 'Geofence zone' );
function pointInPolygon ( lat , lng , poly ) {
let inside = false ;
for ( let i = 0 , j = poly. length - 1 ; i < poly. length ; j = i ++ ) {
const [ yi , xi ] = poly[i], [ yj , xj ] = poly[j];
if ((yi > lng) !== (yj > lng) && lat < (xj - xi) * (lng - yi) / (yj - yi) + xi) inside = ! inside;
}
return inside;
}
map. on ( 'click' , ( e ) => {
const inside = pointInPolygon (e.latlng.lat, e.latlng.lng, fence);
L . popup ()
. setLatLng (e.latlng)
. setContent (inside ? '<b style="color:#4CAF50">Inside zone</b>' : '<b style="color:#e63946">Outside zone</b>' )
. openOn (map);
});
</ script >
</ body >
</ html > content_copy Copyopen_in_new Open
import L from 'leaflet' ;
import 'leaflet/dist/leaflet.css' ;
const map = L . map ( 'map' ). setView ([ 48.8566 , 2.3522 ], 13 );
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);
const fence = [[ 48.865 , 2.335 ], [ 48.865 , 2.365 ], [ 48.845 , 2.365 ], [ 48.845 , 2.335 ]];
L . polygon (fence, { color: '#e63946' , fillOpacity: 0.15 , weight: 2 }). addTo (map). bindPopup ( 'Geofence zone' );
function pointInPolygon ( lat , lng , poly ) {
let inside = false ;
for ( let i = 0 , j = poly. length - 1 ; i < poly. length ; j = i ++ ) {
const [ yi , xi ] = poly[i], [ yj , xj ] = poly[j];
if ((yi > lng) !== (yj > lng) && lat < (xj - xi) * (lng - yi) / (yj - yi) + xi) inside = ! inside;
}
return inside;
}
map. on ( 'click' , ( e ) => {
const inside = pointInPolygon (e.latlng.lat, e.latlng.lng, fence);
L . popup ()
. setLatLng (e.latlng)
. setContent (inside ? '<b style="color:#4CAF50">Inside zone</b>' : '<b style="color:#e63946">Outside zone</b>' )
. openOn (map);
});
const map = L.map('map').setView([48.8566, 2.3522], 13);
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);
const fence = [[48.865, 2.335], [48.865, 2.365], [48.845, 2.365], [48.845, 2.335]];
L.polygon(fence, { color: '#e63946', fillOpacity: 0.15, weight: 2 }).addTo(map).bindPopup('Geofence zone');
function pointInPolygon(lat, lng, poly) {
let inside = false;
for (let i = 0, j = poly.length - 1; i < poly.length; j = i++) {
const [yi, xi] = poly[i], [yj, xj] = poly[j];
if ((yi > lng) !== (yj > lng) && lat < (xj - xi) * (lng - yi) / (yj - yi) + xi) inside = !inside;
}
return inside;
}
map.on('click', (e) => {
const inside = pointInPolygon(e.latlng.lat, e.latlng.lng, fence);
L.popup()
.setLatLng(e.latlng)
.setContent(inside ? '<b style="color:#4CAF50">Inside zone</b>' : '<b style="color:#e63946">Outside zone</b>')
.openOn(map);
}); <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<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([48.8566, 2.3522], 13);
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);
const fence = [[48.865, 2.335], [48.865, 2.365], [48.845, 2.365], [48.845, 2.335]];
L.polygon(fence, { color: '#e63946', fillOpacity: 0.15, weight: 2 }).addTo(map).bindPopup('Geofence zone');
function pointInPolygon(lat, lng, poly) {
let inside = false;
for (let i = 0, j = poly.length - 1; i < poly.length; j = i++) {
const [yi, xi] = poly[i], [yj, xj] = poly[j];
if ((yi > lng) !== (yj > lng) && lat < (xj - xi) * (lng - yi) / (yj - yi) + xi) inside = !inside;
}
return inside;
}
map.on('click', (e) => {
const inside = pointInPolygon(e.latlng.lat, e.latlng.lng, fence);
L.popup()
.setLatLng(e.latlng)
.setContent(inside ? '<b style="color:#4CAF50">Inside zone</b>' : '<b style="color:#e63946">Outside zone</b>')
.openOn(map);
});
</script>
</body>
</html> import L from 'leaflet';
import 'leaflet/dist/leaflet.css';
const map = L.map('map').setView([48.8566, 2.3522], 13);
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);
const fence = [[48.865, 2.335], [48.865, 2.365], [48.845, 2.365], [48.845, 2.335]];
L.polygon(fence, { color: '#e63946', fillOpacity: 0.15, weight: 2 }).addTo(map).bindPopup('Geofence zone');
function pointInPolygon(lat, lng, poly) {
let inside = false;
for (let i = 0, j = poly.length - 1; i < poly.length; j = i++) {
const [yi, xi] = poly[i], [yj, xj] = poly[j];
if ((yi > lng) !== (yj > lng) && lat < (xj - xi) * (lng - yi) / (yj - yi) + xi) inside = !inside;
}
return inside;
}
map.on('click', (e) => {
const inside = pointInPolygon(e.latlng.lat, e.latlng.lng, fence);
L.popup()
.setLatLng(e.latlng)
.setContent(inside ? '<b style="color:#4CAF50">Inside zone</b>' : '<b style="color:#e63946">Outside zone</b>')
.openOn(map);
});