geocode_cache 1.0.0
geocode_cache: ^1.0.0 copied to clipboard
A Flutter package for caching geocoding and place search results locally using Hive CE storage with Haversine distance matching. Minimizes API calls by serving nearby coordinates from cache.
example/example.dart
// ignore_for_file: avoid_print
import 'package:geocode_cache/geocode_cache.dart';
/// Example demonstrating the geocode_cache package.
///
/// This example shows how to configure, initialise, and use the
/// [GeocodingService] for reverse geocoding with automatic caching.
Future<void> main() async {
// 1. Configure with custom options (optional — call before init)
GeocodingService.instance.configure(
options: const GeocodeCacheOptions(
cacheRadiusMeters: 15, // match within 15 meters
maxAge: Duration(days: 7), // expire entries after 7 days
maxCacheSize: 500, // keep up to 500 entries
userAgent: 'MyApp/1.0 (contact@example.com)',
),
);
// 2. Initialise once (opens Hive boxes, evicts stale entries)
await GeocodingService.instance.init();
// 3. Reverse geocode — first call hits the API
final address = await GeocodingService.instance
.getAddressFromCoordinates(-8.6705, 115.2126);
print('Address: $address');
// 4. Nearby coordinates → served from cache (no API call)
final nearby = await GeocodingService.instance
.getAddressFromCoordinates(-8.6706, 115.2127);
print('Nearby: $nearby'); // Same result, from cache
// 5. Place search with caching
final results = await GeocodingService.instance.searchPlaces('Ubud, Bali');
for (final place in results) {
print('Found: ${place['display_name']}');
}
// 6. Cache management
print('Cache size: ${GeocodingService.instance.cacheSize}');
print(
'Place search cache: ${GeocodingService.instance.placeSearchCacheSize}');
// Evict old entries
final evicted = await GeocodingService.instance
.evictEntriesOlderThan(const Duration(days: 30));
print('Evicted $evicted stale entries');
// 7. Cleanup when done
await GeocodingService.instance.dispose();
}