second commit
This commit is contained in:
85
lib/Helper/location_service.dart
Normal file
85
lib/Helper/location_service.dart
Normal file
@@ -0,0 +1,85 @@
|
||||
import 'dart:convert';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'Logger.dart';
|
||||
|
||||
class GooglePlacesService {
|
||||
|
||||
String? latitude;
|
||||
String? longitude;
|
||||
String? city;
|
||||
int? radius;
|
||||
|
||||
GooglePlacesService();
|
||||
Future<List<Map<String, dynamic>>> getPlacesPredictions(String input) async {
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
final url = Uri.parse(
|
||||
'https://maps.googleapis.com/maps/api/place/autocomplete/json?input=$input&components=country:IN&key=AIzaSyCF4KatYCI3vqz1_H3kiHeyS3yCMfYToh8');
|
||||
// 'https://maps.googleapis.com/maps/api/place/autocomplete/json?input=$input$city&location=$latitude,$longitude&radius=15000&bounds=$latitude,$longitude&components=country:IN&types=establishment&key=AIzaSyCF4KatYCI3vqz1_H3kiHeyS3yCMfYToh8');
|
||||
logger.i(url);
|
||||
final response = await http.get(url);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final Map<String, dynamic> data = json.decode(response.body);
|
||||
final predictions = data['predictions'] as List<dynamic>;
|
||||
return predictions.map((prediction) => prediction as Map<String, dynamic>).toList();
|
||||
} else {
|
||||
throw Exception('Failed to load autocomplete suggestions');
|
||||
}
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> getPlaceDetails(String placeId) async {
|
||||
final url = Uri.parse(
|
||||
'https://maps.googleapis.com/maps/api/place/details/json?place_id=$placeId&key=AIzaSyCF4KatYCI3vqz1_H3kiHeyS3yCMfYToh8');
|
||||
|
||||
final response = await http.get(url);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final details = json.decode(response.body)['result'];
|
||||
return Map<String, dynamic>.from(details);
|
||||
} else {
|
||||
throw Exception('Failed to load place details');
|
||||
}
|
||||
}
|
||||
|
||||
Future<String?> getPlaceIdFromAddress(String address) async {
|
||||
const String apiKey = 'AIzaSyDQ2c_pOSOFYSjxGMwkFvCVWKjYOM9siow'; // Replace with your Google API key
|
||||
final String url = 'https://maps.googleapis.com/maps/api/geocode/json?address=${Uri.encodeComponent(address)}&key=$apiKey';
|
||||
|
||||
final response = await http.get(Uri.parse(url));
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final data = json.decode(response.body);
|
||||
|
||||
if (data['results'].isNotEmpty) {
|
||||
String placeId = data['results'][0]['place_id'];
|
||||
logger.i('The placeId from GooglePlace APi : ${placeId}');
|
||||
return placeId;
|
||||
} else {
|
||||
print('No results found for the provided address.');
|
||||
}
|
||||
} else {
|
||||
print('Failed to fetch Place ID. Status code: ${response.statusCode}');
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<double> getDistance(double pickupLat, double pickupLong, double dropLat, double dropLong) async {
|
||||
const 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');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user