// distance_service.dart
import 'dart:convert';
import 'package:http/http.dart' as http;




class DistanceService {
  Future<double> getDistance(double pickupLat, double pickupLong, double dropLat, double dropLong) async {
    final apiKey = 'AIzaSyDQ2c_pOSOFYSjxGMwkFvCVWKjYOM9siow';
    final url =
        'https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=$pickupLat,$pickupLong&destinations=$dropLat,$dropLong&key=$apiKey';

    final response = await http.get(Uri.parse(url));

    if (response.statusCode == 200) {
      final data = jsonDecode(response.body);
      final distanceText = data['rows'][0]['elements'][0]['distance']['text'];
      final distanceValue = data['rows'][0]['elements'][0]['distance']['value'] / 1000; // Convert meters to kilometers
      print('Distance: $distanceText');
      return distanceValue;
    } else {
      throw Exception('Failed to load distance');
    }
  }
}
