Compartir a través de


Azure Maps SDK Route client library for Java - version 1.0.0-beta.2

Azure Maps SDK Route client library for Java.

This package contains Microsoft Azure SDK for Route Management SDK which contains Azure Maps Route REST APIs. For documentation on how to use this package, please see Azure Management Libraries for Java.

Source code | API reference documentation | REST API documentation | Product documentation | Samples

Documentation

Various documentation is available to help you get started

Getting started

Prerequisites

Adding the package to your product

<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-maps-route</artifactId>
    <version>1.0.0-beta.2</version>
</dependency>

Azure Maps Libraries require a TokenCredential implementation for authentication and an HttpClient implementation for HTTP client.

Azure Identity package and Azure Core Netty HTTP package provide the default implementation.

Authentication

By default, Azure Active Directory token authentication depends on correct configure of following environment variables.

  • AZURE_CLIENT_ID for Azure client ID.
  • AZURE_TENANT_ID for Azure tenant ID.
  • AZURE_CLIENT_SECRET or AZURE_CLIENT_CERTIFICATE_PATH for client secret or client certificate.

In addition, Azure subscription ID can be configured via environment variable AZURE_SUBSCRIPTION_ID.

With above configuration, azure client can be authenticated by following code:

// Authenticates using Azure AD building a default credential
// This will look for AZURE_CLIENT_ID, AZURE_TENANT_ID, and AZURE_CLIENT_SECRET env variables
DefaultAzureCredential tokenCredential = new DefaultAzureCredentialBuilder().build();

// Creates a builder
MapsRouteClientBuilder builder = new MapsRouteClientBuilder();
builder.credential(tokenCredential);
builder.mapsClientId(System.getenv("MAPS_CLIENT_ID"));
builder.httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS));

// Builds a client
MapsRouteAsyncClient client = builder.buildAsyncClient();

The sample code assumes global Azure. Please change AzureEnvironment.AZURE variable if otherwise.

See Authentication for more options.

Key concepts

See API design for general introduction on design and key concepts on Azure Management Libraries.

Examples

Begin Request Route Matrix

System.out.println("Request route matrix");
RouteMatrixQuery matrixQuery = new RouteMatrixQuery();

// origins
GeoPointCollection origins = new GeoPointCollection(Arrays.asList(
    new GeoPoint(4.85106, 52.36006),
    new GeoPoint(4.85056, 52.36187)
));

// destinations
GeoPointCollection destinations = new GeoPointCollection(Arrays.asList(
    new GeoPoint(4.85003, 52.36241),
    new GeoPoint(13.42937, 52.50931)
));

matrixQuery.setDestinations(destinations);
matrixQuery.setOrigins(origins);

RouteMatrixOptions matrixOptions = new RouteMatrixOptions(matrixQuery);
client.beginGetRouteMatrix(matrixOptions).getFinalResult();

Get Route Directions

System.out.println("Get route directions");
List<GeoPosition> routePoints = Arrays.asList(
    new GeoPosition(13.42936, 52.50931),
    new GeoPosition(13.43872, 52.50274));
RouteDirectionsOptions routeOptions = new RouteDirectionsOptions(routePoints);
RouteDirections directions = client.getRouteDirections(routeOptions);
RouteReport report = directions.getReport(); // get the report and use it

Get Route Directions With Parameters

System.out.println("Get route parameters");
// supporting points
GeoCollection supportingPoints = new GeoCollection(
    Arrays.asList(
        new GeoPoint(13.42936, 52.5093),
        new GeoPoint(13.42859, 52.50844)
        ));

// avoid areas
List<GeoPolygon> polygons = Arrays.asList(
    new GeoPolygon(
        new GeoLinearRing(Arrays.asList(
            new GeoPosition(-122.39456176757811, 47.489368981370724),
            new GeoPosition(-122.00454711914061, 47.489368981370724),
            new GeoPosition(-122.00454711914061, 47.65151268066222),
            new GeoPosition(-122.39456176757811, 47.65151268066222),
            new GeoPosition(-122.39456176757811, 47.489368981370724)
        ))
    ),
    new GeoPolygon(
        new GeoLinearRing(Arrays.asList(
            new GeoPosition(100.0, 0.0),
            new GeoPosition(101.0, 0.0),
            new GeoPosition(101.0, 1.0),
            new GeoPosition(100.0, 1.0),
            new GeoPosition(100.0, 0.0)
        ))
    )
);
GeoPolygonCollection avoidAreas = new GeoPolygonCollection(polygons);
RouteDirectionsParameters parameters = new RouteDirectionsParameters()
    .setSupportingPoints(supportingPoints)
    .setAvoidVignette(Arrays.asList("AUS", "CHE"))
    .setAvoidAreas(avoidAreas);
client.getRouteDirections(routeOptions,
    parameters);

Get Route Range

System.out.println("Get route range");
RouteRangeOptions rangeOptions = new RouteRangeOptions(new GeoPosition(50.97452, 5.86605), Duration.ofSeconds(6000));
client.getRouteRange(rangeOptions);

Begin Request Route Directions Batch

RouteDirectionsOptions options1 = new RouteDirectionsOptions(
    Arrays.asList(new GeoPosition(-122.128384, 47.639987),
        new GeoPosition(-122.184408, 47.621252),
        new GeoPosition(-122.332000, 47.596437)))
    .setRouteType(RouteType.FASTEST)
    .setTravelMode(TravelMode.CAR)
    .setMaxAlternatives(5);

RouteDirectionsOptions options2 = new RouteDirectionsOptions(
    Arrays.asList(new GeoPosition(-122.348934, 47.620659),
        new GeoPosition(-122.342015, 47.610101)))
    .setRouteType(RouteType.ECONOMY)
    .setTravelMode(TravelMode.BICYCLE)
    .setUseTrafficData(false);

RouteDirectionsOptions options3 = new RouteDirectionsOptions(
    Arrays.asList(new GeoPosition(-73.985108, 40.759856),
        new GeoPosition(-73.973506, 40.771136)))
    .setRouteType(RouteType.SHORTEST)
    .setTravelMode(TravelMode.PEDESTRIAN);

System.out.println("Get Route Directions Batch");

List<RouteDirectionsOptions> optionsList = Arrays.asList(options1, options2, options3);
SyncPoller<RouteDirectionsBatchResult, RouteDirectionsBatchResult> poller =
    client.beginRequestRouteDirectionsBatch(optionsList);
poller.getFinalResult();

Troubleshooting

When you interact with the Azure Maps Services, errors returned by the Maps service correspond to the same HTTP status codes returned for REST API requests.

For example, if you search with an invalid coordinate, a error is returned, indicating "Bad Request".400

Next steps

Several Azure Maps Route Java SDK samples are available to you in the SDK's GitHub repository. Azure Maps Route Samples

Contributing

For details on contributing to this repository, see the contributing guide.

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Impressions