102 lines
2.8 KiB
Dart
102 lines
2.8 KiB
Dart
import 'package:url_launcher/url_launcher.dart';
|
|
import '../../core/constants/app_constants.dart';
|
|
|
|
class NavigationService {
|
|
static final NavigationService _instance = NavigationService._internal();
|
|
factory NavigationService() => _instance;
|
|
NavigationService._internal();
|
|
|
|
Future<bool> openGoogleMapsNavigation({
|
|
required double destinationLat,
|
|
required double destinationLng,
|
|
double? originLat,
|
|
double? originLng,
|
|
}) async {
|
|
try {
|
|
// Try Google Maps first
|
|
final googleMapsUrl = Uri.parse(
|
|
'${AppConstants.googleMapsUrlScheme}?daddr=$destinationLat,$destinationLng' +
|
|
(originLat != null && originLng != null
|
|
? '&saddr=$originLat,$originLng'
|
|
: '') +
|
|
'&directionsmode=driving',
|
|
);
|
|
|
|
if (await canLaunchUrl(googleMapsUrl)) {
|
|
await launchUrl(googleMapsUrl);
|
|
return true;
|
|
}
|
|
|
|
// Fallback to web URL if Google Maps app is not installed
|
|
final webUrl = Uri.parse(
|
|
'https://www.google.com/maps/dir/?api=1' +
|
|
'&destination=$destinationLat,$destinationLng' +
|
|
(originLat != null && originLng != null
|
|
? '&origin=$originLat,$originLng'
|
|
: '') +
|
|
'&travelmode=driving',
|
|
);
|
|
|
|
if (await canLaunchUrl(webUrl)) {
|
|
await launchUrl(webUrl, mode: LaunchMode.externalApplication);
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
} catch (e) {
|
|
print('Error opening navigation: $e');
|
|
return false;
|
|
}
|
|
}
|
|
|
|
Future<bool> openAppleMapsNavigation({
|
|
required double destinationLat,
|
|
required double destinationLng,
|
|
String? destinationName,
|
|
}) async {
|
|
try {
|
|
final appleMapsUrl = Uri.parse(
|
|
'${AppConstants.appleMapsUrlScheme}?daddr=$destinationLat,$destinationLng' +
|
|
(destinationName != null ? '&q=$destinationName' : ''),
|
|
);
|
|
|
|
if (await canLaunchUrl(appleMapsUrl)) {
|
|
await launchUrl(appleMapsUrl);
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
} catch (e) {
|
|
print('Error opening Apple Maps: $e');
|
|
return false;
|
|
}
|
|
}
|
|
|
|
Future<bool> openNavigation({
|
|
required double destinationLat,
|
|
required double destinationLng,
|
|
double? originLat,
|
|
double? originLng,
|
|
String? destinationName,
|
|
}) async {
|
|
// Try Google Maps first
|
|
final googleMapsSuccess = await openGoogleMapsNavigation(
|
|
destinationLat: destinationLat,
|
|
destinationLng: destinationLng,
|
|
originLat: originLat,
|
|
originLng: originLng,
|
|
);
|
|
|
|
if (googleMapsSuccess) {
|
|
return true;
|
|
}
|
|
|
|
// Try Apple Maps as fallback
|
|
return await openAppleMapsNavigation(
|
|
destinationLat: destinationLat,
|
|
destinationLng: destinationLng,
|
|
destinationName: destinationName,
|
|
);
|
|
}
|
|
}
|