google_location_extract

pub package Package CI license: MIT

Extract structured location and navigation details from Google Maps URLs in Flutter. The package understands full links, shortened links, directions, searches, place links, and URLs embedded in shared text.

No Google Maps API key is required.

Features

  • Resolve maps.app.goo.gl and legacy goo.gl/maps redirects.
  • Parse links locally when no network lookup is needed.
  • Extract coordinates, zoom, place name, address, place ID, and plus code.
  • Extract directions origin, destination, and travel mode.
  • Preserve original and resolved URLs and generate a canonical Maps URL.
  • Enrich results from JSON-LD, Open Graph, and canonical page metadata.
  • Inject an HTTP client and configure request timeout and redirect limits.
  • Convert results into a JSON-compatible map.

Installation

flutter pub add google_location_extract

Or add it manually:

dependencies:
  google_location_extract: ^1.0.1

Then run flutter pub get.

Quick start

Use GoogleMapsLocationExtractor when the input might contain a shortened URL or when page metadata should be used to obtain a more descriptive place name or address.

import 'package:google_location_extract/google_location_extract.dart';

Future<void> readSharedLocation(String sharedText) async {
  final extractor = GoogleMapsLocationExtractor();

  try {
    final location = await extractor.extract(sharedText);

    print(location.placeName);
    print(location.address);
    print('${location.latitude}, ${location.longitude}');
    print(location.canonicalUrl);
  } finally {
    extractor.close();
  }
}

The input can be either the URL itself or a message containing one:

await readSharedLocation(
  'Meet me here: https://maps.app.goo.gl/example',
);

Parse a full URL offline

GoogleMapsParser is synchronous and performs no HTTP requests:

const parser = GoogleMapsParser();
final location = parser.parse(
  'https://www.google.com/maps/place/Accra+Mall/'
  '@5.6224,-0.1731,17z',
);

if (location.hasCoordinates) {
  print(location.latitude);  // 5.6224
  print(location.longitude); // -0.1731
  print(location.zoom);      // 17.0
}

An unresolved short URL cannot be parsed offline. Pass it to GoogleMapsLocationExtractor.extract instead.

Parse directions

const parser = GoogleMapsParser();
final route = parser.parse(
  'https://www.google.com/maps/dir/?api=1'
  '&origin=Accra&destination=Tema&travelmode=driving',
);

print(route.origin);      // Accra
print(route.destination); // Tema
print(route.travelMode);  // driving

Skip page enrichment

For a full URL, set fetchPageDetails to false for offline-only parsing:

final location = await extractor.extract(
  fullGoogleMapsUrl,
  fetchPageDetails: false,
);

Short URLs still require a network request because the redirect must be resolved.

Supported inputs

Input Example data extracted
Place URLs Place name, coordinates, zoom, place ID
Viewport URLs containing @lat,lng,zoom Coordinates and zoom
Data URLs containing !3dlat!4dlng Coordinates
Search URLs using query, q, or ll Search term or coordinates
URLs with lat/latitude and lng/longitude Coordinates
Directions URLs Origin, destination, travel mode
Shortened Google Maps URLs Expanded URL and all discoverable data
Shared text containing a Maps URL The same data as the embedded URL
Google Maps page metadata Name, address, place ID, coordinates

Google domains with international suffixes are supported.

Result fields

GoogleMapsLocation is immutable and exposes:

Field Meaning
originalUrl URL found in the supplied input
resolvedUrl Final URL after redirects
canonicalUrl Normalized Google Maps URL created from the result
source Whether details came from the URL, redirect, or HTML
latitude, longitude Decimal coordinates, when available
zoom Map zoom level
placeName, address Human-readable location details
placeId, plusCode Google place ID and Open Location Code
searchQuery Search value present in the URL
origin, destination Directions endpoints
travelMode Requested directions mode
metadata Recognized extra values such as layer and map action
hasCoordinates Whether both coordinate values are present
isDirections Whether directions data is present

Use location.toJson() to obtain a JSON-compatible Map<String, Object?>.

Configuration and testing

Customize network behavior through the extractor constructor:

final extractor = GoogleMapsLocationExtractor(
  timeout: const Duration(seconds: 8),
  maxRedirects: 5,
);

An http.Client can be injected for testing or custom transport behavior:

final extractor = GoogleMapsLocationExtractor(client: myHttpClient);

Injected clients remain owned by the caller. extractor.close() only closes a client created internally by the extractor.

Error handling

try {
  final location = await extractor.extract(input);
  print(location.toJson());
} on LocationExtractFormatException catch (error) {
  print('Unsupported input: ${error.message}');
} on LocationExtractNetworkException catch (error) {
  print('Resolution failed: ${error.message}');
  print(error.cause);
}
  • LocationExtractFormatException indicates invalid input, a non-Google URL, or an unresolved short URL passed to the synchronous parser.
  • LocationExtractNetworkException indicates that a short URL could not be resolved because of a timeout, redirect, or HTTP failure.

For full URLs, failed HTML enrichment falls back to the data already available in the URL.

Platform and privacy notes

  • URL-only parsing works anywhere Dart and Flutter run.
  • Redirect resolution and HTML enrichment require internet access.
  • Browser builds can be limited by Google Maps cross-origin request policies; parse full URLs offline or resolve them through a backend when necessary.
  • This package does not access device GPS and requires no location permission.
  • The package does not retain input. Network extraction sends the Maps URL to Google only as part of resolving or downloading that URL.

Limitations

Google can change its URL and page formats without notice. URL parsing is deterministic, while HTML enrichment is best effort. Data that Google exposes only through official services—such as complete Places records, live traffic, or route calculations—still requires the relevant Google Maps Platform API and credentials.

More examples

See example/google_location_extract_example.dart for a complete runnable example.

Contributing

Issues and pull requests are welcome at the GitHub repository. Before submitting a change, run:

dart format --output=none --set-exit-if-changed .
dart analyze
flutter test

License

Licensed under the MIT License.

Libraries

google_location_extract
Extracts structured location information from Google Maps URLs.