Skip to content

Mastering Geographical Calculations in Dart with latlong2

Geographical calculations play a crucial role in many applications, from location-based services to mapping functionalities. When working with Dart, having a reliable library for latitude and longitude computations can significantly streamline your development process. Enter latlong2 – a lightweight yet powerful Dart library designed for precisely that purpose.

What is latlong2?

latlong2 is a Dart library crafted to handle common latitude and longitude calculations with ease. It builds upon the foundation set by its predecessor, latlong, ensuring compatibility with modern Dart language features and platforms like Android, iOS, and web applications.

Key Features of latlong2

1. Distance Calculation

One of the core functionalities of latlong2 is its ability to compute distances between geographical points using both the Haversine and Vincenty algorithms. These algorithms differ in speed and accuracy:

  • Haversine Algorithm: Provides faster calculations suitable for applications where speed is prioritized.

  • Vincenty Algorithm: Offers higher accuracy, making it ideal for applications requiring precise distance measurements over long distances.

Here’s how you can use latlong2 to calculate distances:

import 'package:latlong2/latlong.dart';

void main() {
  final Distance distance = new Distance();

  // Calculate distance in kilometers
  final int km =
    new LatLng(52.518611, 13.408056),
    new LatLng(51.519475, 7.46694444),

  // Calculate distance in meters
  final int meter = distance(
    new LatLng(52.518611, 13.408056),
    new LatLng(51.519475, 7.46694444),

  print('Distance in kilometers: $km');
  print('Distance in meters: $meter');

2. Offset Calculation

latlong2 simplifies calculating the position offset from a given starting point. This is particularly useful for applications that require precise positioning based on geographic coordinates.

import 'package:latlong2/latlong.dart';

void main() {
  final Distance distance = const Distance();
  final num distanceInMeter = 1000; // 1000 meters

  final p1 = new LatLng(0.0, 0.0);
  final p2 = distance.offset(p1, distanceInMeter, 180); // Offset 1000 meters towards south

  print('New Position: ${p2.round()}');
  print('Formatted Position: ${p2.toSexagesimal()}');

3. Path Smoothing with Catmull-Rom Algorithm

For applications dealing with paths defined by a series of coordinates, latlong2 offers the Catmull-Rom algorithm for path smoothing. This ensures that routes appear more natural and visually appealing to users.

import 'package:latlong2/latlong.dart';

void main() {
  final List<LatLng> zigzag = [...]; // List of LatLng coordinates

  final Path path = new Path.from(zigzag);
  final Path smoothedPath = path.equalize(8, smoothPath: true);

  print('Smoothed Path Coordinates: ${smoothedPath.points}');

Getting Started with latlong2

Integrating latlong2 into your Dart project is straightforward. Simply add it as a dependency in your pubspec.yaml file:

  latlong2: ^0.9.1

Then, run flutter pub get to install the package. You’re now ready to leverage latlong2 for all your geographical calculations.


Whether you’re developing a location-based mobile app or a web-based mapping service, latlong2 provides the essential tools you need for accurate geographical computations. Its support for multiple algorithms, path smoothing capabilities, and ease of integration make it a valuable asset in any Dart developer’s toolkit.

To dive deeper into latlong2 and explore additional features, check out the lat2dev/latlong2 repository on GitHub. Start enhancing your Dart applications with precise geographical calculations today!

Leave a Reply

Your email address will not be published. Required fields are marked *