Skip to content

A backend utility to suggest the nearest coordinates (such as a healthcare center) using the Haversine formula based on latitude and longitude, retrieved through pincode geocoding. Ideal for geospatial matching systems like smart healthcare, logistics, or location-aware services.

Notifications You must be signed in to change notification settings

adityasurya4103/Suggest-Nearest-Coordinates-Using-Haversine-Formula-With-Latitude-Longitude

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

12 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Suggest Nearest Coordinates Using Haversine Formula With Latitude And Longitude

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.


πŸš€ Features

  • 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

πŸ“¦ Installation

1. Clone the repository

git clone https://github.com/your-username/SuggestNearestCoordinatesUsingHaversineWithLatitudeAndLongitude.git
cd SuggestNearestCoordinatesUsingHaversineWithLatitudeAndLongitude

2. Install dependencies

npm install

3. Setup Environment Variables

Create a .env file in the root directory and add the following:

GEOAPIFY_API_KEY=your_api_key_here

πŸ’‘ Replace your_api_key_here with your actual API key from Geoapify.


πŸ“‚ Folder Structure

.
β”œβ”€β”€ 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

🧠 How It Works

  1. Input: User provides a pincode (e.g., 411001).
  2. Geocoding: The pincode is sent to Geoapify API to fetch latitude and longitude.
  3. Distance Calculation: The system calculates the Haversine distance between user coordinates and all saved healthcare centers.
  4. Nearest Match: Returns the centerId of the nearest center.

πŸ›  Example Usage

const { assignNearestHealthCareCenter } = require('./utils/assignNearest');

(async () => {
  const nearest = await assignNearestHealthCareCenter("411001");
  console.log("Nearest Center ID:", nearest);
})();

πŸ§ͺ Sample MongoDB Document (healthCareCenter)

{
  "centerId": "HC123",
  "name": "Primary Health Center",
  "location": {
    "latitude": 19.7515,
    "longitude": 75.7139
  }
}

⚠️ Note

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.

About

A backend utility to suggest the nearest coordinates (such as a healthcare center) using the Haversine formula based on latitude and longitude, retrieved through pincode geocoding. Ideal for geospatial matching systems like smart healthcare, logistics, or location-aware services.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%