Google teams reply:
I have checked our logs and saw spike of usage has a high duplication rate as high as 99.33% (e.g. total request: 16657,Unique request: 112). This indicates that certain application are generating an excessive number of duplicate request, leading to redundant usage of the Directions Api web service/server-side request.
These are data for 4 days of a month where we have tried live tracking.
Now I am sharing my code implementation so that you can get the whole idea.
package use: Location package.
for live tracking I am using firebase to store the data and retrive as Stream.
For Displaying to the user the live tracking am using this code:
StreamBuilder(
stream: firestoreInstance
.collection('location')
.doc('${widget.dishOrderId}' /*'609'*/)
.snapshots(),
builder: (context,
AsyncSnapshot<DocumentSnapshot<Map<String, dynamic>>> snapshot) {
if (model.added) {
updateCamera(snapshot);
}
if (!snapshot.hasData) {
return const Center(child: CircularProgressIndicator());
}
return Stack(
alignment: Alignment.topCenter,
children: [
GoogleMap(
myLocationButtonEnabled: false,
zoomControlsEnabled: false,
mapToolbarEnabled: false,
initialCameraPosition: model.initialCameraPosition!,
onMapCreated: model.onMapCreated,
markers: {
if (model.origin != null) model.origin!,
if (model.destination != null) model.destination!
},
polylines: {
if (model.info != null)
Polyline(
polylineId: const PolylineId('line'),
color: redColor,
width: 2,
points: model.info!.polylinePoints!
.map((e) => LatLng(e.latitude, e.longitude))
.toList(),
),
},
),
if (model.info != null)
Padding(
padding: const EdgeInsets.symmetric(
vertical: 10, horizontal: 15),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Container(
padding: const EdgeInsets.symmetric(
vertical: 4.0, horizontal: 12.0),
decoration:
bottomConDecoration.copyWith(color: Colors.black),
child: Text(
'${model.info!.totalDistance}, ${model.info!.totalDuration}',
style: const TextStyle(
fontSize: 14.0, color: Colors.white),
),
),
],
),
),
],
);
}),
Update Camera Method
Future<void> updateCamera(
AsyncSnapshot<DocumentSnapshot<Map<String, dynamic>>> snapshot) async {
model.startMapPosition =
LatLng(snapshot.data!['latitude'], snapshot.data!['longitude']);
model.setNewPositions();
}
Set New Position Method
void setNewPositions() async {
final Uint8List markerIcon =
await getBytesFromAsset('assets/images/delivery_bike.png', 70);
origin = Marker(
markerId: const MarkerId('origin'),
infoWindow: const InfoWindow(title: 'Origin'),
icon: BitmapDescriptor.fromBytes(markerIcon),
position: lastMapPosition!,
);
destination = Marker(
markerId: const MarkerId('destination'),
infoWindow: const InfoWindow(title: 'Destination'),
icon: BitmapDescriptor.defaultMarker,
position: startMapPosition!,
);
final directions = await DirectionsRepository()
.getDirections(origin: lastMapPosition, destination: startMapPosition);
directions.polylinePoints?.insert(
0, PointLatLng(lastMapPosition!.latitude, lastMapPosition!.longitude));
info = directions;
notifyListeners();
}
Get Directions Method:
static const String _baseUrl =
'https://maps.googleapis.com/maps/api/directions/json?';
Future<Directions> getDirections(
{LatLng? origin, LatLng? destination}) async {
final response = await http.get(
Uri.parse(
"${_baseUrl}origin=${origin?.latitude},${origin?.longitude}&destination=${destination?.latitude},${destination?.longitude}&key=${URLConstants.googleMapApiKey}"),
);
// Check if response is successful
if (response.statusCode == 200) {
return Directions.fromMap(jsonDecode(response.body));
}
return const Directions();
}
This is how the directions Api is getting called multiple times in my Project Because of Streams . Now thats why may be I am getting duplicate requests.
If you can tell me what I am doing wrong to minimise the call for directionApi to minimise the bill. I’ll just be bankrupt if this goes on. Your help is much need and urgent .
Please HELP!!! Thanks in advance.