Skip to content

Commit

Permalink
query autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
lejard-h committed Sep 5, 2017
1 parent bd714f7 commit a30273a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ Google Maps Web Services [API](https://developers.google.com/maps/web-services)
- [ ] add
- [ ] delete
- [ ] photo
- [ ] autocomplete
- [ ] queryautocomplete
- [x] autocomplete
- [x] queryautocomplete
- [ ] [Directions](https://developers.google.com/maps/documentation/directions/)
- [ ] [Distance Matrix](https://developers.google.com/maps/documentation/distance-matrix/)
- [ ] [Geolocation](https://developers.google.com/maps/documentation/geolocation/intro)
Expand Down
30 changes: 30 additions & 0 deletions lib/src/places.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const _nearbySearchUrl = "/nearbysearch/json";
const _textSearchUrl = "/textsearch/json";
const _detailsSearchUrl = "/details/json";
const _autocompleteUrl = "/autocomplete/json";
const _queryAutocompleteUrl = "/queryautocomplete/json";

/// https://developers.google.com/places/web-service/
class GoogleMapsPlaces extends GoogleWebService {
Expand Down Expand Up @@ -114,6 +115,17 @@ class GoogleMapsPlaces extends GoogleWebService {
return _decodeAutocompleteResponse(await _doGet(url));
}

Future<PlacesAutocompleteResponse> queryAutocomplete(String input,
{num offset, Location location, num radius, String language}) async {
final url = buildQueryAutocompleteUrl(
input: input,
location: location,
offset: offset,
radius: radius,
language: language);
return _decodeAutocompleteResponse(await _doGet(url));
}

String buildNearbySearchUrl(
{Location location,
num radius,
Expand Down Expand Up @@ -223,6 +235,24 @@ class GoogleMapsPlaces extends GoogleWebService {
return "$url$_autocompleteUrl?${buildQuery(params)}";
}

String buildQueryAutocompleteUrl(
{String input,
num offset,
Location location,
num radius,
String language}) {
final params = {
"key": apiKey,
"input": input != null ? Uri.encodeComponent(input) : null,
"language": language,
"location": location,
"radius": radius,
"offset": offset
};

return "$url$_queryAutocompleteUrl?${buildQuery(params)}";
}

Future<Response> _doGet(String url) => httpClient.get(url);
PlacesSearchResponse _decodeSearchResponse(Response res) =>
new PlacesSearchResponse.fromJson(JSON.decode(res.body));
Expand Down
39 changes: 38 additions & 1 deletion test/places_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,44 @@ launch([Client client]) async {
});
});

group("queryautocomplete", () {});
group("queryautocomplete", () {
test("basic", () {
expect(
places.buildQueryAutocompleteUrl(input: "Amoeba"),
equals(
"https://maps.googleapis.com/maps/api/place/queryautocomplete/json?key=$apiKey&input=Amoeba"));
});

test("with offset", () {
expect(
places.buildQueryAutocompleteUrl(input: "Amoeba", offset: 3),
equals(
"https://maps.googleapis.com/maps/api/place/queryautocomplete/json?key=$apiKey&input=Amoeba&offset=3"));
});

test("with location", () {
expect(
places.buildQueryAutocompleteUrl(
input: "Amoeba",
location: new Location(-33.8670522, 151.1957362)),
equals(
"https://maps.googleapis.com/maps/api/place/queryautocomplete/json?key=$apiKey&input=Amoeba&location=-33.8670522,151.1957362"));
});

test("with radius", () {
expect(
places.buildQueryAutocompleteUrl(input: "Amoeba", radius: 500),
equals(
"https://maps.googleapis.com/maps/api/place/queryautocomplete/json?key=$apiKey&input=Amoeba&radius=500"));
});

test("with language", () {
expect(
places.buildQueryAutocompleteUrl(input: "Amoeba", language: "fr"),
equals(
"https://maps.googleapis.com/maps/api/place/queryautocomplete/json?key=$apiKey&input=Amoeba&language=fr"));
});
});

test("decode response", () {
PlacesSearchResponse response =
Expand Down

0 comments on commit a30273a

Please sign in to comment.