محاسبه خلاصه مسیریابی

برای استفاده از جستجوی متنی (جدید) یا جستجوی نزدیک (جدید) برای محاسبه مدت زمان سفر و فاصله تا هر مکان در پاسخ:

  1. Pass the routingParameters.origin parameter in the request to specify the latitude and longitude coordinates of the routing origin. This parameter is required to calculate the duration and distance to each place in the response.

  2. Include routingSummaries in the field mask so that the response includes the routingSummaries array. This array contains the duration and distance from the routing origin to each place in the response.

مرورگر APIها به شما امکان می‌دهد درخواست‌های زنده ارسال کنید تا بتوانید با API و گزینه‌های API آشنا شوید:

In the following request, you calculate the travel duration and distance to each place in the Text Search (New) response:

curl -X POST -d '{
  "textQuery" : "Spicy Vegetarian Food in Sydney, Australia",
  "routingParameters": {
    "origin": {
      "latitude": -33.8688,
      "longitude": 151.1957362
    }
  }
}' \
-H 'Content-Type: application/json' -H 'X-Goog-Api-Key: API_KEY' \
-H 'X-Goog-FieldMask: places.displayName,places.formattedAddress,places.priceLevel,routingSummaries' \
'https://places.googleapis.com/v1/places:searchText'

The response contains two JSON arrays: the places array contains the matching places, and the routingSummaries array containing the duration and distance to travel to each place:

{
  "places": [
    {
      object (Place)
    }
  ]
  "routingSummaries": [
    {
      object (RoutingSummary)
    }
}

هر عنصر در آرایه routingSummaries در مکان آرایه مربوطه به عنوان مکان در آرایه places قرار دارد. یعنی عنصر موجود در routingSummaries[0] معادل مکان موجود در places[0] است.

The array length of routingSummaries is the same as the array length of places . In the case where the routingSummary for a place is not available, the array entry is empty.

Because this example calculates the duration and distance from the routing origin to each place, the routingSummaries.legs field in the response contains a single Leg object that contains the duration and distanceMeters from the routing origin to the place.

{
  "places": [
    {
      "formattedAddress": "1, Westfield Sydney Central Plaza, 450 George St, Sydney NSW 2000, Australia",
      "displayName": {
        "text": "Gözleme King Sydney",
        "languageCode": "en"
      }
    },
    {
      "formattedAddress": "367 Pitt St, Sydney NSW 2000, Australia",
      "priceLevel": "PRICE_LEVEL_MODERATE",
      "displayName": {
        "text": "Mother Chu's Vegetarian Kitchen",
        "languageCode": "en"
      }
    },
    
  ]
  "routingSummaries": [
    {
      "legs": [
        {
          "duration": "597s",
          "distanceMeters": 2607
        }
      ],
      "directionsUri": "https://www.google.com/maps/dir/-33.8688,151.1957362/''/data=!4m6!4m5!1m0!1m2!1m1!1s0x6b12ae3fa97cd745:0x6aecf365bf497c08!3e0"
    },
    {
      "legs": [
        {
          "duration": "562s",
          "distanceMeters": 2345
        }
      ],
      "directionsUri": "https://www.google.com/maps/dir/-33.8688,151.1957362/''/data=!4m6!4m5!1m0!1m2!1m1!1s0x6b12ae3da97f60c1:0x845f3273bd764f6c!3e0"
    },
   
  ]
}

از این مثال، می‌توانید ببینید که مدت زمان و مسافت از مبدا مسیریابی تا اولین مکان در نتایج، ۵۹۷ ثانیه و ۲۶۰۷ متر است.

در این مثال، شما مدت زمان سفر و فاصله تا هر مکان را در پاسخ جستجوی نزدیک محاسبه می‌کنید. این مثال رستوران‌ها را در سیدنی، استرالیا جستجو می‌کند و محدودیت مکان و مبدا مسیریابی را روی مختصات طول و عرض جغرافیایی یکسان تنظیم می‌کند:

  curl -X POST -d '{
    "includedTypes": ["restaurant"],
    "maxResultCount": 10,
    "locationRestriction": {
      "circle": {
        "center": {
          "latitude": -33.8688,
          "longitude": 151.1957362},
        "radius": 500.0
      }
    },
    "routingParameters": {
      "origin": {
        "latitude": -33.8688,
        "longitude": 151.1957362
      }
    }
  }' \
  -H 'Content-Type: application/json' -H "X-Goog-Api-Key:API_KEY" \
  -H "X-Goog-FieldMask: places.displayName,routingSummaries" \
  https://places.googleapis.com/v1/places:searchNearby

You don't have to use the same coordinates for the locationRestriction and the for routing origin. For example, you set the locationRestriction to the center point of Sydney to bias the search results to that circle. But you then set the routing origin to the coordinates of your house, meaning to a different location within the search circle. The request then biases the search results to the circle, and calculates the routing summaries based on the location of your house.

Specify travel options

به طور پیش‌فرض، محاسبات مدت زمان و مسافت برای یک خودرو انجام می‌شود. با این حال، می‌توانید نوع خودرو و همچنین سایر گزینه‌ها را در جستجو کنترل کنید.

  • از پارامتر routingParameters.travelMode برای تنظیم حالت حمل و نقل روی DRIVE ، BICYCLE ، WALK یا TWO_WHEELER استفاده کنید. برای اطلاعات بیشتر در مورد این گزینه‌ها، به بخش انواع وسایل نقلیه موجود برای مسیرها مراجعه کنید.

  • Use the routingParameters.routingPreference property to set the routing preference option to TRAFFIC_UNAWARE (default), TRAFFIC_AWARE , or TRAFFIC_AWARE_OPTIMAL . Each option has varying levels of data quality and latency. For more information, see Specify how and if to include traffic data .

    ویژگی routingParameters.routingPreference بر مسیرهای موجود در فیلد Preview (Pre-GA) directionsUri تأثیر می‌گذارد، زیرا Google Maps هنگام باز کردن لینک، گزینه‌های ترافیک را نمایش می‌دهد.

  • Use the routingParameters.routeModifiers property to specify to avoidTolls , avoidHighways , avoidFerries , and avoidIndoor . For more information on these options, see Specify route features to avoid .

In the next example, you specify the travel mode as DRIVE and to avoid highways:

curl -X POST -d '{
  "textQuery" : "Spicy Vegetarian Food in Sydney, Australia",
  "routingParameters": {
    "origin": {
      "latitude": -33.8688,
      "longitude": 151.1957362
    },
    "travelMode":"DRIVE",
    "routeModifiers": {
      "avoidHighways": true
    }
  }
}' \
-H 'Content-Type: application/json' -H 'X-Goog-Api-Key: API_KEY' \
-H 'X-Goog-FieldMask: places.displayName,places.formattedAddress,places.priceLevel,routingSummaries' \
'https://places.googleapis.com/v1/places:searchText'

امتحانش کن!

مرورگر APIها به شما امکان می‌دهد درخواست‌های نمونه ایجاد کنید تا با API و گزینه‌های API آشنا شوید.

  1. آیکون API یعنی api را در سمت راست صفحه انتخاب کنید.

  2. در صورت تمایل، پارامترهای درخواست را ویرایش کنید.

  3. دکمه اجرا را انتخاب کنید. در کادر محاوره‌ای، حسابی را که می‌خواهید برای ارسال درخواست استفاده کنید، انتخاب کنید.

  4. در پنل APIs Explorer، آیکون تمام صفحه را در حالت تمام صفحه انتخاب کنید تا پنجره APIs Explorer باز شود.