Heyo :)
I stumbled across this exact issue while creating a "medium-sized" MapsAPI Project.
Even though we had billing enabled and configured on Google, I had this status code show up.
You are allowed to pass max. 25 destinations and origins per request (like Do2 correctly said).
But what if you have 150 Stores and more??
My idea was to split the requests into chunks and call the API server-side.
So i excluded the API Call in a PHP file and passed my data with AJAX (app.js <-> magic.php)
See my Workaround:
/* CLIENTSIDE app.js */
const getDistanceMatrix = (service, parameters) =>
new Promise((resolve, reject) => {
let paramString = JSON.stringify(parameters);
//console.log(paramString);
$.ajax({
type: "Post",
url: "/develope/kk-store-locator/magic.php",
data: {
daten: paramString,
},
success: function (response) {
//Wenn Query success
response = JSON.parse(response);
console.log(response);
const distances = [];
const results = response.rows[0].elements;
for (let j = 0; j < results.length; j++) {
const element = results[j];
var addressText = response.destination_addresses[j];
//const distanceText = element.distance.text;
//const distanceVal = element.distance.value;
const distanceObject = {
storeid: stores[j],
distanceText: addressText,
};
distances.push(distanceObject);
}
resolve(distances);
},
error: function (response) {
// Wenn Query failed = Promise revoked
responserr = JSON.parse(response);
console.log(responserr);
reject(response);
},
});
});
const distancesList = await getDistanceMatrix(service, {
origins: [origin],
destinations: destinations,
travelMode: "DRIVING",
unitSystem: google.maps.UnitSystem.METRIC,
});
// HIER Zurückgeben
distancesList.sort((first, second) => {
return first.distanceVal - second.distanceVal;
});
return distancesList;
}
SERVERSIDE (magic.php):
<?php
data = json_decode($_POST["daten"]);
$origin = $data->origins[0];
$destinations = $data->destinations;
//Variablendeklaration Request-String
$chunk_size = 24; //25 Stores
$dest_counter = sizeof($destinations); //StoreLength gesamt
$dest_chunkz = ceil($dest_counter / $chunk_size); //Aufrunden für Requestanzahl
$strings = [];
//Build String max.25 Destinations
$iter = 0;
for ($i = 0; $i < $dest_chunkz; $i++) {
for ($iter; $iter < $dest_counter; $iter++) {
$strings[$i] .= $destinations[$iter]->lat . "," . $destinations[$iter]->lng . "|";
$items = substr_count($strings[$i], '|');
if ($items == 25) { //Runtime-Limit (max. 25)
break;
}
}
}
//Call to Google Distanz-API
foreach ($strings as $string) {
$string = substr($string, 0, -1);
$latNlong_origin = $origin->lat . "," . $origin->lng;
$distance_data = file_get_contents('https://maps.googleapis.com/maps/api/distancematrix/json?&origins=' . urlencode($latNlong_origin) . '&destinations=' . urlencode($string) . '&key=' . $apiKey . '');
$distance_arr = json_decode($distance_data);
array_push($distance_copy, $distance_arr);
if ($distance_arr->status == 'OK') {
$destination_addresses = $distance_arr->destination_addresses;
$origin_addresses = $distance_arr->origin_addresses;
} else {
echo "<p>The request was Invalid</p>";
echo $distance_arr->status;
exit();
}
if ($origin_addresses == "" or $destination_addresses == "") {
echo "<p>Destination or origin address not found</p>";
exit();
}
}
=> remove duplicates, and return <=
I hope this brings the idea of the "workaround" a little closer - note that you always need billing enabled to work with all of the Google APIs
Have a nice Day :)