latlong_to_place 0.0.8 copy "latlong_to_place: ^0.0.8" to clipboard
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.

example/lib/main.dart

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),
        ),
      ),
    );
  }
}
4
likes
150
points
253
downloads

Publisher

verified publisherarpitjai.com

Weekly Downloads

A Flutter library to fetch device LatLng via geolocator and convert it into rich PlaceInfo via the null-safe geocoding plugin.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter, geocoding, geolocator, http, latlong2

More

Packages that depend on latlong_to_place