location_provider

pub package License: MIT

A lightweight and easy-to-use Flutter plugin for:

  • Reverse geocoding
  • Forward geocoding
  • Address formatting
  • Distance calculations
  • Coordinate extensions
  • Batch geocoding
  • Address caching

Built on top of the Flutter geocoding platform APIs.


Features

✅ Reverse geocoding
✅ Forward geocoding
✅ Address model support
✅ Coordinate extensions
✅ Distance calculation
✅ Batch APIs
✅ In-memory caching
✅ Retry support
✅ Timeout handling
✅ Backward compatibility APIs

Demo

Default


Installation

Add this to your pubspec.yaml:

dependencies:
  location_provider: 1.0.0

Then run:

flutter pub get

Platform Support

Platform Support
Android
iOS

Usage

To use this plugin, please follow the installation guide on the official geocoding plugin page.

NOTE: This plugin relies on the AndroidX version of the Android Support Libraries. This means you need to make sure your Android project is also upgraded to support AndroidX. Detailed instructions can be found here.

The TL;DR version is:

  1. Add the following to your "gradle.properties" file:
android.useAndroidX=true
android.enableJetifier=true
  1. Make sure you set the compileSdkVersion in your "android/app/build.gradle" file to 31:
android {
 compileSdkVersion 31

 ...
}
  1. Make sure you replace all the android. dependencies to their AndroidX counterparts (a full list can be found Android migration guide).

Basic Usage

import 'package:location_provider/location_provider.dart';

Reverse Geocoding

Convert coordinates into an address.

final address = await LocationHelper.getAddress(
  28.6139,
  77.2090,
);

print(address?.fullAddress);

Get City, State, Country

final city = await LocationHelper.getCity(
  28.6139,
  77.2090,
);

final state = await LocationHelper.getState(
  28.6139,
  77.2090,
);

final country = await LocationHelper.getCountry(
  28.6139,
  77.2090,
);

Forward Geocoding

Convert address into coordinates.

final location =
    await LocationHelper.getLocationFromAddress(
  'New Delhi',
);

print(location?.lat);
print(location?.lng);

Address Model

final address = await LocationHelper.getAddress(
  28.6139,
  77.2090,
);

print(address?.city);
print(address?.state);
print(address?.country);
print(address?.postalCode);

Address Formatting

print(address?.shortAddress);

print(address?.fullAddress);

Coordinate Extensions

final coords = (
  lat: 28.6139,
  lng: 77.2090,
);

final city = await coords.city;

final address = await coords.address;

Distance Calculation

final delhi = (
  lat: 28.6139,
  lng: 77.2090,
);

final mumbai = (
  lat: 19.0760,
  lng: 72.8777,
);

final distance =
    delhi.distanceBetween(mumbai);

print(distance);

Extension APIs

The package provides multiple Dart extensions for a cleaner and more readable API.


Coordinate Extension

Work directly with coordinates.

final coords = (
  lat: 28.6139,
  lng: 77.2090,
);

Get City

final city = await coords.city;

print(city);

Get State

final state = await coords.state;

print(state);

Get Country

final country = await coords.country;

print(country);

Get Full Address Object

final address = await coords.address;

print(address?.fullAddress);

Get Full Address String

final fullAddress =
    await coords.fullAddress;

print(fullAddress);

Get Short Address

final shortAddress =
    await coords.shortAddress;

print(shortAddress);

Calculate Distance

final distance = coords.distanceTo(
  19.0760,
  72.8777,
);

print(distance);

Address String Extension

Convert an address string into coordinates.

final result =
    await 'New Delhi'.coordinates;

print(result?.lat);
print(result?.lng);

Address Extension

Format address objects easily.

final address = await coords.address;

print(address?.formatted);

Distance Extension

Calculate distance between two coordinates.

final delhi = (
  lat: 28.6139,
  lng: 77.2090,
);

final mumbai = (
  lat: 19.0760,
  lng: 72.8777,
);

final distance =
    delhi.distanceBetween(mumbai);

print(distance);

Batch APIs

Multiple Addresses

final locations =
    await LocationHelper
        .getLocationsFromAddresses([
  'Delhi',
  'Mumbai',
  'Bangalore',
]);

Multiple Coordinates

final addresses =
    await LocationHelper.getAddressesBatch([
  (28.6139, 77.2090),
  (19.0760, 72.8777),
]);

Cache Support

The package includes internal in-memory caching for optimized reverse geocoding.

LocationHelper.maxCacheSize = 200;

Clear cache:

LocationHelper.clearCache();

Deprecated APIs

The following APIs are still supported for backward compatibility:

LocationHelper.getLatitudeFromAddress();

LocationHelper.getLongitudeFromAddress();

LocationHelper.getLocationFullAddress();

LocationHelper.getSubLocality();

LocationHelper.getLocality();

Recommended migration:

LocationHelper.getLocationFromAddress();

LocationHelper.getAddress();

LocationHelper.getFormattedAddress();

Error Handling

try {
  final address =
      await LocationHelper.getAddress(
    28.6139,
    77.2090,
  );
} catch (e) {
  print(e);
}

Notes

This plugin uses the native geocoding services provided by Android and iOS.

Rate limits may apply depending on platform restrictions.

  • Apple CoreLocation
  • Android Geocoder APIs

Example

See the /example folder for a complete Flutter example.


License

MIT License


Credits

This package extends the functionality of the Flutter geocoding plugin by providing:

  • Extension APIs
  • Address formatting
  • Distance calculations
  • Batch utilities
  • In-memory caching
  • Cleaner helper methods

Core geocoding functionality is powered by the geocoding package and native platform APIs.

Libraries

location_provider