MIF_E31231623/android/wisata_app/lib/services/maps_launcher_service.dart

61 lines
1.5 KiB
Dart

import 'package:flutter/foundation.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:wisata_app/models/destination.dart';
class MapsLauncherException implements Exception {
const MapsLauncherException(this.message);
final String message;
@override
String toString() => message;
}
class MapsLauncherService {
const MapsLauncherService();
Future<void> openDestination(Destination destination) async {
if (!destination.hasCoordinates) {
throw const MapsLauncherException(
'Koordinat wisata belum tersedia.',
);
}
final query = _queryFor(destination);
final appUri = Uri(
scheme: 'geo',
host: '0,0',
queryParameters: {'q': query},
);
final webUri = Uri.https(
'www.google.com',
'/maps/search/',
{'api': '1', 'query': query},
);
debugPrint('[Maps] Buka Maps app: $appUri');
if (await launchUrl(appUri, mode: LaunchMode.externalApplication)) {
return;
}
debugPrint('[Maps] Fallback browser: $webUri');
if (await launchUrl(webUri, mode: LaunchMode.externalApplication)) {
return;
}
throw const MapsLauncherException(
'Google Maps belum bisa dibuka. Periksa aplikasi Maps atau browser.',
);
}
String _queryFor(Destination destination) {
final latitude = destination.latitude;
final longitude = destination.longitude;
if (latitude != null && longitude != null) {
return '$latitude,$longitude';
}
return '${destination.title}, ${destination.displayLocation}';
}
}