Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,4 @@ build/
!**/ios/**/default.pbxuser
!**/ios/**/default.perspectivev3
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
test/.env
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ The Geocoding API is a service that provides geocoding and reverse geocoding of
The following requests are available:

1. [Geocoding](https://developers.google.com/maps/documentation/geocoding/start?hl=en_GB#geocoding-request-and-response-latitudelongitude-lookup) is the process of converting addresses (like a street address) into geographic coordinates (like latitude and longitude), which you can use to place markers on a map, or position the map.
2. [Reverse geocoding](https://developers.google.com/maps/documentation/geocoding/start?hl=en_GB#reverse) is the process of converting geographic coordinates into a human-readable address.
2. [Geocoding by PlaceId](https://developers.google.com/maps/documentation/geocoding/overview#place-id) is the process of retrieving the address for a place ID. The place ID is a unique identifier that can be used with other Google APIs.
3. [Reverse geocoding](https://developers.google.com/maps/documentation/geocoding/start?hl=en_GB#reverse) is the process of converting geographic coordinates into a human-readable address.

You can also use the Geocoding API to find the address for a given place ID.

Expand Down Expand Up @@ -39,12 +40,19 @@ To use this plugin, add **google_geocoding** as a [dependency in your pubspec.ya

```dart
var googleGeocoding = GoogleGeocoding("Your-Key");
var risult = await googleGeocoding.geocoding.get("1600 Amphitheatre",null);
var result = await googleGeocoding.geocoding.get("1600 Amphitheatre",null);
```

- Geocoding by Place ID

```dart
var googleGeocoding = GoogleGeocoding("Your-Key");
var result = await googleGeocoding.geocoding.getByPlaceId("ChIJ3S-JXmauEmsRUcIaWtf4MzE");
```

- Reverse geocoding

```dart
var googleGeocoding = GoogleGeocoding("Your-Key");
var risult = await googleGeocoding.geocoding.getReverse(LatLon(40.714224,-73.961452));
var result = await googleGeocoding.geocoding.getReverse(LatLon(40.714224,-73.961452));
```
10 changes: 5 additions & 5 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.5.0"
version: "2.8.1"
boolean_selector:
dependency: transitive
description:
Expand All @@ -28,7 +28,7 @@ packages:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.3.1"
clock:
dependency: transitive
description:
Expand Down Expand Up @@ -115,7 +115,7 @@ packages:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
version: "1.7.0"
path:
dependency: transitive
description:
Expand All @@ -141,7 +141,7 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
version: "1.8.1"
stack_trace:
dependency: transitive
description:
Expand Down Expand Up @@ -176,7 +176,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.19"
version: "0.4.2"
typed_data:
dependency: transitive
description:
Expand Down
29 changes: 29 additions & 0 deletions lib/src/geocoding/geocoding.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,33 @@ class Geocoding {
}
return null;
}

/// Retrieving an address for a Place ID
/// Required parameters:
///
/// [placeId] — The place ID of the place for which you wish to obtain the human-readable address. The place ID is a unique
/// identifier that can be used with other Google APIs. For example, you can use the placeID returned by the Roads API to
/// get the address for a snapped point. For more information about place IDs, see the place ID overview.
///
/// The optional parameters are the same as those for [Geocoding].
Future<GeocodingResponse?> getByPlaceId(
String placeId, {
String? language,
List<String>? resultType,
List<String>? locationType,
}) async {
var queryParameters = GeocodingParameters.createGeocodingPlaceIdParameters(
apiKEY,
placeId,
language,
resultType,
locationType,
);
var uri = Uri.https(_authority, _unencodedPath, queryParameters);
var response = await NetworkUtility.fetchUrl(uri);
if (response != null) {
return GeocodingResponse.parseGeocodingResponse(response);
}
return null;
}
}
50 changes: 50 additions & 0 deletions lib/src/geocoding/geocoding_parameters.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,54 @@ class GeocodingParameters {

return queryParameters;
}

static Map<String, String> createGeocodingPlaceIdParameters(
String apiKEY,
String placeId,
String? language,
List<String>? resultType,
List<String>? locationType,
) {
Map<String, String> queryParameters = {
'key': apiKEY,
'place_id': placeId,
};

if (language != null && language != '') {
var item = {
'language': language,
};
queryParameters.addAll(item);
}

if (resultType != null && resultType.length > 0) {
String result = '';
for (int i = 0; i < resultType.length; i++) {
result += '${resultType[i]}';
if (i + 1 != resultType.length) {
result += '|';
}
}
var item = {
'result_type': result,
};
queryParameters.addAll(item);
}

if (locationType != null && locationType.length > 0) {
String result = '';
for (int i = 0; i < locationType.length; i++) {
result += '${locationType[i]}';
if (i + 1 != locationType.length) {
result += '|';
}
}
var item = {
'location_type': result,
};
queryParameters.addAll(item);
}

return queryParameters;
}
}
17 changes: 12 additions & 5 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.5.0"
version: "2.8.1"
boolean_selector:
dependency: transitive
description:
Expand All @@ -28,7 +28,7 @@ packages:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.3.1"
clock:
dependency: transitive
description:
Expand All @@ -55,6 +55,13 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_dotenv:
dependency: "direct dev"
description:
name: flutter_dotenv
url: "https://pub.dartlang.org"
source: hosted
version: "5.0.2"
flutter_test:
dependency: "direct dev"
description: flutter
Expand Down Expand Up @@ -87,7 +94,7 @@ packages:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
version: "1.7.0"
path:
dependency: transitive
description:
Expand All @@ -113,7 +120,7 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
version: "1.8.1"
stack_trace:
dependency: transitive
description:
Expand Down Expand Up @@ -148,7 +155,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.19"
version: "0.4.2"
typed_data:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies:
http: ^0.13.0

dev_dependencies:
flutter_dotenv: ^5.0.2
flutter_test:
sdk: flutter

Expand Down
1 change: 1 addition & 0 deletions test/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GOOGLE_MAPS_API_KEY=Your-Key1234
34 changes: 32 additions & 2 deletions test/google_geocoding_test.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,40 @@
import 'dart:io';

import 'package:flutter_test/flutter_test.dart';
import 'package:google_geocoding/google_geocoding.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';

String defaultKey = "Your-Key";
String apiKey = defaultKey;

void main() async {
try {
dotenv.testLoad(fileInput: File('test/.env').readAsStringSync());
apiKey = dotenv.env['GOOGLE_MAPS_API_KEY'] ?? defaultKey;
} catch (e) {
print("No API key set in test/.env file, skipping integration tests");
}

void main() {
test('adds one to input values', () {
String apiKey = "Your-Key";
var googleGeocoding = GoogleGeocoding(apiKey);
expect(googleGeocoding.apiKEY, apiKey);
});

if (apiKey == defaultKey) return;

group('Integration Tests', () => integrationTests());
}

void integrationTests() {
print("Running integration tests...");

test('placeId lookup', () async {
var googleGeocoding = GoogleGeocoding(apiKey);
String placeId = "ChIJ3S-JXmauEmsRUcIaWtf4MzE";
String address = "Bennelong Point, Sydney NSW 2000, Australia";
GeocodingResponse? response = await googleGeocoding.geocoding.getByPlaceId(placeId);
expect(response?.results?.length ?? 0, 1);
expect(response?.results?.first.formattedAddress ?? "", address);
});

}