geocoding 5.0.0 copy "geocoding: ^5.0.0" to clipboard
geocoding: ^5.0.0 copied to clipboard

A Flutter Geocoding plugin which provides easy geocoding and reverse-geocoding features.

example/lib/main.dart

import 'package:baseflow_plugin_template/baseflow_plugin_template.dart';
import 'package:flutter/material.dart';
import 'package:geocoding/geocoding.dart';

void main() {
  runApp(const _GeocodingExample());
}

/// Example [Widget] showing the use of the Geocode plugin
class GeocodeWidget extends StatefulWidget {
  /// Constructs the [GeocodeWidget] class
  const GeocodeWidget({super.key});

  @override
  State<GeocodeWidget> createState() => _GeocodeWidgetState();
}

class _GeocodeWidgetState extends State<GeocodeWidget> {
  final Geocoding _geocoding = Geocoding();
  final TextEditingController _addressController = TextEditingController();
  final TextEditingController _latitudeController = TextEditingController();
  final TextEditingController _longitudeController = TextEditingController();
  String _output = '';
  Locale? _locale;

  @override
  void initState() {
    _addressController.text = 'Gronausestraat 710, Enschede';
    _latitudeController.text = '52.2165157';
    _longitudeController.text = '6.9437819';

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(24.0),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisAlignment: MainAxisAlignment.start,
        children: <Widget>[
          const Padding(
            padding: EdgeInsets.only(top: 32),
          ),
          Row(
            children: <Widget>[
              Expanded(
                child: TextField(
                  autocorrect: false,
                  controller: _latitudeController,
                  style: Theme.of(context).textTheme.bodyMedium,
                  decoration: const InputDecoration(
                    hintText: 'Latitude',
                  ),
                  keyboardType: TextInputType.number,
                ),
              ),
              const SizedBox(
                width: 20,
              ),
              Expanded(
                child: TextField(
                  autocorrect: false,
                  controller: _longitudeController,
                  style: Theme.of(context).textTheme.bodyMedium,
                  decoration: const InputDecoration(
                    hintText: 'Longitude',
                  ),
                  keyboardType: TextInputType.number,
                ),
              ),
            ],
          ),
          const Padding(
            padding: EdgeInsets.only(top: 8),
          ),
          Center(
            child: ElevatedButton(
                child: const Text('Look up address'),
                onPressed: () {
                  final latitude = double.parse(_latitudeController.text);
                  final longitude = double.parse(_longitudeController.text);

                  _geocoding
                      .placemarkFromCoordinates(
                    latitude,
                    longitude,
                    locale: _locale,
                  )
                      .then((placemarks) {
                    var output = 'No results found.';
                    if (placemarks.isNotEmpty) {
                      output = placemarks[0].toDisplayString();
                    }

                    setState(() {
                      _output = output;
                    });
                  });
                }),
          ),
          const Padding(
            padding: EdgeInsets.only(top: 32),
          ),
          TextField(
            autocorrect: false,
            controller: _addressController,
            style: Theme.of(context).textTheme.bodyMedium,
            decoration: const InputDecoration(
              hintText: 'Address',
            ),
            keyboardType: TextInputType.text,
          ),
          const Padding(
            padding: EdgeInsets.only(top: 8),
          ),
          Center(
            child: ElevatedButton(
                child: const Text('Look up location'),
                onPressed: () {
                  _geocoding
                      .locationFromAddress(
                    _addressController.text,
                    locale: _locale,
                  )
                      .then((locations) {
                    var output = 'No results found.';
                    if (locations.isNotEmpty) {
                      output = locations[0].toDisplayString();
                    }
                    setState(() {
                      _output = output;
                    });
                  });
                }),
          ),
          const Padding(
            padding: EdgeInsets.only(top: 8),
          ),
          Center(
            child: ElevatedButton(
                child: const Text('is Present'),
                onPressed: () {
                  _geocoding.isPresent().then((isPresent) {
                    var output = isPresent ? 'Is present' : 'Is not present';
                    setState(() {
                      _output = output;
                    });
                  });
                }),
          ),
          const Padding(
            padding: EdgeInsets.only(top: 8),
          ),
          Center(
              child: ElevatedButton(
                  child: const Text('Set locale en_US'),
                  onPressed: () {
                    setState(() => _locale = Locale('en_US'));
                  })),
          const Padding(
            padding: EdgeInsets.only(top: 8),
          ),
          Center(
              child: ElevatedButton(
                  child: const Text('Set locale nl_NL'),
                  onPressed: () {
                    setState(() => _locale = Locale('nl_NL'));
                  })),
          const Padding(
            padding: EdgeInsets.only(top: 8),
          ),
          Expanded(
            child: SingleChildScrollView(
              child: SizedBox(
                width: MediaQuery.of(context).size.width,
                child: Text(_output),
              ),
            ),
          )
        ],
      ),
    );
  }
}

extension _PlacemarkExtensions on Placemark {
  String toDisplayString() {
    return '''
      Name: $name, 
      Street: $street, 
      ISO Country Code: $isoCountryCode, 
      Country: $country, 
      Postal code: $postalCode, 
      Administrative area: $administrativeArea, 
      Subadministrative area: $subAdministrativeArea,
      Locality: $locality,
      Sublocality: $subLocality,
      Thoroughfare: $thoroughfare,
      Subthoroughfare: $subThoroughfare''';
  }
}

extension _LocationExtensions on Location {
  String toDisplayString() {
    return '''
      Latitude: $latitude,
      Longitude: $longitude,
      Timestamp: $timestamp''';
  }
}

class _GeocodingExample extends StatelessWidget {
  const _GeocodingExample();

  @override
  Widget build(BuildContext context) {
    return BaseflowPluginExample(
        pluginName: 'Geocoding',
        githubURL: 'https://github.com/Baseflow/flutter-geocoding',
        pubDevURL: 'https://pub.dev/packages/geocoding',
        pages: [
          ExamplePage(
            Icons.pin_drop,
            (BuildContext context) => const GeocodeWidget(),
          ),
        ]);
  }
}
1.38k
likes
160
points
588k
downloads

Documentation

API reference

Publisher

verified publisherbaseflow.com

Weekly Downloads

A Flutter Geocoding plugin which provides easy geocoding and reverse-geocoding features.

Repository (GitHub)
View/report issues
Contributing

License

MIT (license)

Dependencies

flutter, geocoding_android, geocoding_darwin, geocoding_platform_interface

More

Packages that depend on geocoding

Packages that implement geocoding