diff --git a/.gitignore b/.gitignore index bb431f0..e669cd1 100644 --- a/.gitignore +++ b/.gitignore @@ -73,3 +73,4 @@ build/ !**/ios/**/default.pbxuser !**/ios/**/default.perspectivev3 !/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages +test/.env diff --git a/README.md b/README.md index e689b29..1de5cf5 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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)); ``` diff --git a/example/pubspec.lock b/example/pubspec.lock index 1f01dc4..6d0a430 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -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: @@ -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: @@ -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: @@ -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: @@ -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: diff --git a/lib/src/geocoding/geocoding.dart b/lib/src/geocoding/geocoding.dart index 20eb0d0..d15b509 100644 --- a/lib/src/geocoding/geocoding.dart +++ b/lib/src/geocoding/geocoding.dart @@ -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 getByPlaceId( + String placeId, { + String? language, + List? resultType, + List? 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; + } } diff --git a/lib/src/geocoding/geocoding_parameters.dart b/lib/src/geocoding/geocoding_parameters.dart index 13360a6..53f5c5f 100644 --- a/lib/src/geocoding/geocoding_parameters.dart +++ b/lib/src/geocoding/geocoding_parameters.dart @@ -118,4 +118,54 @@ class GeocodingParameters { return queryParameters; } + + static Map createGeocodingPlaceIdParameters( + String apiKEY, + String placeId, + String? language, + List? resultType, + List? locationType, + ) { + Map 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; + } } diff --git a/pubspec.lock b/pubspec.lock index 244efd9..f23c198 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -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: @@ -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: @@ -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 @@ -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: @@ -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: @@ -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: diff --git a/pubspec.yaml b/pubspec.yaml index 7d48533..d9c8dd7 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -12,6 +12,7 @@ dependencies: http: ^0.13.0 dev_dependencies: + flutter_dotenv: ^5.0.2 flutter_test: sdk: flutter diff --git a/test/.env.sample b/test/.env.sample new file mode 100644 index 0000000..451c368 --- /dev/null +++ b/test/.env.sample @@ -0,0 +1 @@ +GOOGLE_MAPS_API_KEY=Your-Key1234 \ No newline at end of file diff --git a/test/google_geocoding_test.dart b/test/google_geocoding_test.dart index 2a98b14..8d98d37 100644 --- a/test/google_geocoding_test.dart +++ b/test/google_geocoding_test.dart @@ -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); + }); + }