Azure Maps SDK Traffic client library for Java - version 1.0.0-beta.1
Azure Maps SDK Traffic client library for Java.
This package contains the Azure Maps SDK Traffic client library which contains Azure Maps Traffic APIs. For documentation on how to use this package, please see Azure Maps Traffic SDK 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
- Java Development Kit (JDK) with version 8 or above
- Azure Subscription
Adding the package to your product
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-maps-traffic</artifactId>
<version>1.0.0-beta.1</version>
</dependency>
Include the recommended packages
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
orAZURE_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
TrafficClientBuilder builder = new TrafficClientBuilder();
builder.credential(tokenCredential);
builder.trafficClientId(System.getenv("MAPS_CLIENT_ID"));
builder.httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS));
// Builds a client
TrafficClient client = builder.buildClient();
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
Get Traffic Flow Segment
System.out.println("Get Traffic Flow Segment:");
// options
client.getTrafficFlowSegment(
new TrafficFlowSegmentOptions()
.setTrafficFlowSegmentStyle(TrafficFlowSegmentStyle.ABSOLUTE).setZoom(10)
.setCoordinates(new GeoPosition(4.84239, 52.41072)));
// complete
client.getTrafficFlowSegment(
new TrafficFlowSegmentOptions()
.setTrafficFlowSegmentStyle(TrafficFlowSegmentStyle.ABSOLUTE).setZoom(10)
.setCoordinates(new GeoPosition(4.84239, 52.41072)).setOpenLr(false)
.setThickness(2).setUnit(SpeedUnit.MPH));
Get Traffic Flow Tile
System.out.println("Get Traffic Flow Tile:");
// options
client.getTrafficFlowTile(
new TrafficFlowTileOptions()
.setTrafficFlowTileStyle(TrafficFlowTileStyle.RELATIVE_DELAY).setFormat(TileFormat.PNG).setZoom(10));
// complete
client.getTrafficFlowTile(
new TrafficFlowTileOptions()
.setTrafficFlowTileStyle(TrafficFlowTileStyle.RELATIVE_DELAY).setFormat(TileFormat.PNG).setZoom(10)
.setTileIndex(new TileIndex().setX(2044).setY(1360)).setThickness(10));
Get Traffic Incident Detail
System.out.println("Get Traffic Incident Detail:");
// options
client.getTrafficIncidentDetail(
new TrafficIncidentDetailOptions()
.setBoundingBox(new GeoBoundingBox(45, 45, 45, 45)).setBoundingZoom(11)
.setIncidentDetailStyle(IncidentDetailStyle.S3).setBoundingZoom(11)
.setTrafficmodelId("1335294634919"));
// complete
client.getTrafficIncidentDetail(
new TrafficIncidentDetailOptions()
.setBoundingBox(new GeoBoundingBox(45, 45, 45, 45)).setBoundingZoom(11)
.setIncidentDetailStyle(IncidentDetailStyle.S3).setBoundingZoom(11)
.setTrafficmodelId("1335294634919").setLanguage("en")
.setProjectionStandard(ProjectionStandard.EPSG900913).setIncidentGeometryType(IncidentGeometryType.ORIGINAL)
.setExpandCluster(false).setOriginalPosition(false));
Get Traffic Incident Tile
System.out.println("Get Traffic Incident Tile:");
// options
client.getTrafficIncidentTile(
new TrafficIncidentTileOptions()
.setFormat(TileFormat.PNG).setTrafficIncidentTileStyle(TrafficIncidentTileStyle.S3)
.setZoom(10));
// complete
client.getTrafficIncidentTile(
new TrafficIncidentTileOptions()
.setFormat(TileFormat.PNG).setTrafficIncidentTileStyle(TrafficIncidentTileStyle.S3)
.setZoom(10).setTileIndex(new TileIndex().setX(175).setY(408)));
Get Traffic Incident Viewport
System.out.println("Get Traffic Incident Tile:");
// options
client.getTrafficIncidentViewport(
new TrafficIncidentViewportOptions()
.setBoundingBox(new GeoBoundingBox(45, 45, 45, 45))
.setBoundingZoom(2).setOverview(new GeoBoundingBox(45, 45, 45, 45))
.setOverviewZoom(2));
// complete
client.getTrafficIncidentViewport(
new TrafficIncidentViewportOptions()
.setBoundingBox(new GeoBoundingBox(45, 45, 45, 45))
.setBoundingZoom(2).setOverview(new GeoBoundingBox(45, 45, 45, 45))
.setOverviewZoom(2).setCopyright(true));
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 400 - Bad Request
Next steps
Several Azure Maps Traffic Java SDK samples are available to you in the SDK's GitHub repository. Azure Maps Traffic Samples
Contributing
For details on contributing to this repository, see the contributing guide.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
Azure SDK for Java