Calculating Distance using Latitude and Longitude in FlutterFlow
Distance using Latitude and Longitude
In the world of location-based applications, accurately calculating distances between two points on the Earth's surface is crucial.Whether you're building a travel app, a delivery service, or a navigation system, understanding how to compute distances using latitude and longitude coordinates is essential.
In this article, we'll delve into the details of writing a Dart function to calculate distances between two points on the Earth's surface using their latitude and longitude coordinates.
Understanding the Basics
Before we dive into the code, let's briefly understand the concepts we'll be working with:
- Latitude and Longitude: These are geographical coordinates used to specify any location on Earth. Latitude measures how far north or south a point is from the Equator, while longitude measures how far east or west a point is from the Prime Meridian.
- Haversine Formula: The Haversine formula is a popular method for calculating distances between two points on a sphere given their latitude and longitude. This formula takes into account the curvature of the Earth, making it suitable for calculating short distances.
Writing the Dart Function
Let's get into the practical part.
Below is a Dart function that implements the Haversine formula to calculate the distance between two points:
import 'dart:math'; double calculateDistance(double lat1, double lon1, double lat2, double lon2) { const earthRadius = 6371.0; // in kilometers final dLat = radians(lat2 - lat1); final dLon = radians(lon2 - lon1); final a = pow(sin(dLat / 2), 2) + cos(radians(lat1)) * cos(radians(lat2)) * pow(sin(dLon / 2), 2); final c = 2 * atan2(sqrt(a), sqrt(1 - a)); final distance = earthRadius * c; return distance; } void main() { double distance = calculateDistance(37.7749, -122.4194, 34.0522, -118.2437); print('Distance: \$distance km'); }
FlutterFlow Custom Code
import 'dart:convert'; import 'dart:math' as math; import 'package:flutter/material.dart'; import 'package:google_fonts/google_fonts.dart'; import 'package:intl/intl.dart'; import 'package:timeago/timeago.dart' as timeago; import '../../flutter_flow/lat_lng.dart'; import '../../flutter_flow/place.dart'; import '../../flutter_flow/uploaded_file.dart'; import '../../flutter_flow/custom_functions.dart'; import '/backend/backend.dart'; import 'package:cloud_firestore/cloud_firestore.dart'; import '/auth/firebase_auth/auth_util.dart'; String? distanceFunction( LatLng? start, LatLng? end, ) { /// MODIFY CODE ONLY BELOW THIS LINE // Finding distance with two latitude and longitude double earthRadius = 6371.0; // in kilometers if (start == null || end == null) { return null; } double lat1 = start.latitude; double lon1 = start.longitude; double lat2 = end.latitude; double lon2 = end.longitude; lat1 = lat1 * math.pi / 180; lon1 = lon1 * math.pi / 180; lat2 = lat2 * math.pi / 180; lon2 = lon2 * math.pi / 180; double dlon = lon2 - lon1; double dlat = lat2 - lat1; double a = math.pow(math.sin(dlat / 2), 2) + math.cos(lat1) * math.cos(lat2) * math.pow(math.sin(dlon / 2), 2); double c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a)); double distance = earthRadius * c; //return '${distance.toStringAsFixed(2)} km'; final formatter = NumberFormat("#,##0.00", "en_US"); return '${formatter.format(distance)} km'; /// MODIFY CODE ONLY ABOVE THIS LINE }
Exploring the Code
- We start by importing the 'dart:math' library to gain access to trigonometric functions like 'sin', 'cos', 'sqrt', and 'atan2'.
- The 'calculateDistance' function takes four parameters: the latitude and longitude of the two points.
- We define the Earth's radius as '6371.0' kilometers. You can adjust this value if you need distances in other units.
- The function then converts the latitude and longitude differences from degrees to radians using the 'radians' function.
- The Haversine formula calculations are performed using the converted values, and the result is returned in kilometers.
Conclusion
In this article, we've walked through the process of calculating distances between two points on the Earth's surface using their latitude and longitude coordinates.
The Dart function we've developed employs the Haversine formula to provide accurate results, which is crucial for location-based applications. By understanding the fundamentals of geographical coordinates and the Haversine formula, you're well-equipped to enhance your Dart applications with precise distance calculations.
So, whether you're building the next generation of travel apps or optimizing logistics in your business, the ability to calculate distances using latitude and longitude coordinates opens up a world of possibilities. Happy coding!