how to add delay for distance request in google map api

时间:2015-07-28 16:13:19

标签: php google-maps google-maps-api-3

I am using google map api to get distance between cities after getting these distance, I want to store them in array. But google api doesnt allow request without delay. How I can add delay. or is there any other way to get driving distance. Here is code

<?php 
$origin="London";
$destination= array("Manchester","Bristol","glasgow","liverpool");
/* print_r($destination);
$size=sizeof($destination);
echo $size; */
$distance = array("abc");
for($i=0; $i<4;$i++){   
    $url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=$origin&destinations=$destination[i]&mode=driving&language=en&sensor=false";
    $data = file_get_contents($url);
    $data = utf8_decode($data);
    $obj = json_decode($data);

    //echo($obj->rows[0]->elements[0]->distance->text); //km
    //echo($obj->rows[0]->elements[0]->distance->value); // meters  
    array_push($distance,"$obj->rows[0]->elements[0]->distance->text");
}
print_r($distance);
?>

above code works fine without loop.

2 个答案:

答案 0 :(得分:0)

The DirectionsMatrix computes and returns multiple results in a single request. You can place a single request to return all four of those results:

http://maps.googleapis.com/maps/api/distancematrix/json?origins=London&destinations=Manchester|Bristol|glasgow|liverpool&mode=driving&language=en&sensor=false

Result:

{
   "destination_addresses" : [
      "Manchester, UK",
      "Bristol, City of Bristol, UK",
      "Glasgow, Glasgow City, UK",
      "Liverpool, Merseyside, UK"
   ],
   "origin_addresses" : [ "London, UK" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "336 km",
                  "value" : 335756
               },
               "duration" : {
                  "text" : "3 hours 48 mins",
                  "value" : 13655
               },
               "status" : "OK"
            },
            {
               "distance" : {
                  "text" : "190 km",
                  "value" : 190196
               },
               "duration" : {
                  "text" : "2 hours 15 mins",
                  "value" : 8094
               },
               "status" : "OK"
            },
            {
               "distance" : {
                  "text" : "663 km",
                  "value" : 662591
               },
               "duration" : {
                  "text" : "6 hours 34 mins",
                  "value" : 23621
               },
               "status" : "OK"
            },
            {
               "distance" : {
                  "text" : "354 km",
                  "value" : 354422
               },
               "duration" : {
                  "text" : "3 hours 54 mins",
                  "value" : 14027
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}

答案 1 :(得分:0)

You need to use the code from an algorithm that finds or selects the shortest path in between a point and a set of points (multiple nodes), like in this case. You may follow the strategies describes in the Dijkstra's algorithm. You can do so by using the comparable interface and comparing the probable set of destinations finding out the shortest one. Doing the same when you choose the shortest one using a look.

This SO post may help you in understanding what I am trying to say.