You are a senior Next.js developer with GIS expertise; I need your guidance to build a…
- User
You are a senior Next.js developer with GIS expertise; I need your guidance to build a district-mandal-village selection map interface. Please outline the step-by-step process to implement this with 80% map display and 20% selection panel, ensuring proper layer visibility at each selection level.
Assistant
Building a District-Mandal-Village Selection Map Interface in Next.js
As a senior Next.js developer with GIS expertise, I'll outline a comprehensive approach to building this hierarchical map selection interface with proper layer visibility management.
Step 1: Project Setup and Dependencies
# Create a new Next.js project npx create-next-app@latest district-mandal-village-map cd district-mandal-village-map # Install required dependencies npm install leaflet react-leaflet @react-leaflet/core leaflet-geosearch topojson-client npm install --save-dev @types/leaflet @types/topojson-clientStep 2: Data Preparation
-
Obtain GeoJSON/TopoJSON data for:
- District boundaries
- Mandal boundaries (within each district)
- Village boundaries (within each mandal)
-
Structure your data in a hierarchical format:
{ "districts": [ { "id": "district1", "name": "District A", "mandals": [ { "id": "mandal1", "name": "Mandal X", "villages": [ {"id": "village1", "name": "Village 1"}, {"id": "village2", "name": "Village 2"} ] } ] } ] }Step 3: Create Map Components
components/MapContainer.tsximport { MapContainer, TileLayer } from 'react-leaflet' import 'leaflet/dist/leaflet.css' interface MapContainerProps { children: React.ReactNode } export default function MapContainer({ children }: MapContainerProps) { return ( <div className="w-full h-screen"> <div className="flex h-full"> <div className="w-4/5 h-full"> <MapContainer center={[20.5937, 78.9629]} // Center on India zoom={6} style={{ height: '100%', width: '100%' }} > <TileLayer attribution='© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors' url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" /> {children} </MapContainer> </div> <div className="w-1/5 h-full bg-white p-4 overflow-auto"> {/* Selection panel will go here */} </div> </div> </div> ) }Step 4: Implement Hierarchical Layers
components/DistrictLayer.tsximport { GeoJSON } from 'react-leaflet' import { useMap } from 'react-leaflet/hooks' import { FeatureGroup } from 'leaflet' interface DistrictLayerProps { data: any onDistrictSelect: (districtId: string) => void selectedDistrict?: string } export default function DistrictLayer({ data, onDistrictSelect, selectedDistrict }: DistrictLayerProps) { const map = useMap() const districtStyle = (feature: any) => { return { weight: 2, opacity: 1, color: feature.properties.id === selectedDistrict ? '#ff7800' : '#3388ff', fillOpacity: feature.properties.id === selectedDistrict ? 0.7 : 0.2 } } const onEachDistrict = (district: any, layer: any) => { layer.on({ click: () => { onDistrictSelect(district.properties.id) map.fitBounds(layer.getBounds()) } }) } return ( <FeatureGroup> <GeoJSON data={data} style={districtStyle} onEachFeature={onEachDistrict} /> </FeatureGroup> ) }components/MandalLayer.tsxandcomponents/VillageLayer.tsxCreate similar components for mandals and villages with appropriate styling and interaction handlers.
Step 5: Create Selection Panel
components/SelectionPanel.tsxinterface SelectionPanelProps { districts: any[] selectedDistrict?: string selectedMandal?: string selectedVillage?: string onDistrictSelect: (districtId: string) => void onMandalSelect: (mandalId: string) => void onVillageSelect: (villageId: string) => void } export default function SelectionPanel({ districts, selectedDistrict, selectedMandal, selectedVillage, onDistrictSelect, onMandalSelect, onVillageSelect }: SelectionPanelProps) { return ( <div className="space-y-4"> <h2 className="text-lg font-bold">Location Selection</h2> <div> <h3 className="font-semibold mb-2">Districts</h3> <ul className="space-y-1 max-h-40 overflow-auto"> {districts.map(district => ( <li key={district.id} className={`cursor-pointer p-1 rounded ${selectedDistrict === district.id ? 'bg-blue-100' : 'hover:bg-gray-100'}`} onClick={() => onDistrictSelect(district.id)} > {district.name} </li> ))} </ul> </div> {selectedDistrict && ( <div> <h3 className="font-semibold mb-2">Mandals</h3> <ul className="space-y-1 max-h-40 overflow-auto"> {districts.find(d => d.id === selectedDistrict)?.mandals.map((mandal: any) => ( <li key={mandal.id} className={`cursor-pointer p-1 rounded ${selectedMandal === mandal.id ? 'bg-blue-100' : 'hover:bg-gray-100'}`} onClick={() => onMandalSelect(mandal.id)} > {mandal.name} </li> ))} </ul> </div> )} {selectedMandal && ( <div> <h3 className="font-semibold mb-2">Villages</h3> <ul className="space-y-1 max-h-40 overflow-auto"> {districts .find(d => d.id === selectedDistrict) ?.mandals.find((m: any) => m.id === selectedMandal) ?.villages.map((village: any) => ( <li key={village.id} className={`cursor-pointer p-1 rounded ${selectedVillage === village.id ? 'bg-blue-100' : 'hover:bg-gray-100'}`} onClick={() => onVillageSelect(village.id)} > {village.name} </li> ))} </ul> </div> )} </div> ) }Step 6: Create Main Page Component
pages/index.tsximport { useState } from 'react' import dynamic from 'next/dynamic' import MapContainer from '../components/MapContainer' import DistrictLayer from '../components/DistrictLayer' import MandalLayer from '../components/MandalLayer' import VillageLayer from '../components/VillageLayer' import SelectionPanel from '../components/SelectionPanel' // Dynamically import Leaflet components to avoid SSR issues const Map = dynamic(() => import('../components/MapContainer'), { ssr: false }) export default function Home() { const [selectedDistrict, setSelectedDistrict] = useState<string | undefined>() const [selectedMandal, setSelectedMandal] = useState<string | undefined>() const [selectedVillage, setSelectedVillage] = useState<string | undefined>() // Load your GeoJSON data here (or fetch from API) const [districtData, setDistrictData] = useState<any>(null) const [mandalData, setMandalData] = useState<any>(null) const [villageData, setVillageData] = useState<any>(null) const [districtsList, setDistrictsList] = useState<any[]>([]) // Load your data (example - replace with actual data loading) useEffect(() => { // Fetch or import your GeoJSON data import('../data/districts.json').then(data => { setDistrictData(data.default) setDistrictsList(data.default.features.map((f: any) => ({ id: f.properties.id, name: f.properties.name, mandals: f.properties.mandals || [] }))) }) }, []) const handleDistrictSelect = (districtId: string) => { setSelectedDistrict(districtId) setSelectedMandal(undefined) setSelectedVillage(undefined) // Load mandal data for this district import(`../data/mandals/${districtId}.json`).then(data => { setMandalData(data.default) }) } const handleMandalSelect = (mandalId: string) => { setSelectedMandal(mandalId) setSelectedVillage(undefined) // Load village data for this mandal import(`../data/villages/${mandalId}.json`).then(data => { setVillageData(data.default) }) } const handleVillageSelect = (villageId: string) => { setSelectedVillage(villageId) } return ( <MapContainer> {/* District Layer - always visible */} {districtData && ( <DistrictLayer data={districtData} onDistrictSelect={handleDistrictSelect} selectedDistrict={selectedDistrict} /> )} {/* Mandal Layer - visible when district is selected */} {selectedDistrict && mandalData && ( <MandalLayer data={mandalData} onMandalSelect={handleMandalSelect} selectedMandal={selectedMandal} districtId={selectedDistrict} /> )} {/* Village Layer - visible when mandal is selected */} {selectedMandal && villageData && ( <VillageLayer data={villageData} onVillageSelect={handleVillageSelect} selectedVillage={selectedVillage} mandalId={selectedMandal} /> )} {/* Selection Panel */} <div className="absolute right-0 top-0 h-full w-1/5 z-[1000]"> <SelectionPanel districts={districtsList} selectedDistrict={selectedDistrict} selectedMandal={selectedMandal} selectedVillage={selectedVillage} onDistrictSelect={handleDistrictSelect} onMandalSelect={handleMandalSelect} onVillageSelect={handleVillageSelect} /> </div> </MapContainer> ) }Step 7: Styling and UI Enhancements
- Add CSS for better visual hierarchy:
/* styles/globals.css */ .leaflet-container { background-color: #f8f9fa; } .leaflet-interactive:hover { cursor: pointer; } .selected-boundary { fill-opacity: 0.7 !important; color: #ff7800 !important; }- Add loading states and error handling for data fetching.
Step 8: Performance Optimization
- Implement dynamic loading of GeoJSON data based on current selection level
- Use TopoJSON for more efficient data transfer
- Consider using a spatial index for faster feature lookup
- Implement debouncing for rapid selections
Step 9: Additional Features
- Search functionality:
// Add to SelectionPanel const [searchTerm, setSearchTerm] = useState('') // Filter lists based on search term const filteredDistricts = districts.filter(d => d.name.toLowerCase().includes(searchTerm.toLowerCase()) )- Breadcrumbs navigation:
<div className="flex items-center space-x-2 mb-4"> {selectedDistrict && ( <> <span className="text-sm">{districts.find(d => d.id === selectedDistrict)?.name}</span> <span className="text-gray-500">/</span> </> )} {selectedMandal && ( <> <span className="text-sm"> {districts .find(d => d.id === selectedDistrict) ?.mandals.find((m: any) => m.id === selectedMandal)?.name} </span> <span className="text-gray-500">/</span> </> )} {selectedVillage && ( <span className="text-sm"> {districts .find(d => d.id === selectedDistrict) ?.mandals.find((m: any) => m.id === selectedMandal) ?.villages.find((v: any) => v.id === selectedVillage)?.name} </span> )} </div>Step 10: Deployment Considerations
- Optimize GeoJSON files for web delivery
- Consider using a CDN for map tiles
- Implement proper caching strategies
- Set up proper error boundaries
This implementation provides a complete solution with:
- 80% map display and 20% selection panel
- Proper layer visibility management at each selection level
- Interactive selection between map and panel
- Visual feedback for selected areas
- Hierarchical data structure
Would you like me to elaborate on any specific part of this implementation?
-