latlong_to_place 0.0.8
latlong_to_place: ^0.0.8 copied to clipboard
A Flutter library to fetch device LatLng via geolocator and convert it into rich PlaceInfo via the null-safe geocoding plugin.
import 'package:flutter/material.dart';
import 'package:latlong_to_place/latlong_to_place.dart';
void main() => runApp(const MyApp());
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final _geo = GeocodingService();
PlaceInfo? _place;
String? _error;
@override
void initState() {
super.initState();
_runAllExamples();
}
Future<void> _runAllExamples() async {
await _singleLookup();
await _batchLookup();
await _checkPermissions();
await _checkGeoFence();
await _multiStopRoute();
}
Future<void> _singleLookup() async {
try {
final info = await _geo.getPlaceInfo(28.6139, 77.2090, useCache: true);
setState(() => _place = info);
debugPrint('Single Lookup: ${info.formattedAddress}');
} catch (e) {
setState(() => _error = e.toString());
}
}
Future<void> _batchLookup() async {
final points = [LatLng(28.61, 77.20), LatLng(28.53, 77.39)];
final places = await _geo.getPlacesInfo(points);
debugPrint('Batch Lookup: ${places.map((p) => p.city).join(', ')}');
}
Future<void> _checkPermissions() async {
final status = await LocationService.checkStatus();
debugPrint('Location Status: $status');
}
Future<void> _checkGeoFence() async {
final isInside = GeoFence.isInside(
point: LatLng(28.61, 77.20),
center: LatLng(28.60, 77.19),
radiusMeters: 2000,
);
debugPrint('Is Inside Geofence: $isInside'); // Should be true
}
Future<void> _multiStopRoute() async {
final route = await RouteService.getBestRoute(
stops: [
LatLng(28.6139, 77.2090), // Delhi
LatLng(28.5355, 77.3910), // Noida
LatLng(28.4595, 77.0266), // Gurgaon
],
mode: TravelMode.driving,
);
if (route != null) {
debugPrint('Multi-Stop Route Distance: ${route.distanceKm.toStringAsFixed(2)} km');
debugPrint('Instructions: ${route.instructions.take(2).join(' | ')}...');
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('latlong_to_place v0.0.8')),
body: Center(
child: _error != null
? Text('Error: $_error')
: _place == null
? const CircularProgressIndicator()
: Padding(
padding: const EdgeInsets.all(16.0),
child: Text(
'📍 Last Lookup:\n${_place!.formattedAddress}',
textAlign: TextAlign.center,
),
),
),
floatingActionButton: FloatingActionButton(
onPressed: _runAllExamples,
child: const Icon(Icons.refresh),
),
),
);
}
}