diff --git a/example/dump.dart b/example/dump.dart index 9010153..c31bc0b 100644 --- a/example/dump.dart +++ b/example/dump.dart @@ -33,11 +33,14 @@ class Dumper { print(' stateDistrict: ${r.components?.stateDistrict}'); print(' suburb: ${r.components?.suburb}'); print(' city: ${r.components?.city}'); + print(' town: ${r.components?.town}'); print(' cityDistrict: ${r.components?.cityDistrict}'); print(' politicalUnion: ${r.components?.politicalUnion}'); print(' county: ${r.components?.county}'); print(' houseNumber: ${r.components?.houseNumber}'); print(' road: ${r.components?.road}'); + print(' street: ${r.components?.street}'); + print(' postCode: ${r.components?.postCode}'); print(' annotations:'); print(' callingCode: ${r.annotations?.callingCode}'); print(' dms: ${r.annotations?.dms}'); @@ -69,4 +72,4 @@ class Dumper { print(' thousandsSeparator: ${r.annotations?.currency?.thousandsSeparator}'); }); } -} \ No newline at end of file +} diff --git a/lib/src/geocoder.dart b/lib/src/geocoder.dart index 12e7db0..f8837c0 100644 --- a/lib/src/geocoder.dart +++ b/lib/src/geocoder.dart @@ -8,18 +8,18 @@ import 'model.dart'; /// /// More details here : https://opencagedata.com/. class Geocoder { - static const String host = "https://api.opencagedata.com/geocode/v1/json"; + static const String host = 'https://api.opencagedata.com/geocode/v1/json'; final String _baseUrl; /// Create an OpenCage geocoder for the given [apiKey]. - const Geocoder(String apiKey) : this._baseUrl = "${host}?key=${apiKey}"; + const Geocoder(String apiKey) : this._baseUrl = '${host}?key=${apiKey}'; /// Get coordinates and data from a search [query]. /// /// All optional parameters are described here : https://opencagedata.com/api#request. Future geocode(String query, - {String language = "en", + {String language = 'en', String countryCode = null, Bounds bounds = null, bool abbrv = false, @@ -28,6 +28,7 @@ class Geocoder { bool noAnnotations = false, bool noDedupe = false, bool noRecord = false, + bool roadInfo = false, bool addRequest = false}) { return _send({ 'q': query, @@ -40,7 +41,8 @@ class Geocoder { 'add_request': addRequest, 'no_dedupe': noDedupe, 'no_record': noRecord, - 'abbrv': abbrv + 'roadinfo': roadInfo, + 'abbrv': abbrv, }); } @@ -50,7 +52,7 @@ class Geocoder { Future reverseGeocode( double latitude, double longitude, { - String language = "en", + String language = 'en', String countryCode = null, Bounds bounds = null, bool abbrv = false, @@ -59,21 +61,23 @@ class Geocoder { bool noAnnotations = false, bool noDedupe = false, bool noRecord = false, + bool roadInfo = false, bool addRequest = false, }) { - return geocode( - "${latitude}+${longitude}", - language: language, - countryCode: countryCode, - bounds: bounds, - abbrv: abbrv, - limit: limit, - minConfidence: minConfidence, - noAnnotations: noAnnotations, - noDedupe: noDedupe, - noRecord: noRecord, - addRequest: addRequest, - ); + return _send({ + 'q': {'latitude': latitude, 'longitude': longitude}, + 'language': language, + 'countryCode': countryCode, + 'bounds': bounds, + 'limit': limit, + 'no_annotations': noAnnotations, + 'min_confidence': minConfidence, + 'add_request': addRequest, + 'no_dedupe': noDedupe, + 'no_record': noRecord, + 'roadinfo': roadInfo, + 'abbrv': abbrv, + }); } Future _send(Map args) async { @@ -91,7 +95,7 @@ class Geocoder { queryArgs.forEach((key, value) { if (value != null && value != false) { var formattedValue = _formatQueryArgValue(value); - result.write("&${key}=${formattedValue}"); + result.write('&${key}=${formattedValue}'); } }); return Uri.parse(result.toString()); @@ -99,10 +103,13 @@ class Geocoder { String _formatQueryArgValue(Object value) { if (value is bool) { - return value ? "1" : "0"; + return value ? '1' : '0'; + } + if (value is Map) { + return '${value['latitude']}+${value['longitude']}'; } if (value is Bounds) { - return "${value.northeast.longitude}%2C${value.northeast.latitude}%2C${value.southwest.longitude}%2C${value.southwest.latitude}"; + return '${value.northeast.longitude}%2C${value.northeast.latitude}%2C${value.southwest.longitude}%2C${value.southwest.latitude}'; } if (value is int || value is double) { return value.toString(); diff --git a/lib/src/model.dart b/lib/src/model.dart index f394bec..8e7a048 100644 --- a/lib/src/model.dart +++ b/lib/src/model.dart @@ -318,11 +318,15 @@ class Currency { @JsonSerializable() class Components { + final String town; final String city; final String state; final String country; final String county; final String road; + final String street; + @JsonKey(name: 'postcode') + final String postCode; final String suburb; @JsonKey(name: 'house_number') final String houseNumber; @@ -345,9 +349,12 @@ class Components { this.stateDistrict, this.county, this.city, + this.town, this.cityDistrict, this.suburb, this.road, + this.street, + this.postCode, this.houseNumber, this.politicalUnion, ); diff --git a/lib/src/model.g.dart b/lib/src/model.g.dart index 4c0c6f3..9c8819f 100644 --- a/lib/src/model.g.dart +++ b/lib/src/model.g.dart @@ -282,10 +282,13 @@ Components _$ComponentsFromJson(Map json) { json['state'] as String, json['state_district'] as String, json['county'] as String, + json['town'] as String, json['city'] as String, json['city_district'] as String, json['suburb'] as String, json['road'] as String, + json['street'] as String, + json['postcode'] as String, json['house_number'] as String, json['political_union'] as String, ); @@ -293,11 +296,14 @@ Components _$ComponentsFromJson(Map json) { Map _$ComponentsToJson(Components instance) => { + 'town': instance.town, 'city': instance.city, 'state': instance.state, 'country': instance.country, 'county': instance.county, 'road': instance.road, + 'street': instance.street, + 'postcode': instance.postCode, 'suburb': instance.suburb, 'house_number': instance.houseNumber, 'country_code': instance.countryCode,