processGoogleMapsUrl static method

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

Enhanced URL processing with multiple fallback strategies

Implementation

static Future<Map<String, double>?> processGoogleMapsUrl(String url) async {
  try {
    String processedUrl = url.trim();

    // Handle different URL schemes
    if (!processedUrl.startsWith('http')) {
      if (processedUrl.startsWith('maps.google.com') ||
          processedUrl.startsWith('google.com/maps')) {
        processedUrl = 'https://$processedUrl';
      }
    }

    // First, try direct coordinate extraction
    Map<String, double>? coordinates = extractCoordinates(processedUrl);
    if (coordinates != null) {
      return coordinates;
    }

    // If it's a shortened URL, expand it first
    if (processedUrl.contains('goo.gl') ||
        processedUrl.contains('maps.app.goo.gl') ||
        processedUrl.contains('t.co') ||
        processedUrl.contains('bit.ly')) {
      print('Attempting to expand shortened URL...');
      final String? expandedUrl = await expandShortUrl(processedUrl);

      if (expandedUrl != null) {
        print('Expanded URL: $expandedUrl');
        coordinates = extractCoordinates(expandedUrl);
        if (coordinates != null) {
          return coordinates;
        }

        // Try expanding again if it's still a short URL
        if (expandedUrl.contains('goo.gl') ||
            expandedUrl.contains('maps.app.goo.gl')) {
          final String? doubleExpandedUrl = await expandShortUrl(expandedUrl);
          if (doubleExpandedUrl != null) {
            print('Double expanded URL: $doubleExpandedUrl');
            coordinates = extractCoordinates(doubleExpandedUrl);
            if (coordinates != null) {
              return coordinates;
            }
          }
        }
      } else {
        print('Failed to expand short URL');
      }
    }

    return null;
  } catch (e) {
    print('Error processing Google Maps URL: $e');
    return null;
  }
}