processGoogleMapsUrl static method

Future<Map<String, double>?> processGoogleMapsUrl(
  1. String url
)

Processes a Google Maps URL, expanding it if it's a short URL, and then extracts coordinates.

This method combines the functionality of expandShortUrl and extractCoordinates. It first checks if the URL is a shortened Google Maps URL (containing 'goo.gl' or 'maps.app.goo.gl'). If it is, it expands the URL. Then, it attempts to extract coordinates from the resulting URL.

Returns a Map containing 'latitude' and 'longitude' keys with their respective double values if coordinates are successfully extracted. Returns null if the URL is invalid, expansion fails, or coordinates cannot be found.

Example usage:

final shortUrl = 'https://goo.gl/maps/abcdefg';
final coordinates = await GoogleMapsUrlExtractor.processGoogleMapsUrl(shortUrl);
if (coordinates != null) {
  print('Latitude: ${coordinates['latitude']}');
  print('Longitude: ${coordinates['longitude']}');
} else {
  print('Failed to process URL');
}

Implementation

static Future<Map<String, double>?> processGoogleMapsUrl(String url) async {
  try {
    // Validate URL first
    if (!isValidGoogleMapsUrl(url) && !_isValidShortUrl(url)) {
      throw const InvalidUrlException('Invalid Google Maps URL format');
    }

    if (url.contains('goo.gl') || url.contains('maps.app.goo.gl')) {
      final expandedUrl = await expandShortUrl(url);
      if (expandedUrl != null) {
        url = expandedUrl;
      } else {
        throw const UrlExpansionException('Failed to expand short URL');
      }
    }

    return extractCoordinates(url);
  } on InvalidUrlException {
    rethrow;
  } on UrlExpansionException {
    rethrow;
  } on CoordinateExtractionException {
    rethrow;
  } catch (e) {
    throw CoordinateExtractionException(
        'Failed to process Google Maps URL: $e');
  }
}