ubhub_maps 0.2.1
ubhub_maps: ^0.2.1 copied to clipboard
Official Dart SDK for the UBHUB Geospatial API, with typed geocoding, reverse geocoding, routing, and secure API-key authentication.
example/main.dart
// Run: dart run example/main.dart
// Set UBHUB_API_KEY in your process environment before running.
import 'dart:io';
import 'package:ubhub_maps/ubhub_maps.dart';
Future<void> main() async {
final key = Platform.environment['UBHUB_API_KEY'];
if (key == null || key.isEmpty) {
stderr.writeln('Set UBHUB_API_KEY in the process environment.');
exitCode = 64;
return;
}
final baseUrl = Platform.environment['UBHUB_BASE_URL'];
final client = baseUrl == null
? UbHubGeo(apiKey: key)
: UbHubGeo(apiKey: key, baseUrl: baseUrl);
try {
await demonstrate(client, write: stdout.writeln);
} on RateLimitException catch (e) {
stderr.writeln(
'Rate limited. Retry after: ${e.retryAfter}. Request: ${e.requestId}');
exitCode = 1;
} on UbHubException catch (e) {
stderr.writeln('${e.message} Request: ${e.requestId}');
exitCode = 1;
} finally {
client.close();
}
}
/// Runs the three services; injectable output keeps this example testable.
Future<void> demonstrate(UbHubGeo client,
{required void Function(String) write}) async {
final places = await client.geocoding.search('Sukhbaatar Square', limit: 5);
for (final place in places.results) {
write(
'${place.name}: ${place.address ?? "No address"} (${place.location})');
}
const origin = GeoPoint(latitude: 47.9184, longitude: 106.9177);
const destination = GeoPoint(latitude: 47.9220, longitude: 106.9057);
final nearest = await client.geocoding.reverse(location: origin);
write(nearest.results.isEmpty
? 'No nearby address.'
: 'Nearest: ${nearest.results.first.name}');
final route =
await client.routing.route(origin: origin, destination: destination);
write(
'Route: ${route.route.distanceMeters} m, ${route.route.durationSeconds} s');
}