Bernardi23 / search_map_place

This is a Flutter package that uses the Google Maps API to make a TextField that tries to autocomplete places as the user types, with simple smooth animations, making a nice UI and UX.
MIT License
126 stars 96 forks source link

search_map_place

This is a Flutter package that uses the Google Maps API to make a TextField that tries to autocomplete places as the user types, with simple smooth animations, providing a nice UI and UX. This will also provide great information about the user selected place, like the coordinates, the bounds to determine the zoom of the GoogleMap widget, and so on.

example

This is an example of what can be done using this package. To see the source code, check the examples folder.

Getting Started

To install, add it to your pubspec.yaml file:

dependencies:
    search_map_place: <latest>

After that, make sure you have the following APIs activated in your Google Cloud Platform:

You can now import it to your file and use it on your app.

import 'package:search_map_place/search_map_place.dart';

How to use it

Where you want to add the search widget, call SearchMapPlaceWidget's constructor:

Widget build(BuildContext context) {
  return Scaffold(
    body: Center(
      child: SearchMapPlaceWidget(
        apiKey: // YOUR GOOGLE MAPS API KEY
      )
    )
  );
}

The constructor has 7 attributes related to the API:

The Place class

This class will be returned on the onSelected and onSearch methods. It allows us to get more information about the user selected place.

Initially, it provides you with only basic information:

However, you can get more information like the coordinates and the bounds of the place by calling

await myPlace.geolocation;

Example

Here's an example of the widget being used:

return SearchMapPlaceWidget(
    apiKey: YOUR_API_KEY,
    // The language of the autocompletion
    language: 'en',
    // The position used to give better recomendations. In this case we are using the user position
    location: userPosition.coordinates,
    radius: 30000,
    onSelected: (Place place) async {
        final geolocation = await place.geolocation;

        // Will animate the GoogleMap camera, taking us to the selected position with an appropriate zoom
        final GoogleMapController controller = await _mapController.future;
        controller.animateCamera(CameraUpdate.newLatLng(geolocation.coordinates));
        controller.animateCamera(CameraUpdate.newLatLngBounds(geolocation.bounds, 0));
    },
);

Future Implementations

There are a lot of features that can be added to this package. Here are some features I want to implement (or could be implemented by someone else):