launchMap function
dynamic
launchMap(
- Uri? uri
)
Implementation
launchMap(Uri? uri) async {
if (uri == null) {
throw 'No URI provided.';
}
final availableMaps = await MapLauncher.installedMaps;
if (availableMaps.isEmpty) {
throw 'No map applications are installed on the device.';
}
Position position;
try {
position = await determinePosition();
} catch (e) {
throw 'Unable to determine current position: $e';
}
Map<String, String>? queryParameters = uri.queryParameters;
if (queryParameters["method"] == "showDirections") {
String? destination = queryParameters["destination"];
if (destination == null || !destination.contains(',')) {
throw 'Invalid or missing destination coordinates.';
}
double latitude, longitude;
try {
latitude = double.parse(destination.split(",")[0]);
longitude = double.parse(destination.split(",")[1]);
} catch (e) {
throw 'Error parsing destination coordinates: $e';
}
if (latitude < -90 ||
latitude > 90 ||
longitude < -180 ||
longitude > 180) {
throw 'Invalid latitude or longitude values.';
}
await availableMaps.first.showDirections(
destination: Coords(latitude, longitude),
origin: Coords(position.latitude, position.longitude),
);
} else {
throw 'Unsupported method in URI.';
}
}