findCoordinates property
The findCoordinates extension uses a regular expression (coordinatesRegExp) to find coordinate pairs in the provided text. It matches coordinates in the format "(latitude, longitude)" with both positive and negative values for latitude and longitude.
==================================================================>
void main() {
String longText = """
Here are some coordinates:
(40.7128, -74.0060)
(34.0522, -118.2437)
(51.5074, -0.1278)
Don't forget (48.8566, 2.3522). """;
List
for (String coordinate in foundCoordinates) {
print(coordinate);
} }
==================================================================>
Implementation
Future<List<String>> get findCoordinates async {
final RegExp coordinatesRegExp = RegExp(
r'\((-?\d+(\.\d+)?),\s*(-?\d+(\.\d+)?)\)',
caseSensitive: false,
multiLine: true,
);
final Iterable<Match> matches = coordinatesRegExp.allMatches(this);
final List<String> coordinates = [];
for (Match match in matches) {
coordinates.add(match.group(0)!);
}
return coordinates;
}