একটি রুট উদাহরণ গণনা

আপনি নিম্নলিখিত সংস্থানে একটি HTTP POST অনুরোধ পাঠিয়ে রুট পছন্দের API থেকে রুটের একটি সেট পুনরুদ্ধার করতে পারেন:

https://routespreferred.googleapis.com/v1:computeRoutes

আপনার অনুরোধের বিকল্পগুলি অন্তর্ভুক্ত করুন, JSON-এ ফর্ম্যাট করা, মেসেজের বডিতে। বিকল্পগুলির সম্পূর্ণ সেট সম্পর্কে তথ্যের জন্য, computeRoutes Request Body দেখুন।

উদাহরণ অনুরোধ বডি

নিচের JSON কোডটি দেখায় যে কিভাবে একটি computeRoutes অনুরোধের জন্য একটি সাধারণ অনুরোধের বডি তৈরি করা যায়।

POST /v1:computeRoutes
Host: routespreferred.googleapis.com
Content-Type: application/json
X-Goog-Api-Key: YOUR_API_KEY
X-Goog-FieldMask: routes.duration,routes.distanceMeters,routes.polyline.encodedPolyline

{
  "origin":{
    "location":{
      "latLng":{
        "latitude": 37.419734,
        "longitude": -122.0827784
      }
    }
  },
  "destination":{
    "location":{
      "latLng":{
        "latitude": 37.417670,
        "longitude": -122.079595
      }
    }
  },
  "travelMode": "DRIVE",
  "routingPreference": "TRAFFIC_AWARE",
  "polylineQuality": "OVERVIEW",
  "departureTime": "2019-10-15T15:01:23.045123456Z",
  "computeAlternativeRoutes": false,
  "routeModifiers": {
    "avoidTolls": false,
    "avoidHighways": false,
    "avoidFerries": false
  },
  "languageCode": "en-US",
  "units": "IMPERIAL"
}

উদাহরণ প্রতিক্রিয়া শরীর

নিম্নোক্ত JSON কোডটি উপরের কল থেকে computeRoutes এ ফিরে আসা রেসপন্স বডির একটি উদাহরণ।

{
  "routes": [
    {
      "distanceMeters": 772,
      "duration": "165s",
      "polyline": {
        "encodedPolyline": "ipkcFfichVnP@j@BLoFVwM{E?"
      }
    }
  ]
}

উদাহরণ gRPC অনুরোধ

নিম্নলিখিত একটি উদাহরণ gRPC অনুরোধ দেখায়.

যাওয়া

package main

import (
	"context"
	"fmt"
	"log"
	"time"

	routespreferred "developers.google.com/maps/go/routespreferred/v1"
	"google.golang.org/api/option"
	routespb "google.golang.org/genproto/googleapis/maps/routes/v1"
	"google.golang.org/genproto/googleapis/type/latlng"
	"google.golang.org/grpc/metadata"
)

const (
	// https://cloud.google.com/iam/docs/creating-managing-service-account-keys#creating_service_account_keys
	credentialsFile = "service-account.json"
	// Note that setting the field mask to * is OK for testing, but discouraged in
	// production.
	// For example, for ComputeRoutes, set the field mask to
	// "routes.distanceMeters,routes.duration,routes.polyline.encodedPolyline"
	// in order to get the route distances, durations, and encoded polylines.
	fieldMask = "*"
)

func main() {
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()

	// instantiate a client
	c, err := routespreferred.NewRoutesPreferredClient(ctx,
		option.WithCredentialsFile(credentialsFile))
	defer c.Close()

	if err != nil {
		log.Fatal(err)
	}

	// create the origin using a latitude and longitude
	origin := &routespb.Waypoint{
		LocationType: &routespb.Waypoint_Location{
			Location: &routespb.Location{
				LatLng: &latlng.LatLng{
					Latitude:  37.417670,
					Longitude: -122.0827784,
				},
			},
		},
	}

	// create the destination using a latitude and longitude
	destination := &routespb.Waypoint{
		LocationType: &routespb.Waypoint_Location{
			Location: &routespb.Location{
				LatLng: &latlng.LatLng{
					Latitude:  37.417670,
					Longitude: -122.079595,
				},
			},
		},
	}

	// create the request with additional options
	req := &routespb.ComputeRoutesRequest{
		Origin:                   origin,
		Destination:              destination,
		TravelMode:               routespb.RouteTravelMode_DRIVE,
		RoutingPreference:        routespb.RoutingPreference_TRAFFIC_AWARE,
		ComputeAlternativeRoutes: true,
		Units:                    routespb.Units_METRIC,
		LanguageCode:             "en-us",
		RouteModifiers: &routespb.RouteModifiers{
			AvoidTolls:    false,
			AvoidHighways: true,
			AvoidFerries:  true,
		},
		PolylineQuality: routespb.PolylineQuality_OVERVIEW,
	}

	// set the field mask
	ctx = metadata.AppendToOutgoingContext(ctx, "X-Goog-Fieldmask", fieldMask)

	// execute rpc
	resp, err := c.ComputeRoutes(ctx, req)

	if err != nil {
		// "rpc error: code = InvalidArgument desc = Request contains an invalid
		// argument" may indicate that your project lacks access to Routes Preferred
		log.Fatal(err)
	}

	fmt.Printf("Duration of route %d", resp.Routes[0].Duration.Seconds)
}

টোল ফি উদাহরণ গণনা

নিম্নোক্ত উদাহরণটি টোল পাস ব্যবহার করার সময় আনুমানিক মূল্য সহ টোল তথ্য ফেরত দিতে computeRoutes পদ্ধতি ব্যবহার করে।

এই বৈশিষ্ট্যটি অনুরোধে উল্লেখিত routes.travelAdvisory.tollInfo ফিল্ড মাস্ক দিয়ে সক্ষম করা হয়েছে। টোল পাস route_modifiers ফিল্ডে নির্দিষ্ট করা আছে। ফেরত দেওয়া টোল মূল্য নির্দিষ্ট পাস দ্বারা ব্যবহৃত মূল্যের উপর ভিত্তি করে। একাধিক পাস নির্দিষ্ট করা হলে, সর্বনিম্ন ব্যয়বহুল মূল্য ফেরত দেওয়া হয়।

অনুরোধ:

curl -X POST -d '{
  "origin":{
    "location":{
      "lat_lng":{
        "latitude":47.7020056,
        "longitude":-122.3479236
      }
    }
  },
  "destination":{
    "location":{
      "lat_lng":{
        "latitude":47.6192234,
        "longitude": -122.1676792
      }
    }
  },
  "travel_mode":"DRIVE",
  "route_modifiers":{
    "vehicle_info":{
      "emission_type": "GASOLINE"
    },
    "toll_passes": [
      "US_MA_EZPASSMA",
      "US_WA_GOOD_TO_GO"
    ]
  }
}' \
-H 'Content-Type: application/json' \
-H 'X-Goog-Api-Key: <YOUR_API_KEY>' \
-H 'X-Goog-FieldMask: routes.duration,routes.distanceMeters,routes.travelAdvisory.tollInfo,routes.legs.travelAdvisory.tollInfo' \
'https://routespreferred.googleapis.com/v1alpha:computeRoutes'

প্রতিক্রিয়া:

{
  "routes": [
    {
      "legs": [
        {
          "travelAdvisory": {
            "tollInfo": {
              "estimatedPrice": [
                {
                  "currencyCode": "USD",
                  "units": "3",
                  "nanos": 400000000
                }
              ]
            }
          }
        }
      ],
      "distanceMeters": 22496,
      "duration": "1400s",
      "travelAdvisory": {
        "tollInfo": {
          "estimatedPrice": [
            {
              "currencyCode": "USD",
              "units": "3",
              "nanos": 400000000
            }
          ]
        }
      }
    }
  ]
}