Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support evolutions and lack of homogeneity in opencage resposes #5

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
5 changes: 4 additions & 1 deletion example/dump.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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}');
Expand Down Expand Up @@ -69,4 +72,4 @@ class Dumper {
print(' thousandsSeparator: ${r.annotations?.currency?.thousandsSeparator}');
});
}
}
}
49 changes: 28 additions & 21 deletions lib/src/geocoder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<GeocoderResponse> geocode(String query,
{String language = "en",
{String language = 'en',
String countryCode = null,
Bounds bounds = null,
bool abbrv = false,
Expand All @@ -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,
Expand All @@ -40,7 +41,8 @@ class Geocoder {
'add_request': addRequest,
'no_dedupe': noDedupe,
'no_record': noRecord,
'abbrv': abbrv
'roadinfo': roadInfo,
'abbrv': abbrv,
});
}

Expand All @@ -50,7 +52,7 @@ class Geocoder {
Future<GeocoderResponse> reverseGeocode(
double latitude,
double longitude, {
String language = "en",
String language = 'en',
String countryCode = null,
Bounds bounds = null,
bool abbrv = false,
Expand All @@ -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<GeocoderResponse> _send(Map args) async {
Expand All @@ -91,18 +95,21 @@ 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());
}

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();
Expand Down
7 changes: 7 additions & 0 deletions lib/src/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
);
Expand Down
6 changes: 6 additions & 0 deletions lib/src/model.g.dart

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