expandShortUrl static method
A utility class for extracting location information from Google Maps URLs.
This class provides methods to expand shortened Google Maps URLs, extract coordinates from full Google Maps URLs, and process URLs to retrieve location information.
Example usage:
final url = 'https://maps.app.goo.gl/mWtb4a1cUE9zMWya7';
final coordinates = await GoogleMapsUrlExtractor.processGoogleMapsUrl(url);
if (coordinates != null) {
print('Latitude: ${coordinates['latitude']}');
print('Longitude: ${coordinates['longitude']}');
} else {
print('Failed to extract coordinates');
}
Implementation
static Future<String?> expandShortUrl(String shortUrl) async {
try {
final client = http.Client();
final request = http.Request('GET', Uri.parse(shortUrl))
..followRedirects = false;
final response = await client.send(request);
if (response.statusCode == 301 || response.statusCode == 302) {
final location = response.headers['location'];
client.close();
return location;
}
client.close();
} catch (e) {
print('Error expanding URL: $e');
}
return null;
}