This Repository contains the backend logic for Immuverse project for suggesting the nearest healthcare center
A backend utility to suggest the nearest coordinates (like a healthcare center) using the Haversine formula with latitude and longitude, retrieved from a userβs pincode via Geoapify. Ideal for geolocation-based services in healthcare, logistics, or smart city systems.
- Convert pincode to latitude and longitude using Geoapify Geocoding API
- Use the Haversine formula to calculate spherical distance
- Find and return the nearest location from a database
- Built with Node.js, MongoDB, and Axios
git clone https://github.com/your-username/SuggestNearestCoordinatesUsingHaversineWithLatitudeAndLongitude.git
cd SuggestNearestCoordinatesUsingHaversineWithLatitudeAndLongitudenpm installCreate a .env file in the root directory and add the following:
GEOAPIFY_API_KEY=your_api_key_hereπ‘ Replace
your_api_key_herewith your actual API key from Geoapify.
.
βββ models/
β βββ healthCareCenter.js # MongoDB schema for healthcare centers
βββ utils/
β βββ assignNearest.js # Core logic using Haversine formula
βββ .env # API key (not committed to Git)
βββ package.json
βββ README.md
- Input: User provides a pincode (e.g.,
411001). - Geocoding: The pincode is sent to Geoapify API to fetch latitude and longitude.
- Distance Calculation: The system calculates the Haversine distance between user coordinates and all saved healthcare centers.
- Nearest Match: Returns the
centerIdof the nearest center.
const { assignNearestHealthCareCenter } = require('./utils/assignNearest');
(async () => {
const nearest = await assignNearestHealthCareCenter("411001");
console.log("Nearest Center ID:", nearest);
})();{
"centerId": "HC123",
"name": "Primary Health Center",
"location": {
"latitude": 19.7515,
"longitude": 75.7139
}
}This repository demonstrates only a specific functionality of a larger project.
It focuses solely on suggesting the nearest location using Haversine formula and latitude-longitude-based logic.
π§© This code does not include
app.js,index.js, or routing/server logic.
It is meant to be integrated into a broader Node.js application.