googel map routs to near gasstation
This commit is contained in:
@@ -9,6 +9,8 @@ class GasstationsController extends GetxController {
|
||||
final isLoadingLocation = false.obs;
|
||||
final errorMessage = ''.obs;
|
||||
final eControlData = <EControlModel>[].obs;
|
||||
double? currentLatitude;
|
||||
double? currentLongitude;
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
@@ -32,19 +34,24 @@ class GasstationsController extends GetxController {
|
||||
|
||||
// Standort-Abfrage mit Timeout versehen (z. B. 8 Sekunden),
|
||||
// falls der Browser/User bei der Rechtefreigabe blockiert oder hängt
|
||||
await geolocationService
|
||||
.getCurrentLocation()
|
||||
.timeout(
|
||||
const Duration(seconds: 8),
|
||||
onTimeout: () {
|
||||
throw TimeoutException('Standortabfrage hat zu lange gedauert.');
|
||||
},
|
||||
);
|
||||
await geolocationService.getCurrentLocation().timeout(
|
||||
const Duration(seconds: 8),
|
||||
onTimeout: () {
|
||||
throw TimeoutException('Standortabfrage hat zu lange gedauert.');
|
||||
},
|
||||
);
|
||||
|
||||
if (geolocationService.hasLocation) {
|
||||
currentLatitude = geolocationService.latitude;
|
||||
currentLongitude = geolocationService.longitude;
|
||||
// for test only später wieder deaktivieren statische koordinaten
|
||||
// currentLatitude = 47.938783246743355;
|
||||
// currentLongitude = 13.762770926423814;
|
||||
// ende Test only
|
||||
|
||||
final fuelData = await eControlService.getBothFuelData(
|
||||
geolocationService.latitude,
|
||||
geolocationService.longitude,
|
||||
currentLatitude!,
|
||||
currentLongitude!,
|
||||
);
|
||||
|
||||
List<EControlModel> dieselStations = (fuelData['DIE'] as List)
|
||||
@@ -68,7 +75,10 @@ class GasstationsController extends GetxController {
|
||||
if (combinedMap.containsKey(supStation.id)) {
|
||||
final existingPrices = combinedMap[supStation.id!]!.prices ?? [];
|
||||
final supPrices = supStation.prices ?? [];
|
||||
combinedMap[supStation.id!]!.prices = [...existingPrices, ...supPrices];
|
||||
combinedMap[supStation.id!]!.prices = [
|
||||
...existingPrices,
|
||||
...supPrices,
|
||||
];
|
||||
} else {
|
||||
combinedMap[supStation.id!] = supStation;
|
||||
}
|
||||
@@ -84,8 +94,7 @@ class GasstationsController extends GetxController {
|
||||
errorMessage.value =
|
||||
'Standortabfrage zeitlich überschritten. Bitte erlauben Sie den Standortzugriff im Browser.';
|
||||
} catch (e) {
|
||||
errorMessage.value =
|
||||
'Fehler beim Laden der Daten: ${e.toString()}';
|
||||
errorMessage.value = 'Fehler beim Laden der Daten: ${e.toString()}';
|
||||
} finally {
|
||||
// Stellt sicher, dass das Lade-Icon auf jeden Fall verschwindet
|
||||
isLoading.value = false;
|
||||
@@ -96,4 +105,4 @@ class GasstationsController extends GetxController {
|
||||
void retry() {
|
||||
_loadStationInfosList();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ class GasstationsPage extends GetView<GasstationsController> {
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
// Anzeige bei Fehler/Unerreichbarkeit des E-Control Services
|
||||
else if (staCtrl.errorMessage.isNotEmpty) {
|
||||
return Center(
|
||||
@@ -83,11 +83,7 @@ class GasstationsPage extends GetView<GasstationsController> {
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.cloud_off,
|
||||
size: 64,
|
||||
color: Colors.red[400],
|
||||
),
|
||||
Icon(Icons.cloud_off, size: 64, color: Colors.red[400]),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
'Service nicht erreichbar',
|
||||
@@ -120,7 +116,7 @@ class GasstationsPage extends GetView<GasstationsController> {
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
// Anzeige, wenn keine Tankstellen gefunden wurden
|
||||
else if (staCtrl.eControlData.isEmpty) {
|
||||
return Center(
|
||||
@@ -220,6 +216,8 @@ class GasstationsPage extends GetView<GasstationsController> {
|
||||
return GasStationCard(
|
||||
station: staCtrl.eControlData[index],
|
||||
index: index,
|
||||
originLatitude: staCtrl.currentLatitude!,
|
||||
originLongitude: staCtrl.currentLongitude!,
|
||||
);
|
||||
},
|
||||
),
|
||||
@@ -232,4 +230,4 @@ class GasstationsPage extends GetView<GasstationsController> {
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,8 +7,10 @@ class EControlService {
|
||||
double longitude,
|
||||
String fuelType,
|
||||
) async {
|
||||
String apiUrl =
|
||||
'https://api.e-control.at/sprit/1.0/search/gas-stations/by-address?latitude=$latitude&longitude=$longitude&fuelType=$fuelType&includeClosed=false';
|
||||
// For Test purposes only - later deactivate
|
||||
// latitude = 47.938783246743355;
|
||||
// longitude = 13.762770926423814;
|
||||
String apiUrl = 'https://api.e-control.at/sprit/1.0/search/gas-stations/by-address?latitude=$latitude&longitude=$longitude&fuelType=$fuelType&includeClosed=false';
|
||||
var response = await http.get(Uri.parse(apiUrl));
|
||||
if (response.statusCode == 200) {
|
||||
return jsonDecode(response.body);
|
||||
@@ -21,6 +23,9 @@ class EControlService {
|
||||
double latitude,
|
||||
double longitude,
|
||||
) async {
|
||||
// For Test purposes only - later deactivate
|
||||
// latitude = 47.938783246743355;
|
||||
// longitude = 13.762770926423814;
|
||||
final results = await Future.wait([
|
||||
getEControlData(latitude, longitude, 'DIE'),
|
||||
getEControlData(latitude, longitude, 'SUP'),
|
||||
|
||||
@@ -1,11 +1,21 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
|
||||
import '../models/econtrol_model.dart';
|
||||
|
||||
class GasStationCard extends StatelessWidget {
|
||||
final EControlModel station;
|
||||
final int index;
|
||||
final double originLatitude;
|
||||
final double originLongitude;
|
||||
|
||||
const GasStationCard({super.key, required this.station, required this.index});
|
||||
const GasStationCard({
|
||||
super.key,
|
||||
required this.station,
|
||||
required this.index,
|
||||
required this.originLatitude,
|
||||
required this.originLongitude,
|
||||
});
|
||||
|
||||
Color _getPriceColor(double? price) {
|
||||
if (price == null) return Colors.grey;
|
||||
@@ -23,6 +33,53 @@ class GasStationCard extends StatelessWidget {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _openInGoogleMaps(BuildContext context) async {
|
||||
final location = station.location;
|
||||
final destination =
|
||||
location?.latitude != null && location?.longitude != null
|
||||
? '${location!.latitude},${location.longitude}'
|
||||
: [
|
||||
station.name,
|
||||
location?.address,
|
||||
location?.postalCode,
|
||||
location?.city,
|
||||
]
|
||||
.whereType<String>()
|
||||
.where((value) => value.trim().isNotEmpty)
|
||||
.join(', ');
|
||||
|
||||
if (destination.isEmpty) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Für diese Tankstelle ist kein Standort verfügbar.'),
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
final origin = '$originLatitude,$originLongitude';
|
||||
final mapsUri = Uri.https('www.google.com', '/maps/dir/', {
|
||||
'api': '1',
|
||||
'origin': origin,
|
||||
'destination': destination,
|
||||
'travelmode': 'driving',
|
||||
});
|
||||
|
||||
final didLaunch = await launchUrl(
|
||||
mapsUri,
|
||||
mode: LaunchMode.platformDefault,
|
||||
webOnlyWindowName: '_blank',
|
||||
);
|
||||
|
||||
if (!didLaunch && context.mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Google Maps konnte nicht geöffnet werden.'),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Hilfsmethode zur Erstellung eines einzelnen Preis-Badges
|
||||
Widget _buildPriceBadge(Prices? priceInfo) {
|
||||
if (priceInfo == null || priceInfo.amount == null) {
|
||||
@@ -77,12 +134,18 @@ class GasStationCard extends StatelessWidget {
|
||||
Widget build(BuildContext context) {
|
||||
// Suchen nach Diesel und Super/Benzin im Prices-Array
|
||||
final dieselPrice = station.prices?.firstWhere(
|
||||
(p) => p.fuelType == 'DIE' || (p.label?.toLowerCase().contains('diesel') ?? false),
|
||||
orElse: () => station.prices?.isNotEmpty == true ? station.prices!.first : Prices(),
|
||||
(p) =>
|
||||
p.fuelType == 'DIE' ||
|
||||
(p.label?.toLowerCase().contains('diesel') ?? false),
|
||||
orElse: () =>
|
||||
station.prices?.isNotEmpty == true ? station.prices!.first : Prices(),
|
||||
);
|
||||
|
||||
final supPrice = station.prices?.firstWhere(
|
||||
(p) => p.fuelType == 'SUP' || (p.label?.toLowerCase().contains('benzin') ?? false) || (p.label?.toLowerCase().contains('super') ?? false),
|
||||
(p) =>
|
||||
p.fuelType == 'SUP' ||
|
||||
(p.label?.toLowerCase().contains('benzin') ?? false) ||
|
||||
(p.label?.toLowerCase().contains('super') ?? false),
|
||||
orElse: () => Prices(),
|
||||
);
|
||||
|
||||
@@ -90,167 +153,170 @@ class GasStationCard extends StatelessWidget {
|
||||
margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
||||
elevation: 4,
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
|
||||
child: Container(
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topLeft,
|
||||
end: Alignment.bottomRight,
|
||||
colors: [Colors.white, Colors.grey[50]!],
|
||||
clipBehavior: Clip.antiAlias,
|
||||
child: InkWell(
|
||||
onTap: () => _openInGoogleMaps(context),
|
||||
child: Ink(
|
||||
decoration: BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topLeft,
|
||||
end: Alignment.bottomRight,
|
||||
colors: [Colors.white, Colors.grey[50]!],
|
||||
),
|
||||
),
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// Header Row
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 12,
|
||||
vertical: 6,
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.blueGrey[700],
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
),
|
||||
child: Text(
|
||||
'#${index + 1}',
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 12,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// Header Row
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 12,
|
||||
vertical: 6,
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.blueGrey[700],
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
),
|
||||
child: Text(
|
||||
'#${index + 1}',
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 12,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 12,
|
||||
vertical: 6,
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: station.open == true
|
||||
? Colors.green[400]
|
||||
: Colors.red[400],
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(
|
||||
station.open == true
|
||||
? Icons.check_circle
|
||||
: Icons.cancel,
|
||||
color: Colors.white,
|
||||
size: 16,
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
station.open == true ? 'Geöffnet' : 'Geschlossen',
|
||||
style: const TextStyle(
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 12,
|
||||
vertical: 6,
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: station.open == true
|
||||
? Colors.green[400]
|
||||
: Colors.red[400],
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(
|
||||
station.open == true
|
||||
? Icons.check_circle
|
||||
: Icons.cancel,
|
||||
color: Colors.white,
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 12,
|
||||
size: 16,
|
||||
),
|
||||
),
|
||||
],
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
station.open == true ? 'Geöffnet' : 'Geschlossen',
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 12,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
|
||||
// Station Name
|
||||
Text(
|
||||
station.name ?? 'Unbekannte Tankstelle',
|
||||
style: const TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.black87,
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
const SizedBox(height: 12),
|
||||
|
||||
// Address
|
||||
Row(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.location_on,
|
||||
size: 16,
|
||||
color: Colors.blueGrey[600],
|
||||
// Station Name
|
||||
Text(
|
||||
station.name ?? 'Unbekannte Tankstelle',
|
||||
style: const TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.black87,
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
Expanded(
|
||||
child: Text(
|
||||
'${station.location?.address ?? ''}, '
|
||||
'${station.location?.postalCode ?? ''} '
|
||||
'${station.location?.city ?? ''}',
|
||||
style: TextStyle(fontSize: 13, color: Colors.grey[700]),
|
||||
maxLines: 2,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
|
||||
// Prices & Distance Row
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
// Diesel & Benzin Preise nebeneinander
|
||||
Expanded(
|
||||
child: Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 8,
|
||||
children: [
|
||||
if (dieselPrice?.amount != null)
|
||||
_buildPriceBadge(dieselPrice),
|
||||
if (supPrice?.amount != null)
|
||||
_buildPriceBadge(supPrice),
|
||||
],
|
||||
// Address
|
||||
Row(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.location_on,
|
||||
size: 16,
|
||||
color: Colors.blueGrey[600],
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
Expanded(
|
||||
child: Text(
|
||||
'${station.location?.address ?? ''}, '
|
||||
'${station.location?.postalCode ?? ''} '
|
||||
'${station.location?.city ?? ''}',
|
||||
style: TextStyle(fontSize: 13, color: Colors.grey[700]),
|
||||
maxLines: 2,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
|
||||
// Distance Badge
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 12,
|
||||
vertical: 8,
|
||||
// Prices & Distance Row
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
// Diesel & Benzin Preise nebeneinander
|
||||
Expanded(
|
||||
child: Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 8,
|
||||
children: [
|
||||
if (dieselPrice?.amount != null)
|
||||
_buildPriceBadge(dieselPrice),
|
||||
if (supPrice?.amount != null)
|
||||
_buildPriceBadge(supPrice),
|
||||
],
|
||||
),
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.blue[50],
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.navigation,
|
||||
color: Colors.blue[700],
|
||||
size: 18,
|
||||
),
|
||||
const SizedBox(width: 6),
|
||||
Text(
|
||||
_formatDistance(station.distance),
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.bold,
|
||||
|
||||
// Distance Badge
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 12,
|
||||
vertical: 8,
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.blue[50],
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.navigation,
|
||||
color: Colors.blue[700],
|
||||
size: 18,
|
||||
),
|
||||
),
|
||||
],
|
||||
const SizedBox(width: 6),
|
||||
Text(
|
||||
_formatDistance(station.distance),
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.blue[700],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user