getShortAddress static method
Gets a shortened version of the address (city, state, country).
This extracts key parts from the full address for display in limited space.
Implementation
static Future<String> getShortAddress(double lat, double lng) async {
try {
final uri = Uri.parse(
'$_nominatimEndpoint?format=json&lat=$lat&lon=$lng'
'&zoom=14&addressdetails=1&accept-language=en',
);
final response = await http
.get(
uri,
headers: {'User-Agent': _userAgent, 'Accept': 'application/json'},
)
.timeout(const Duration(seconds: 10));
if (response.statusCode == 200) {
final data = jsonDecode(response.body) as Map<String, dynamic>;
final address = data['address'] as Map<String, dynamic>?;
if (address != null) {
final parts = <String>[];
final suburb =
address['suburb'] ?? address['city'] ?? address['town'];
if (suburb != null) parts.add(suburb as String);
final state = address['state'];
if (state != null) parts.add(state as String);
final country = address['country'];
if (country != null) parts.add(country as String);
if (parts.isNotEmpty) {
return parts.join(', ');
}
}
final displayName = data['display_name'] as String?;
if (displayName != null) {
if (displayName.length > 50) {
return '${displayName.substring(0, 47)}...';
}
return displayName;
}
}
return 'Unknown location';
} catch (_) {
return 'Unknown location';
}
}