Get nearby limit users location in dart code
✅ Main method
void main() {
double userLat = 19.0760;
double userLon = 72.8777;
allUsers.sort((a, b) {
GeoPointModel locA = a[‘current_location’] as GeoPointModel;
GeoPointModel locB = b[‘current_location’] as GeoPointModel;
double distanceA = calculateDistance(userLat, userLon, locA.latitude, locA.longitude);
double distanceB = calculateDistance(userLat, userLon, locB.latitude, locB.longitude);
return distanceA.compareTo(distanceB); // Sort nearest first
});
for (var user in allUsers) {
print(user[‘name’]);
}
}
✅ GeoPointModel is model class of GeoLocation
class GeoPointModel {
final double latitude;
final double longitude;
GeoPointModel(this.latitude, this.longitude);
}
List<Map<String, dynamic>> allUsers = [
{
‘name’: ‘User A’,
‘current_location’: GeoPointModel(12.9716, 77.5946), // Bangalore
},
{
‘name’: ‘User B’,
‘current_location’: GeoPointModel(28.7041, 77.1025), // Delhi
},
{
‘name’: ‘User C’,
‘current_location’: GeoPointModel(19.0760, 72.8777), // Mumbai
},
{
‘name’: ‘User D’,
‘current_location’: GeoPointModel(13.0827, 80.2707), // Chennai
},
];
✅ Haversine Distance Function
import ‘dart:math’;
double calculateDistance(double lat1, double lon1, double lat2, double lon2) {
const earthRadius = 6371; // Earth’s radius in km
double dLat = _degToRad(lat2 — lat1);
double dLon = _degToRad(lon2 — lon1);
double a = sin(dLat / 2) * sin(dLat / 2) +
cos(_degToRad(lat1)) * cos(_degToRad(lat2)) *
sin(dLon / 2) * sin(dLon / 2);
double c = 2 * atan2(sqrt(a), sqrt(1 — a));
return earthRadius * c;
}
double _degToRad(double deg) => deg * (pi / 180);
Get Raj kumar(Mobile App Specialist)’s stories in your inbox
Join Medium for free to get updates from this writer.
The Haversine formula calculates the distance between two points on a sphere. The formula is broken down as follows:
double a = sin(dLat / 2) * sin(dLat / 2) +
cos(_degToRad(lat1)) * cos(_degToRad(lat2)) *
sin(dLon / 2) * sin(dLon / 2);sin(dLat / 2) * sin(dLat / 2): Square of the sine of half the difference in latitude.cos(_degToRad(lat1)) * cos(_degToRad(lat2)): Product of the cosines of the latitudes.sin(dLon / 2) * sin(dLon / 2): Square of the sine of half the difference in longitude.
double c = 2 * atan2(sqrt(a), sqrt(1 - a));atan2(sqrt(a), sqrt(1 - a)): This calculates the angular distance in radians between the two points.- Multiply by 2 to get the central angle.
return earthRadius * c;Multiply the central angle c by the Earth's radius (earthRadius) to get the distance in kilometers.
Summary of the Formula
The Haversine formula is:
Where:
- RR is the Earth’s radius.
- Δlat is the difference in latitude.
- Δlon is the difference in longitude.
This calculates the square of half the chord length between the two points.
✅ Sorting by Distance
Let’s assume the current user is in Mumbai (19.0760, 72.8777):
✅ Expected Output:
User C // Mumbai (self)
User A // Bangalore
User D // Chennai
User B // Delhi
✅ Summary
Use .sort() directly on the list.
Calculate distance inside the sort’s comparison function.
Use compareTo() to sort by nearest distance.







