मैसेज के मुख्य हिस्से में, अनुरोध के विकल्प शामिल करें. इन्हें JSON फ़ॉर्मैट में फ़ॉर्मैट करें. विकल्पों के पूरे सेट के बारे में जानकारी के लिए, computeRoutes अनुरोध का मुख्य हिस्सा देखें.
अनुरोध के मुख्य हिस्से का उदाहरण
यहां दिए गए JSON कोड में, computeRoutes अनुरोध के लिए, सामान्य अनुरोध बॉडी बनाने का तरीका बताया गया है.
packagemainimport("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_keyscredentialsFile="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="*")funcmain(){ctx,cancel:=context.WithTimeout(context.Background(),30*time.Second)defercancel()// instantiate a clientc,err:=routespreferred.NewRoutesPreferredClient(ctx,option.WithCredentialsFile(credentialsFile))deferc.Close()iferr!=nil{log.Fatal(err)}// create the origin using a latitude and longitudeorigin:=&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 longitudedestination:=&routespb.Waypoint{LocationType:&routespb.Waypoint_Location{Location:&routespb.Location{LatLng:&latlng.LatLng{Latitude:37.417670,Longitude:-122.079595,},},},}// create the request with additional optionsreq:=&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 maskctx=metadata.AppendToOutgoingContext(ctx,"X-Goog-Fieldmask",fieldMask)// execute rpcresp,err:=c.ComputeRoutes(ctx,req)iferr!=nil{// "rpc error: code = InvalidArgument desc = Request contains an invalid// argument" may indicate that your project lacks access to Routes Preferredlog.Fatal(err)}fmt.Printf("Duration of route %d",resp.Routes[0].Duration.Seconds)}
यह सुविधा, अनुरोध में बताए गए routes.travelAdvisory.tollInfo फ़ील्ड मास्क के साथ चालू होती है. टोल पास की जानकारी route_modifiers
फ़ील्ड में दी गई है. टोल की दिखाई गई कीमत, बताए गए पास के लिए तय की गई कीमत के आधार पर तय की जाती है. अगर एक से ज़्यादा पास तय किए गए हैं, तो सबसे कम कीमत वाली कीमत दिखाई जाती है.
[null,null,["आखिरी बार 2025-04-24 (UTC) को अपडेट किया गया."],[[["\u003cp\u003eRetrieve routes using the Routes Preferred API by sending an HTTP POST request to \u003ccode\u003ehttps://routespreferred.googleapis.com/v1:computeRoutes\u003c/code\u003e with request options in JSON format.\u003c/p\u003e\n"],["\u003cp\u003eCustomize route calculations by specifying parameters like origin, destination, travel mode, routing preferences, departure time, and route modifiers in the request body.\u003c/p\u003e\n"],["\u003cp\u003eResponses include route details such as distance, duration, and encoded polylines, with optional toll information and estimated prices when requested.\u003c/p\u003e\n"],["\u003cp\u003eThe API supports both REST and gRPC interfaces for requesting routes, providing flexibility in integration.\u003c/p\u003e\n"],["\u003cp\u003eToll price estimates are for non-commercial vehicles and may differ for commercial use.\u003c/p\u003e\n"]]],[],null,["# Computing a Route Examples\n\nYou can retrieve a set of routes from the Routes Preferred API\nby sending an HTTP POST request to the following resource: \n\n https://routespreferred.googleapis.com/v1:computeRoutes\n\nInclude your request options, formatted in JSON, in the message body. For\ninformation about the complete set of options, see the\n[`computeRoutes` Request Body](/maps/documentation/routes_preferred/reference/rest/v1/TopLevel/computeRoutes#request-body).\n\nExample request body\n--------------------\n\nThe following JSON code demonstrates how to construct a typical request body\nfor a\n[`computeRoutes`](/maps/documentation/routes_preferred/reference/rest/v1/TopLevel/computeRoutes)\nrequest. \n\n POST /v1:computeRoutes\n Host: routespreferred.googleapis.com\n Content-Type: application/json\n X-Goog-Api-Key: YOUR_API_KEY\n X-Goog-FieldMask: routes.duration,routes.distanceMeters,routes.polyline.encodedPolyline\n\n {\n \"origin\":{\n \"location\":{\n \"latLng\":{\n \"latitude\": 37.419734,\n \"longitude\": -122.0827784\n }\n }\n },\n \"destination\":{\n \"location\":{\n \"latLng\":{\n \"latitude\": 37.417670,\n \"longitude\": -122.079595\n }\n }\n },\n \"travelMode\": \"DRIVE\",\n \"routingPreference\": \"TRAFFIC_AWARE\",\n \"polylineQuality\": \"OVERVIEW\",\n \"departureTime\": \"2019-10-15T15:01:23.045123456Z\",\n \"computeAlternativeRoutes\": false,\n \"routeModifiers\": {\n \"avoidTolls\": false,\n \"avoidHighways\": false,\n \"avoidFerries\": false\n },\n \"languageCode\": \"en-US\",\n \"units\": \"IMPERIAL\"\n }\n\nExample response body\n---------------------\n\nThe following JSON code is an example of the response body returned from the\nabove call to `computeRoutes`. \n\n {\n \"routes\": [\n {\n \"distanceMeters\": 772,\n \"duration\": \"165s\",\n \"polyline\": {\n \"encodedPolyline\": \"ipkcFfichVnP@j@BLoFVwM{E?\"\n }\n }\n ]\n }\n\nExample gRPC Request\n--------------------\n\nThe following shows an example gRPC request. \n\n### Go\n\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\t\"log\"\n\t\"time\"\n\n\troutespreferred \"developers.google.com/maps/go/routespreferred/v1\"\n\t\"google.golang.org/api/option\"\n\troutespb \"google.golang.org/genproto/googleapis/maps/routes/v1\"\n\t\"google.golang.org/genproto/googleapis/type/latlng\"\n\t\"google.golang.org/grpc/metadata\"\n)\n\nconst (\n\t// https://cloud.google.com/iam/docs/creating-managing-service-account-keys#creating_service_account_keys\n\tcredentialsFile = \"service-account.json\"\n\t// Note that setting the field mask to * is OK for testing, but discouraged in\n\t// production.\n\t// For example, for ComputeRoutes, set the field mask to\n\t// \"routes.distanceMeters,routes.duration,routes.polyline.encodedPolyline\"\n\t// in order to get the route distances, durations, and encoded polylines.\n\tfieldMask = \"*\"\n)\n\nfunc main() {\n\tctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)\n\tdefer cancel()\n\n\t// instantiate a client\n\tc, err := routespreferred.NewRoutesPreferredClient(ctx,\n\t\toption.WithCredentialsFile(credentialsFile))\n\tdefer c.Close()\n\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// create the origin using a latitude and longitude\n\torigin := &routespb.Waypoint{\n\t\tLocationType: &routespb.Waypoint_Location{\n\t\t\tLocation: &routespb.Location{\n\t\t\t\tLatLng: &latlng.LatLng{\n\t\t\t\t\tLatitude: 37.417670,\n\t\t\t\t\tLongitude: -122.0827784,\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t}\n\n\t// create the destination using a latitude and longitude\n\tdestination := &routespb.Waypoint{\n\t\tLocationType: &routespb.Waypoint_Location{\n\t\t\tLocation: &routespb.Location{\n\t\t\t\tLatLng: &latlng.LatLng{\n\t\t\t\t\tLatitude: 37.417670,\n\t\t\t\t\tLongitude: -122.079595,\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t}\n\n\t// create the request with additional options\n\treq := &routespb.ComputeRoutesRequest{\n\t\tOrigin: origin,\n\t\tDestination: destination,\n\t\tTravelMode: routespb.RouteTravelMode_DRIVE,\n\t\tRoutingPreference: routespb.RoutingPreference_TRAFFIC_AWARE,\n\t\tComputeAlternativeRoutes: true,\n\t\tUnits: routespb.Units_METRIC,\n\t\tLanguageCode: \"en-us\",\n\t\tRouteModifiers: &routespb.RouteModifiers{\n\t\t\tAvoidTolls: false,\n\t\t\tAvoidHighways: true,\n\t\t\tAvoidFerries: true,\n\t\t},\n\t\tPolylineQuality: routespb.PolylineQuality_OVERVIEW,\n\t}\n\n\t// set the field mask\n\tctx = metadata.AppendToOutgoingContext(ctx, \"X-Goog-Fieldmask\", fieldMask)\n\n\t// execute rpc\n\tresp, err := c.ComputeRoutes(ctx, req)\n\n\tif err != nil {\n\t\t// \"rpc error: code = InvalidArgument desc = Request contains an invalid\n\t\t// argument\" may indicate that your project lacks access to Routes Preferred\n\t\tlog.Fatal(err)\n\t}\n\n\tfmt.Printf(\"Duration of route %d\", resp.Routes[0].Duration.Seconds)\n}\nhttps://github.com/googlemaps/go-routespreferred/blob/a6b37904c1ab4f5070e17a7be48e22c387a8197f/samples/compute-routes/main.go#L2-L96\n```\n\n\nCalculating toll fees example\n-----------------------------\n\n\nThe following example uses the\n[`computeRoutes`](/maps/documentation/routes_preferred/reference/rest/v1alpha/TopLevel/computeRoutes)\nmethod to return toll information with an estimated\nprice when a toll pass is used.\n\nThis feature is enabled with the `routes.travelAdvisory.tollInfo` field mask\nspecified in the request. The toll pass is specified in the `route_modifiers`\nfield. The toll price returned is based on the pricing used by the\nspecified pass. If more than one pass is specified, the least expensive\npricing is returned.\n\n### Request:\n\n curl -X POST -d '{\n \"origin\":{\n \"location\":{\n \"lat_lng\":{\n \"latitude\":47.7020056,\n \"longitude\":-122.3479236\n }\n }\n },\n \"destination\":{\n \"location\":{\n \"lat_lng\":{\n \"latitude\":47.6192234,\n \"longitude\": -122.1676792\n }\n }\n },\n \"travel_mode\":\"DRIVE\",\n \"route_modifiers\":{\n \"vehicle_info\":{\n \"emission_type\": \"GASOLINE\"\n },\n \"toll_passes\": [\n \"US_MA_EZPASSMA\",\n \"US_WA_GOOD_TO_GO\"\n ]\n }\n }' \\\n -H 'Content-Type: application/json' \\\n -H 'X-Goog-Api-Key: \u003cYOUR_API_KEY\u003e' \\\n -H 'X-Goog-FieldMask: routes.duration,routes.distanceMeters,routes.travelAdvisory.tollInfo,routes.legs.travelAdvisory.tollInfo' \\\n 'https://routespreferred.googleapis.com/v1alpha:computeRoutes'\n\n### Response:\n\n| **Note** : The Routes Preferred API provides an `estimatedPrice` that applies to non-commercial vehicles.\n|\n| - Toll prices may be different for commercial vehicles.\n- The `units` and `nanos` are omitted when their value is zero. \n\n {\n \"routes\": [\n {\n \"legs\": [\n {\n \"travelAdvisory\": {\n \"tollInfo\": {\n \"estimatedPrice\": [\n {\n \"currencyCode\": \"USD\",\n \"units\": \"3\",\n \"nanos\": 400000000\n }\n ]\n }\n }\n }\n ],\n \"distanceMeters\": 22496,\n \"duration\": \"1400s\",\n \"travelAdvisory\": {\n \"tollInfo\": {\n \"estimatedPrice\": [\n {\n \"currencyCode\": \"USD\",\n \"units\": \"3\",\n \"nanos\": 400000000\n }\n ]\n }\n }\n }\n ]\n }"]]