getGeocode method
GEOCODING: Retrieves geocode information from an address.
Setting geoJson
to true
will load heavy data and may slow down the API call.
Use it only if detailed geographic data is required.
Implementation
Future<FmData?> getGeocode({
bool geoJson = false,
required String address,
}) async {
address = address.trim();
if (address.isEmpty) return null;
// 1. Check local db
try {
final data = await _dbService.getGeocode(address);
if (data != null && (!geoJson || data['geojson'] != null)) {
return FmData.fromJSON(data);
}
} catch (e, st) {
_logService.logError(e, st);
}
// 2. Wait for new API call
final success = await _waitForNewRequest(RequestType.nominatim);
if (!success) return null;
// 3. Make API call
final query = {
'limit': 1,
'q': address,
'format': 'jsonv2',
'polygon_geojson': geoJson ? 1 : 0,
};
final url =
'${_searchURL.url}?${query.keys.map((k) => '$k=${query[k]}').join('&')}';
final res = await _apiService.request(
url: url,
userAgent: _userAgent ?? _systemService.userAgent,
);
// 4. Parse & save response to local db
try {
final data = (res.data as List).firstOrNull;
if (data == null) return null;
await _dbService.saveGeocode(address, data);
return FmData.fromJSON(data);
} catch (e, st) {
_logService.logError(e, st, req: address, res: res.toString());
return null;
}
}