Skip to content

Commit fc33ccb

Browse files
committed
Add geocoding request
1 parent 607718a commit fc33ccb

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

lib/src/places.dart

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import 'utils.dart';
1010
part 'places.g.dart';
1111

1212
const _placesUrl = '/place';
13+
const _geocodeUrl = '/geocode/json';
1314
const _nearbySearchUrl = '/nearbysearch/json';
1415
const _textSearchUrl = '/textsearch/json';
1516
const _detailsSearchUrl = '/details/json';
@@ -126,6 +127,23 @@ class GoogleMapsPlaces extends GoogleWebService {
126127
return _decodeDetailsResponse(await doGet(url, headers: apiHeaders));
127128
}
128129

130+
/// Returns lat and lng of a place
131+
Future<GeocodeResponse> cgetGeoodeByPlaceId(
132+
String placeId, {
133+
String? sessionToken,
134+
List<String> fields = const [],
135+
String? language,
136+
String? region,
137+
}) async {
138+
final url = buildGeocodeUrl(
139+
placeId: placeId,
140+
sessionToken: sessionToken,
141+
language: language,
142+
region: region,
143+
);
144+
return _decodeGeocodeResponse(await doGet(url, headers: apiHeaders));
145+
}
146+
129147
@deprecated
130148
Future<PlacesDetailsResponse> getDetailsByReference(
131149
String reference, {
@@ -380,6 +398,41 @@ class GoogleMapsPlaces extends GoogleWebService {
380398
.toString();
381399
}
382400

401+
String buildGeocodeUrl({
402+
required String placeId,
403+
String? sessionToken,
404+
String? language,
405+
String? region,
406+
}) {
407+
final params = <String, String>{};
408+
409+
params['placeid'] = placeId;
410+
411+
412+
if (language != null) {
413+
params['language'] = language;
414+
}
415+
416+
if (region != null) {
417+
params['region'] = region;
418+
}
419+
420+
if (apiKey != null) {
421+
params['key'] = apiKey!;
422+
}
423+
424+
if (sessionToken != null) {
425+
params['sessiontoken'] = sessionToken;
426+
}
427+
428+
return url
429+
.replace(
430+
path: '${url.path}$_geocodeUrl',
431+
queryParameters: params,
432+
)
433+
.toString();
434+
}
435+
383436
String buildAutocompleteUrl({
384437
required String input,
385438
String? sessionToken,
@@ -527,6 +580,9 @@ class GoogleMapsPlaces extends GoogleWebService {
527580
PlacesDetailsResponse _decodeDetailsResponse(Response res) =>
528581
PlacesDetailsResponse.fromJson(json.decode(res.body));
529582

583+
GeocodeResponse _decodeGeocodeResponse(Response res) =>
584+
GeocodeResponse.fromJson(json.decode(res.body));
585+
530586
PlacesAutocompleteResponse _decodeAutocompleteResponse(Response res) =>
531587
PlacesAutocompleteResponse.fromJson(json.decode(res.body));
532588
}
@@ -926,6 +982,29 @@ class PlacesDetailsResponse extends GoogleResponseStatus {
926982
Map<String, dynamic> toJson() => _$PlacesDetailsResponseToJson(this);
927983
}
928984

985+
@JsonSerializable()
986+
class GeocodeResponse extends GoogleResponseStatus {
987+
final Geometry result;
988+
989+
/// JSON html_attributions
990+
@JsonKey(defaultValue: <String>[])
991+
final List<String> htmlAttributions;
992+
993+
GeocodeResponse({
994+
required String status,
995+
String? errorMessage,
996+
required this.result,
997+
required this.htmlAttributions,
998+
}) : super(
999+
status: status,
1000+
errorMessage: errorMessage,
1001+
);
1002+
1003+
factory GeocodeResponse.fromJson(Map<String, dynamic> json) =>
1004+
_$GeocodeResponseFromJson(json);
1005+
Map<String, dynamic> toJson() => _$GeocodeResponseToJson(this);
1006+
}
1007+
9291008
@JsonSerializable()
9301009
class Review {
9311010
/// JSON author_name

lib/src/places.g.dart

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)