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 {
  if (url.contains('goo.gl') || url.contains('maps.app.goo.gl')) {
    final expandedUrl = await expandShortUrl(url);
    if (expandedUrl != null) {
      url = expandedUrl;
    } else {
      print('Failed to expand short URL');
      return null;
    }
  }

  return extractCoordinates(url);
}