duanlijia5864
2016-08-12 09:32 浏览 86
已采纳

Google Maps API距离矩阵请求

I've been using the Google Distance Matrix API for a while now to calculate distances between the consumer and businesses on my website; I have currently hit a wall and seen that my quote hits 2,500 and doesn't process anymore requests after that.

I then decided to give the documentation a quick read over to see if there's anything I could do to maybe kill two birds with one stone, so to say, and seen on the API Usage Limits Page that you have have 25 origins and 25 destinations per requests.

Does this means that within one request I can concatenate 25 origins and destinations and return their miles.

Here's the PHP code I use to get the milage between the consumer and customer:

final public function Distance($A, $B){

    $URL = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=".urlencode($A)."&destinations=".urlencode($B)."&key=API_KEY_HERE";
    $Data = json_decode(file_get_contents($URL));

    return str_replace(" mi", "", $Data->rows->elements->distance->text);        
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答 默认 最新

  • 已采纳
    duandong7980 duandong7980 2016-08-12 09:37

    The quota is in terms of elements:

    Users of the standard API:

    2,500 free elements per day, calculated as the sum of client-side and server-side queries.

    Also:

    Each query sent to the Google Maps Distance Matrix API is limited by the number of allowed elements, where the number of origins times the number of destinations defines the number of elements.

    Adding more origins/destinations to a request won't increase the amount of distances that can be retrieved before hitting the quota.

    点赞 评论 复制链接分享
  • dsdt66064367 dsdt66064367 2017-11-05 11:01

    I hope it will work.....

    final public function Distance($A, $B){
    
        $URL = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=".urlencode($A)."&destinations=".urlencode($B)."&key=API_KEY_HERE";
        $Data = json_decode(file_get_contents($URL));
    
        return str_replace(' mi', '',$Data->rows[0]->elements[0]->distance->text);        
    }
    
    点赞 评论 复制链接分享

相关推荐