extractPlaceName static method
Extracts place name from a Google Maps URL.
This method parses the given URL and attempts to extract the place name information embedded within it.
Returns the place name as a string if found, null otherwise.
Example usage:
final url = 'https://www.google.com/maps/place/Eiffel+Tower/@48.8583701,2.2922926,17z';
final placeName = GoogleMapsUrlExtractor.extractPlaceName(url);
if (placeName != null) {
print('Place name: $placeName');
}
Implementation
static String? extractPlaceName(String url) {
// Pattern 1: /place/PlaceName/@ format
var regex = RegExp(r'/place/([^/@]+)/@');
var match = regex.firstMatch(url);
if (match != null) {
return Uri.decodeComponent(match.group(1)!.replaceAll('+', ' '));
}
// Pattern 2: /search/PlaceName format
regex = RegExp(r'/search/([^/?]+)');
match = regex.firstMatch(url);
if (match != null) {
return Uri.decodeComponent(match.group(1)!.replaceAll('+', ' '));
}
return null;
}