How To use TomTom Matrix API
Posted By : Pradeep Singh Kushwah | 28-Jun-2018
Introduction:-
The TomTom Maps API's allow you to build easily location-aware applications. A number of cross-platform REST API's and SDK's are available to enhance and build your web apps and mobile with routing, search, traffic, mapping and navigation features.
Matrix. Gives you a calculation of a matrix of route summaries for a set of routes defined with destination and origin locations. The Matrix API uses Online Routing to compute route details like distance and Time. It supports Online Routing parameters excluding maxAlternatives, locations, language, instructionsType, routeRepresentation, computeBestOrder, vehicleHeading, report, callback. Matrix API uses HTTP POST requests.
For every origin this Matrix service calculates the distance and time of routing from every origin to every given destination. The Number of origins and the Number of destinations can be thought of as the row and column headers of a table and every cell in the table contains the total costs of routing from the origin to the destination for that cell. The following costs are computed for each route:
distances
travel times
Use the computed total costs to determine which routes to calculate using the Tomtom Routing API.
Example of matrix locations
For origins: X, Y, Z, and destinations: A, B - the following route will be provided in a matrix API response in Given order:
Origin Destination
X A
Y A
Z A
X B
Y B
Z B
So here is POST Call URL
https://api.tomtom.com/routing/1/matrix/JSON?key=<APIKEY>&routeType=shortest&travelMode=truck?
Request Body:
{
"origins": [
{
"point": {"latitude": 56.48955615,"longitude": 10.02088684}
},
{
"point": {"latitude": 56.48955615,"longitude": 10.02088684}
}
],
"destinations": [
{
"point": {"latitude": 56.48955615,"longitude": 10.02088684}
},
{
"point": {"latitude": 56.49417408,"longitude": 10.02308995}
}
]
}
So after this, I make a API Key Using this Link from tomtom https://developer.tomtom.com/user/login?destination=user/me/apps
After Geting the Key From tomtom I make a post call Using HTTP Client in java which return response map.
private Map submitToTomtom(String requestBody) {
HttpClient httpclient = HttpClients.createDefault();
URIBuilder builder = null;
try {
builder = new URIBuilder("https://api.tomtom.com/routing/1/matrix/json");
List<NameValuePair> params = new ArrayList<NameValuePair>(2);
params.add(new BasicNameValuePair("key", key));
params.add(new BasicNameValuePair("routeType", "shortest"));
params.add(new BasicNameValuePair("travelMode", "car"));
builder.setParameters(params);
HttpPost httppost = new HttpPost(builder.build());
httppost.setHeader("Content-Type","application/json");
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
httppost.setEntity(new ByteArrayEntity(requestBody.getBytes("UTF-8")));
HttpResponse response = httpclient.execute(httppost);
ObjectMapper mapper = new ObjectMapper();
Map resp= mapper.readValue(response.getEntity().getContent(), HashMap.class);
return resp;
} catch (URISyntaxException | IOException e) {
e.printStackTrace();
}
return null;
}
It will give You the response Like this:
{
"formatVersion": "0.0.1",
"matrix": [
[
{
"statusCode": 200,
"response": {
"routeSummary": {
"lengthInMeters":500,
"travelTimeInSeconds":103,
"trafficDelayInSeconds": 0,
"departureTime": "2016-09-03T15:01:57+03:00",
"arrivalTime": "2016-09-03T15:04:34+03:00",
"noTrafficTravelTimeInSeconds":136,
"historicTrafficTravelTimeInSeconds":142,
"liveTrafficIncidentsTravelTimeInSeconds":142
}
}
},
{
"statusCode": 400,
"response": "Engine error while executing route request: MAP_MATCHING_FAILURE"
}
],
[
{
"statusCode": 200,
"response": {
"routeSummary": {
"lengthInMeters":500,
"trafficDelayInSeconds": 0,
"departureTime": "2016-09-03T15:01:57+03:00",
"arrivalTime": "2016-09-03T15:04:34+03:00",
"travelTimeInSeconds":116,
"noTrafficTravelTimeInSeconds":113,
"historicTrafficTravelTimeInSeconds":115,
"liveTrafficIncidentsTravelTimeInSeconds":115
}
}
},
{
"statusCode": 200,
"response": {
"routeSummary": {
"lengthInMeters":668832,
"trafficDelayInSeconds": 0,
"departureTime": "2016-09-03T15:01:57+03:00",
"arrivalTime": "2016-09-03T15:04:34+03:00",
"travelTimeInSeconds":23838,
"noTrafficTravelTimeInSeconds":21732,
"historicTrafficTravelTimeInSeconds":23403,
"liveTrafficIncidentsTravelTimeInSeconds":23838
}
}
}
]
],
"summary": {
"successfulRoutes": 3,
"totalRoutes": 4
}
}
For more Detail checkout this Link https://developer.tomtom.com/online-routing/online-routing-documentation/matrix-routing
Thanks.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Pradeep Singh Kushwah
Pradeep is an accomplished Backend Developer with in-depth knowledge and hands-on experience in various cutting-edge technologies. He specializes in Core Java, Spring-Boot, Optaplanner, Angular, and databases such as MongoDB, Neo4j, Redis, and PostgreSQL. Additionally, he has worked with cloud services like AWS and Google Cloud, and he has experience with monitoring tools such as Datadog and Raygun. Pradeep has honed his skills in API Implementations, Integration, optimization, Webservices, Development Testings, and deployments, code enhancements, and has contributed to company values through his deliverables in various client projects, including Kairos, Slick Payroll, Captionlabs, and FarmQ. He is a creative individual with strong analytical skills and a passion for exploring and learning new technologies.