Skip to content

Commit

Permalink
Add geocoding request
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjiFarquhar committed May 31, 2023
1 parent 607718a commit fc33ccb
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
79 changes: 79 additions & 0 deletions lib/src/places.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'utils.dart';
part 'places.g.dart';

const _placesUrl = '/place';
const _geocodeUrl = '/geocode/json';
const _nearbySearchUrl = '/nearbysearch/json';
const _textSearchUrl = '/textsearch/json';
const _detailsSearchUrl = '/details/json';
Expand Down Expand Up @@ -126,6 +127,23 @@ class GoogleMapsPlaces extends GoogleWebService {
return _decodeDetailsResponse(await doGet(url, headers: apiHeaders));
}

/// Returns lat and lng of a place
Future<GeocodeResponse> cgetGeoodeByPlaceId(
String placeId, {
String? sessionToken,
List<String> fields = const [],
String? language,
String? region,
}) async {
final url = buildGeocodeUrl(
placeId: placeId,
sessionToken: sessionToken,
language: language,
region: region,
);
return _decodeGeocodeResponse(await doGet(url, headers: apiHeaders));
}

@deprecated
Future<PlacesDetailsResponse> getDetailsByReference(
String reference, {
Expand Down Expand Up @@ -380,6 +398,41 @@ class GoogleMapsPlaces extends GoogleWebService {
.toString();
}

String buildGeocodeUrl({
required String placeId,
String? sessionToken,
String? language,
String? region,
}) {
final params = <String, String>{};

params['placeid'] = placeId;


if (language != null) {
params['language'] = language;
}

if (region != null) {
params['region'] = region;
}

if (apiKey != null) {
params['key'] = apiKey!;
}

if (sessionToken != null) {
params['sessiontoken'] = sessionToken;
}

return url
.replace(
path: '${url.path}$_geocodeUrl',
queryParameters: params,
)
.toString();
}

String buildAutocompleteUrl({
required String input,
String? sessionToken,
Expand Down Expand Up @@ -527,6 +580,9 @@ class GoogleMapsPlaces extends GoogleWebService {
PlacesDetailsResponse _decodeDetailsResponse(Response res) =>
PlacesDetailsResponse.fromJson(json.decode(res.body));

GeocodeResponse _decodeGeocodeResponse(Response res) =>
GeocodeResponse.fromJson(json.decode(res.body));

PlacesAutocompleteResponse _decodeAutocompleteResponse(Response res) =>
PlacesAutocompleteResponse.fromJson(json.decode(res.body));
}
Expand Down Expand Up @@ -926,6 +982,29 @@ class PlacesDetailsResponse extends GoogleResponseStatus {
Map<String, dynamic> toJson() => _$PlacesDetailsResponseToJson(this);
}

@JsonSerializable()
class GeocodeResponse extends GoogleResponseStatus {
final Geometry result;

/// JSON html_attributions
@JsonKey(defaultValue: <String>[])
final List<String> htmlAttributions;

GeocodeResponse({
required String status,
String? errorMessage,
required this.result,
required this.htmlAttributions,
}) : super(
status: status,
errorMessage: errorMessage,
);

factory GeocodeResponse.fromJson(Map<String, dynamic> json) =>
_$GeocodeResponseFromJson(json);
Map<String, dynamic> toJson() => _$GeocodeResponseToJson(this);
}

@JsonSerializable()
class Review {
/// JSON author_name
Expand Down
19 changes: 19 additions & 0 deletions lib/src/places.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fc33ccb

Please sign in to comment.