4

I know that "MAX_DIMENSIONS_EXCEEDED" error showing when Your request contained more than 25 origins, or more than 25 destinations. But Just I want to know this limit is only for free api customer or it's for all?

I am using below google service

var service = new google.maps.DistanceMatrixService();

Asha Gajjar
  • 43
  • 1
  • 3

2 Answers2

5

If you currently have the free plan you get a maximum of 25 origins OR 25 destinations per request. If you go above the free plan allowance you have to pay extra. So that limit is whether you pay or not.

The premium plan where you get a maximum of 25 origins AND 25 destinations per request is only available to users with premium plans prior to the latest price change. From what you have said, you can't have the premium.

See more here: https://developers.google.com/maps/premium/usage-limits

Do2
  • 1,751
  • 1
  • 16
  • 28
  • The link in this post says only applies to old customers (new ones can't sign up). It appears to be a general rule now: https://developers.google.com/maps/documentation/javascript/reference/distance-matrix#DistanceMatrixStatus.MAX_DIMENSIONS_EXCEEDED – Mike Cheel Jan 10 '23 at 15:51
0

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 :)

Taddl
  • 11
  • 5