이 페이지에서는 Routes API용 클라이언트 라이브러리를 시작하는 방법을 설명합니다.
클라이언트 라이브러리에 대한 자세한 내용은 클라이언트 라이브러리 참조 설명했습니다.
클라이언트 라이브러리 설치
자바
자세한 내용은 자바 개발 환경 설정을 참고하세요.
Java용 Google Routes API 클라이언트를 참조하세요. 에서 설치 안내를 확인하세요.
Go
go get cloud.google.com/go/maps
Go용 Google Routes API 클라이언트를 참고하세요. 에서 설치 안내를 확인하세요.
Node.js
npm install @googlemaps/routing
전체 설치 안내는 Node.js용 Google Routes API 클라이언트를 참조하세요.
Python
자세한 내용은 Python 개발 환경 설정을 참조하세요.
전체 설치 안내는 Python용 Google Routes API 클라이언트를 참고하세요.
.NET
자세한 내용은 .Net 개발 환경 설정을 참조하세요.
전체 설치 안내는 .Net용 Google Routes API 클라이언트를 참조하세요.
인증 설정
클라이언트 라이브러리를 사용할 때는 애플리케이션 기본 사용자 인증 정보(ADC)를 사용하여 인증합니다. ADC 설정은 애플리케이션 기본 사용자 인증 정보에 대한 사용자 인증 정보 제공을 참조하세요. 클라이언트 라이브러리에서 ADC를 사용하는 방법은 클라이언트 라이브러리를 사용하여 인증을 참조하세요.
클라이언트 라이브러리 사용
자바
Compute Routes 예:
import com.google.maps.routing.v2.*; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] arguments) throws IOException { RoutesSettings routesSettings = RoutesSettings.newBuilder() .setHeaderProvider(() -> { Map<String, String> headers = new HashMap<>(); headers.put("X-Goog-FieldMask", "*"); return headers; }).build(); RoutesClient routesClient = RoutesClient.create(routesSettings); ComputeRoutesResponse response = routesClient.computeRoutes(ComputeRoutesRequest.newBuilder() .setOrigin(Waypoint.newBuilder().setPlaceId("ChIJeRpOeF67j4AR9ydy_PIzPuM").build()) .setDestination(Waypoint.newBuilder().setPlaceId("ChIJG3kh4hq6j4AR_XuFQnV0_t8").build()) .setRoutingPreference(RoutingPreference.TRAFFIC_AWARE) .setTravelMode(RouteTravelMode.DRIVE).build()); System.out.println("Response: " + response.toString()); } }
경로 매트릭스 예:
import com.google.api.gax.rpc.ServerStream; import com.google.api.gax.rpc.ServerStreamingCallable; import com.google.maps.routing.v2.*; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] arguments) throws IOException { RoutesSettings routesSettings = RoutesSettings.newBuilder() .setHeaderProvider(() -> { Map<String, String> headers = new HashMap<>(); headers.put("X-Goog-FieldMask", "*"); return headers; }).build(); RoutesClient routesClient = RoutesClient.create(routesSettings); ServerStreamingCallable<ComputeRouteMatrixRequest, RouteMatrixElement> computeRouteMatrix = routesClient.computeRouteMatrixCallable(); ServerStream<RouteMatrixElement> stream = computeRouteMatrix.call(ComputeRouteMatrixRequest.newBuilder() .addOrigins(RouteMatrixOrigin.newBuilder() .setWaypoint(Waypoint.newBuilder(). setPlaceId("ChIJeRpOeF67j4AR9ydy_PIzPuM").build())) .addDestinations(RouteMatrixDestination.newBuilder() .setWaypoint(Waypoint.newBuilder().setPlaceId("ChIJG3kh4hq6j4AR_XuFQnV0_t8").build()) .build()).setTravelMode(RouteTravelMode.DRIVE) .build()); for (RouteMatrixElement element : stream) { System.out.println("Response : " + element.toString()); } } }
Go
Compute Routes 예시:
import ( "context" "fmt" "io" "os" routing "cloud.google.com/go/maps/routing/apiv2" "cloud.google.com/go/maps/routing/apiv2/routingpb" "google.golang.org/grpc/metadata" ) func main() { ctx := context.Background() client, err := routing.NewRoutesClient(ctx) if err != nil { fmt.Fprintf(os.Stderr, "error: %v\n", err) os.Exit(1) } computeRoutesReq := routingpb.ComputeRoutesRequest{ Origin: &routingpb.Waypoint{ LocationType: &routingpb.Waypoint_PlaceId{ PlaceId: "ChIJeRpOeF67j4AR9ydy_PIzPuM", }, }, Destination: &routingpb.Waypoint{ LocationType: &routingpb.Waypoint_PlaceId{ PlaceId: "ChIJG3kh4hq6j4AR_XuFQnV0_t8", }, }, RoutingPreference: routingpb.RoutingPreference_TRAFFIC_AWARE, TravelMode: routingpb.RouteTravelMode_DRIVE, } ctx = metadata.AppendToOutgoingContext(ctx, "X-Goog-FieldMask", "*") computeRoutesResponse, err := client.ComputeRoutes(ctx, &computeRoutesReq) if err != nil { fmt.Fprintf(os.Stderr, "error: %v\n", err) os.Exit(1) } fmt.Fprintf(os.Stdout, "response: %v\n", computeRoutesResponse) }
경로 매트릭스 예:
import ( "context" "fmt" "io" "os" routing "cloud.google.com/go/maps/routing/apiv2" "cloud.google.com/go/maps/routing/apiv2/routingpb" "google.golang.org/grpc/metadata" ) func main() { ctx := context.Background() client, err := routing.NewRoutesClient(ctx) if err != nil { fmt.Printf("error: %v\n", err) os.Exit(1) } computeRouteMatrixReq := routingpb.ComputeRouteMatrixRequest{ Origins: []*routingpb.RouteMatrixOrigin{ { Waypoint: &routingpb.Waypoint{ LocationType: &routingpb.Waypoint_PlaceId{ PlaceId: "ChIJeRpOeF67j4AR9ydy_PIzPuM", }, }, }, }, Destinations: []*routingpb.RouteMatrixDestination{ { Waypoint: &routingpb.Waypoint{ LocationType: &routingpb.Waypoint_PlaceId{ PlaceId: "ChIJG3kh4hq6j4AR_XuFQnV0_t8", }, }, }, }, TravelMode: routingpb.RouteTravelMode_DRIVE, } ctx = metadata.AppendToOutgoingContext(ctx, "X-Goog-FieldMask", "*") stream, err := client.ComputeRouteMatrix(ctx, &computeRouteMatrixReq) if err != nil { fmt.Fprintf(os.Stderr, "error: %v\n", err) os.Exit(1) } for { element, err := stream.Recv() if err == io.EOF { break } if err != nil { fmt.Fprintf(os.Stderr, "error: %v\n", err) os.Exit(1) } fmt.Fprintf(os.Stdout, "element: %v\n", element) } }
Node.js
Compute Routes 예:
// Imports the Routing library const { RoutesClient } = require("@googlemaps/routing").v2; const origin = { location: { latLng: { latitude: 37.419734, longitude: -122.0827784, }, }, }; const destination = { location: { latLng: { latitude: 37.41767, longitude: -122.079595, }, }, }; // Instantiates a client const routingClient = new RoutesClient(); async function callComputeRoutes() { const request = { origin, destination, }; // Run request const response = await routingClient.computeRoutes(request, { otherArgs: { headers: { "Content-Type": "application/json", "X-Goog-Api-Key": "YOUR_API_KEY", "X-Goog-FieldMask": "routes.duration,routes.distanceMeters,routes.polyline.encodedPolyline", }, }, }); console.log(response); } callComputeRoutes();
Compute Route Matrix 예시:
// Imports the Routing library const { RoutesClient } = require("@googlemaps/routing").v2; const origins = [ { waypoint: { location: { latLng: { latitude: 37.420761, longitude: -122.081356, }, }, }, }, { waypoint: { location: { latLng: { latitude: 37.403184, longitude: -122.097371, }, }, }, }, ]; const destinations = [ { waypoint: { location: { latLng: { latitude: 37.420999, longitude: -122.086894, }, }, }, }, { waypoint: { location: { latLng: { latitude: 37.383047, longitude: -122.044651, }, }, }, }, ]; // Instantiates a client const routingClient = new RoutesClient(); async function callComputeRouteMatrix() { // Construct request const request = { origins, destinations, }; // Run request const stream = await routingClient.computeRouteMatrix(request, { otherArgs: { headers: { "Content-Type": "application/json", "X-Goog-Api-Key": "YOUR_API_KEY", "X-Goog-FieldMask": "*", }, }, }); stream.on("data", (response) => { console.log(response); }); stream.on("error", (err) => { throw err; }); stream.on("end", () => { /* API call completed */ }); } callComputeRouteMatrix();
Python
.NET