Add Geofence visualization layer on map
Implemented visual representation of geofences on the main map view: **New Component:** - GeofenceLayer.tsx - Renders active geofences as circles with: - Configurable opacity (15% fill, 80% border) - Color matching from geofence settings - Interactive popups showing zone details (name, device, radius, coordinates) - Auto-refresh every 30 seconds **Map Integration:** - Added toggle button to show/hide geofences (top-right corner) - Purple button when active, white when hidden - Only displays active geofences (is_active = 1) - Geofences render below markers for better visibility **Features:** - Real-time sync with geofence management - Responsive to geofence changes (color, radius, status) - Clean visual hierarchy with location markers on top - Hover/click popups for detailed zone information Users can now visually see their geofence zones on the map and understand the spatial relationship between device locations and zones. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
96
components/map/GeofenceLayer.tsx
Normal file
96
components/map/GeofenceLayer.tsx
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { useEffect, useState } from "react";
|
||||||
|
import { Circle, Popup } from "react-leaflet";
|
||||||
|
import { Geofence } from "@/lib/types";
|
||||||
|
|
||||||
|
interface GeofenceLayerProps {
|
||||||
|
visible: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function GeofenceLayer({ visible }: GeofenceLayerProps) {
|
||||||
|
const [geofences, setGeofences] = useState<Geofence[]>([]);
|
||||||
|
const [loading, setLoading] = useState(true);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!visible) return;
|
||||||
|
|
||||||
|
fetchGeofences();
|
||||||
|
|
||||||
|
// Refresh every 30 seconds
|
||||||
|
const interval = setInterval(fetchGeofences, 30000);
|
||||||
|
return () => clearInterval(interval);
|
||||||
|
}, [visible]);
|
||||||
|
|
||||||
|
const fetchGeofences = async () => {
|
||||||
|
try {
|
||||||
|
const response = await fetch("/api/geofences");
|
||||||
|
if (!response.ok) throw new Error("Failed to fetch geofences");
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
setGeofences(data.geofences || []);
|
||||||
|
} catch (err) {
|
||||||
|
console.error("Failed to fetch geofences", err);
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!visible || loading) return null;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{geofences
|
||||||
|
.filter((g) => g.is_active === 1)
|
||||||
|
.map((geofence) => (
|
||||||
|
<Circle
|
||||||
|
key={geofence.id}
|
||||||
|
center={[geofence.center_latitude, geofence.center_longitude]}
|
||||||
|
radius={geofence.radius_meters}
|
||||||
|
pathOptions={{
|
||||||
|
color: geofence.color,
|
||||||
|
fillColor: geofence.color,
|
||||||
|
fillOpacity: 0.15,
|
||||||
|
weight: 2,
|
||||||
|
opacity: 0.8,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Popup>
|
||||||
|
<div className="text-sm space-y-2">
|
||||||
|
<p className="font-bold text-base flex items-center gap-2">
|
||||||
|
<span
|
||||||
|
className="w-3 h-3 rounded-full ring-2 ring-white"
|
||||||
|
style={{ backgroundColor: geofence.color }}
|
||||||
|
/>
|
||||||
|
{geofence.name}
|
||||||
|
</p>
|
||||||
|
{geofence.description && (
|
||||||
|
<p className="text-gray-600 text-xs">{geofence.description}</p>
|
||||||
|
)}
|
||||||
|
<div className="space-y-1 text-xs">
|
||||||
|
<p className="flex items-center gap-1">
|
||||||
|
<span>📍</span> Device: {geofence.device_id}
|
||||||
|
</p>
|
||||||
|
<p className="flex items-center gap-1">
|
||||||
|
<span>📏</span> Radius:{" "}
|
||||||
|
{geofence.radius_meters >= 1000
|
||||||
|
? `${(geofence.radius_meters / 1000).toFixed(1)} km`
|
||||||
|
: `${geofence.radius_meters} m`}
|
||||||
|
</p>
|
||||||
|
<p className="flex items-center gap-1">
|
||||||
|
<span>🎯</span> Center:{" "}
|
||||||
|
{geofence.center_latitude.toFixed(4)},{" "}
|
||||||
|
{geofence.center_longitude.toFixed(4)}
|
||||||
|
</p>
|
||||||
|
<p className="flex items-center gap-1">
|
||||||
|
<span className="text-green-600">✓</span>
|
||||||
|
<span className="text-green-600 font-semibold">Active</span>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</Popup>
|
||||||
|
</Circle>
|
||||||
|
))}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -13,6 +13,7 @@ import {
|
|||||||
LayersControl,
|
LayersControl,
|
||||||
useMap,
|
useMap,
|
||||||
} from "react-leaflet";
|
} from "react-leaflet";
|
||||||
|
import GeofenceLayer from "./GeofenceLayer";
|
||||||
|
|
||||||
interface MapViewProps {
|
interface MapViewProps {
|
||||||
selectedDevice: string;
|
selectedDevice: string;
|
||||||
@@ -73,6 +74,7 @@ export default function MapView({ selectedDevice, timeFilter, isPaused, filterMo
|
|||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
const [mapCenter, setMapCenter] = useState<[number, number] | null>(null);
|
const [mapCenter, setMapCenter] = useState<[number, number] | null>(null);
|
||||||
const [currentZoom, setCurrentZoom] = useState(12);
|
const [currentZoom, setCurrentZoom] = useState(12);
|
||||||
|
const [showGeofences, setShowGeofences] = useState(true);
|
||||||
const intervalRef = useRef<NodeJS.Timeout | null>(null);
|
const intervalRef = useRef<NodeJS.Timeout | null>(null);
|
||||||
|
|
||||||
// Add animation styles for latest marker
|
// Add animation styles for latest marker
|
||||||
@@ -226,7 +228,22 @@ export default function MapView({ selectedDevice, timeFilter, isPaused, filterMo
|
|||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="h-full w-full">
|
<div className="h-full w-full relative">
|
||||||
|
{/* Geofence Toggle Button */}
|
||||||
|
<div className="absolute top-4 right-4 z-[1000]">
|
||||||
|
<button
|
||||||
|
onClick={() => setShowGeofences(!showGeofences)}
|
||||||
|
className={`px-4 py-2 rounded-lg shadow-lg font-semibold text-sm transition-all ${
|
||||||
|
showGeofences
|
||||||
|
? "bg-purple-600 text-white hover:bg-purple-700"
|
||||||
|
: "bg-white text-gray-700 hover:bg-gray-100"
|
||||||
|
}`}
|
||||||
|
title={showGeofences ? "Hide Geofences" : "Show Geofences"}
|
||||||
|
>
|
||||||
|
📍 {showGeofences ? "Hide" : "Show"} Geofences
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
<MapContainer
|
<MapContainer
|
||||||
center={[48.1351, 11.582]}
|
center={[48.1351, 11.582]}
|
||||||
zoom={12}
|
zoom={12}
|
||||||
@@ -328,6 +345,9 @@ export default function MapView({ selectedDevice, timeFilter, isPaused, filterMo
|
|||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
|
|
||||||
|
{/* Geofence Layer */}
|
||||||
|
<GeofenceLayer visible={showGeofences} />
|
||||||
</MapContainer>
|
</MapContainer>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user