使用谷歌API获取基于谷歌URL的路线距离

问题描述

我想获得#include <iostream> void MyFunc(int a,int b,int c) { std::cout << "3-argument version called." << std::endl; } void MyFunc(int a,int c,int d) { std::cout << "4-argument version called." << std::endl; } int main() { MyFunc(1,2,3); MyFunc(1,3,4); } ,并最终获得其他通过distance请求的信息。我收到了Google Google Api。正如您在下面看到的,我有一个通用模板,我在程序运行时填写该模板,其中可能包含航路点和旅行模式。通过API key获取距离和其他信息的Google Api请求是什么?

JSON

解决方法

摘自here的Google文档-您将需要使用Distance Matrix API。

请求将类似于:

https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=Washington,DC&destinations=New+York+City,NY&key=YOUR_API_KEY

用您的密钥替换YOUR_API_KEY占位符。

响应将是以下格式的Json:

{
   "destination_addresses" : [ "New York,NY,USA" ],"origin_addresses" : [ "Washington,DC,"rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "225 mi","value" : 361715
               },"duration" : {
                  "text" : "3 hours 49 mins","value" : 13725
               },"status" : "OK"
            }
         ]
      }
   ],"status" : "OK"
}

如果您没有正确的API密钥,请执行以下操作:

  1. 登录您的Google Cloud Platform Console
  2. 添加您的项目并添加API。请注意,距离 Matrix API不是免费的。
  3. 执行此操作后,您将获取API密钥。

编辑

如果您需要添加多个航路点,则需要使用其他API,请参见documentation Maps JavaScript API Directions Service。简而言之,它与Google Map相同。

它允许您添加航路点数组。再次,它不是免费的-它是一种现收现付服务。

DirectionsResult对象将包含Directions Legs(以米为单位的距离)