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.
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:
- places
- geolocation
- geocoding
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 google maps api
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:
string apikey
is the only required attribute. it is the google maps api key your application is using(place) void onselected
is a callback function called when the user selects one of the autocomplete options.(place) void onsearch
is a callback function called when the user clicks on the search icon.string language
is the language used for the autocompletion. default is ‘en’ (english). check the full list of supported languages for the google maps apilatlng location
is the point around which you wish to retrieve place information. if this value is provided,radius
must be provided aswell.int radius
is the distance (in meters) within which to return place results. note that setting a radius biases results to the indicated area, but may not fully restrict results to the specified area. if this value is provided,location
must be provided aswell. see location biasing and location restrict in the google maps api documentation.bool restrictbounds
will return only those places that are strictly within the region defined bylocation
andradius
.placetype placetype
will allow you to filter places by its type. for more information on available types, check supported types for autocompletion. on default, no filters are passed to the request, which means all place types will be shown on autocompletion.
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:
description
contains the human-readable name for the returned result. for establishment results, this is usually the business name.placeid
a textual identifier that uniquely identifies a place. for more information about place ids, see the place ids overview.types
contains an array of types that apply to this place. the array can contain multiple values. learn more about place types.fulljson
has the full json response received from the places api. can be used to extract extra information. more info on the places autocomplete api documentation
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):
- [ ] make the widget more personalizable
- [ ] make this package independent of
google_maps_flutter
- [ ] allow users to close the box clicking outside of it
- [ ] proper widget dimensions for phones in portrait orientation or foldables.
Comments are closed.