google_maps_helper 2.2.0 copy "google_maps_helper: ^2.2.0" to clipboard
google_maps_helper: ^2.2.0 copied to clipboard

A flutter package that extends google_maps_flutter with essential functionalities, including autocomplete textfield for places search, polylines drawing, geocoding, and reverse geocoding.

example/main.dart

// ignore_for_file: unused_field
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:google_maps_helper/google_maps_helper.dart';

void main() => runApp(const MyApp());

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  GmhAddressData? _address;
  GoogleMapController? _controller;

  final _kSrc = const CameraPosition(
    zoom: 18,
    target: LatLng(37.4165849896396, -122.08051867783071),
  );

  final _kDest = const CameraPosition(
    zoom: 15,
    target: LatLng(37.420921119071586, -122.08535335958004),
  );

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('Google Maps Helper')),
        body: SafeArea(child: Stack(children: [_map, _searchField])),
      ),
    );
  }

  /// Google map widget
  Widget get _map {
    return GmhMap(
      mapOptions: GmhMapOptions(
        mapType: MapType.normal,
        initialCameraPosition: _kSrc,
        onTap: (pos) => _getAddress(pos),
        minMaxZoomPreference: const MinMaxZoomPreference(15, 18),
        onMapCreated: (controller) => _controller = controller,
        markers: {
          Marker(markerId: const MarkerId('src'), position: _kSrc.target),
          Marker(markerId: const MarkerId('dest'), position: _kDest.target),
        },
      ),
      polylineOptions: const GmhPolylineOptions(
        geodesic: true,
        color: Colors.blue,
        optimizeWaypoints: true,
        apiKey: '<GOOGLE_DIRECTIONS_API_KEY>',
      ),
    );
  }

  /// Auto-complete places search field
  Widget get _searchField {
    return GmhSearchField(
      selectedValue: _address,
      onSelected: (data) => _address = data,
      searchParams: const GmhSearchParams(apiKey: '<GOOGLE_PLACES_API_KEY>'),
    );
  }

  /// REVERSE GEOCODING: Get address from geocode
  Future<void> _getAddress(LatLng pos) async {
    final data = await GmhService().getAddress(
      lat: pos.latitude,
      lng: pos.longitude,
      apiKey: '<GOOGLE_GEOCODING_API_KEY>',
    );
    if (kDebugMode) print(data?.address);
    if (data != null) _getGeocode(data.address);
  }

  /// GEOCODING: Get geocode from an address
  Future<void> _getGeocode(String address) async {
    final data = await GmhService().getGeocode(
      address: address,
      apiKey: '<GOOGLE_GEOCODING_API_KEY>',
    );
    if (kDebugMode) print('${data?.lat},${data?.lng}');
  }
}
7
likes
150
points
59
downloads

Publisher

verified publishervalueoutput.com

Weekly Downloads

A flutter package that extends google_maps_flutter with essential functionalities, including autocomplete textfield for places search, polylines drawing, geocoding, and reverse geocoding.

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

flutter, google_maps_flutter, http

More

Packages that depend on google_maps_helper