extractZoomLevel static method
Extracts zoom level from a Google Maps URL.
This method parses the given URL and attempts to extract the zoom level information embedded within it.
Returns the zoom level as an integer if found, null otherwise.
Example usage:
final url = 'https://www.google.com/maps/@37.7749,-122.4194,15z';
final zoom = GoogleMapsUrlExtractor.extractZoomLevel(url);
if (zoom != null) {
print('Zoom level: $zoom');
}
Implementation
static int? extractZoomLevel(String url) {
// Pattern 1: @lat,lng,zoom format
var regex = RegExp(r'@-?\d+\.?\d*,-?\d+\.?\d*,(\d+)z?');
var match = regex.firstMatch(url);
if (match != null) {
return int.parse(match.group(1)!);
}
// Pattern 2: zoom parameter in query string
regex = RegExp(r'[?&]z=(\d+)');
match = regex.firstMatch(url);
if (match != null) {
return int.parse(match.group(1)!);
}
return null;
}