0% found this document useful (0 votes)
40 views2 pages

Assistant Methods

This document contains code for an AssistantMethods class that provides methods for geocoding addresses from coordinates, reading user info from a database, and obtaining direction details between two locations using the Google Maps API. Specifically, it contains: 1. A searchAddressForGeographicCoOrdinates method that geocodes coordinates to an address and saves it to the app info provider. 2. A readCurrentOnlineUserInfo method that reads the current user info from the Firebase database. 3. An obtainOriginToDestinationDirectionDetails method that calls the Google Maps Directions API to get route details like distance and duration between two locations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views2 pages

Assistant Methods

This document contains code for an AssistantMethods class that provides methods for geocoding addresses from coordinates, reading user info from a database, and obtaining direction details between two locations using the Google Maps API. Specifically, it contains: 1. A searchAddressForGeographicCoOrdinates method that geocodes coordinates to an address and saves it to the app info provider. 2. A readCurrentOnlineUserInfo method that reads the current user info from the Firebase database. 3. An obtainOriginToDestinationDirectionDetails method that calls the Google Maps Directions API to get route details like distance and duration between two locations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import 'package:bikers_go_couriers/assistants/request_assistant.

dart';
import 'package:bikers_go_couriers/global/global.dart';
import 'package:bikers_go_couriers/global/map_key.dart';
import 'package:bikers_go_couriers/infoHandler/app_info.dart';
import 'package:bikers_go_couriers/models/direction_details_info.dart';
import 'package:bikers_go_couriers/models/directions.dart';
import 'package:bikers_go_couriers/models/user_model.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:geolocator/geolocator.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:provider/provider.dart';

class AssistantMethods
{
static Future<String> searchAddressForGeographicCoOrdinates(Position position,
context) async
{
String apiUrl = "https://maps.googleapis.com/maps/api/geocode/json?latlng=$
{position.latitude},${position.longitude}&key=$mapKey";
String humanReadableAddress="";

var requestResponse = await RequestAssistant.receiveRequest(apiUrl);

if(requestResponse != "Error Occurred, Failed. No Response.")


{
humanReadableAddress = requestResponse["results"][0]["formatted_address"];

Directions userPickUpAddress = Directions();


userPickUpAddress.locationLatitude = position.latitude;
userPickUpAddress.locationLongitude = position.longitude;
userPickUpAddress.locationName = humanReadableAddress;

Provider.of<AppInfo>(context, listen:
false).updatePickUpLocationAddress(userPickUpAddress);
}

return humanReadableAddress;
}

static void readCurrentOnlineUserInfo() async


{
currentFirebaseUser = fAuth.currentUser;

DatabaseReference userRef = FirebaseDatabase.instance


.ref()
.child("users")
.child(currentFirebaseUser!.uid);

userRef.once().then((snap)
{
if(snap.snapshot.value != null)
{
userModelCurrentInfo = UserModel.fromSnapshot(snap.snapshot);
}
});
}
static Future<DirectionDetailsInfo?>
obtainOriginToDestinationDirectionDetails(LatLng origionPosition, LatLng
destinationPosition) async
{
String urlOriginToDestinationDirectionDetails =
"https://maps.googleapis.com/maps/api/directions/json?origin=$
{origionPosition.latitude},${origionPosition.longitude}&destination=$
{destinationPosition.latitude},${destinationPosition.longitude}&key=$mapKey";

var responseDirectionApi = await


RequestAssistant.receiveRequest(urlOriginToDestinationDirectionDetails);

if(responseDirectionApi == "Error Occurred, Failed. No Response.")


{
return null;
}

DirectionDetailsInfo directionDetailsInfo = DirectionDetailsInfo();


directionDetailsInfo.e_points = responseDirectionApi["routes"][0]
["overview_polyline"]["points"];

directionDetailsInfo.distance_text = responseDirectionApi["routes"][0]["legs"]
[0]["distance"]["text"];
directionDetailsInfo.distance_value = responseDirectionApi["routes"][0]["legs"]
[0]["distance"]["value"];

directionDetailsInfo.duration_text = responseDirectionApi["routes"][0]["legs"]
[0]["duration"]["text"];
directionDetailsInfo.duration_value = responseDirectionApi["routes"][0]["legs"]
[0]["duration"]["value"];

return directionDetailsInfo;
}
}

You might also like