Requesting HERE Traffic with Effective Polyline Corridor

Requesting HERE Traffic with Effective Polyline Corridor
Imagine being able to monitor the traffic on the busiest street in your city in real-time, allowing you to optimize routes and reduce travel time. With the HERE Traffic API, this is not just a possibility but a reality. In today's fast-paced world, real-time traffic data is crucial for navigation and logistics applications. The HERE Traffic API provides developers with the tools to access real-time traffic information in three ways:
- Center point and radius in meters;
- By a rectangular bounding box;
- By using a flexible polyline corridor.
In this blog post, we will walk through the process of requesting HERE Traffic data using a flexible polyline corridor. This method involves encoding a series of coordinates into a polyline, which can then be used to define a corridor for traffic data retrieval. This can be very helpful if your use-case involves monitoring a certain route or road segment, like the busiest street in your area.
Get an API Key from HERE Platform and get started
Before you can start making requests to the HERE Traffic API, you need to obtain an API key. Follow these steps to get your API key:
- Sign Up/Log In: Go to the HERE Platform and sign up or log in to your account.
- Create a Project: Once logged in, create a new project or select an existing one.
- Generate API Key: Navigate to the Apps section under Access Manager and generate a new API key for your project.
Now you are all set! You can start requesting.
Build a Request URL
To request traffic data, you need to construct a URL that includes your API key and the parameters for the traffic data you want to retrieve. The base URL for the HERE Traffic API is:
https://data.traffic.hereapi.com/v7/flow
You will need to append query parameters to this URL. For example:
https://data.traffic.hereapi.com/v7/flow?locationReferencing=shape&in=corridor:YOUR_POLYLINE;r=50&useRefReplacements=false&advancedFeatures=deepCoverage&apiKey=YOUR_API_KEY
Replace YOUR_POLYLINE with your encoded polyline and YOUR_API_KEY with your actual API key.
Here is a Working Example provided in the Documentation:
https://data.traffic.hereapi.com/v7/flow?locationReferencing=shape&in=corridor:BF05xgKuy2xCx9B7vUl0OhnR54EqSzpEl-HxjD3pBiGnyGi2CvwFsgD3nD4vB6e;r=50&useRefReplacements=false&advancedFeatures=deepCoverage&apiKey=YOUR_API_KEY

Building a Flexible Polyline ID
Creating a flexible polyline involves encoding a list of coordinates into a compact string. The HERE Maps GitHub repository provides extensive documentation on how to get started. Let's replicate the process using Anaconda and Python.
Setting Up the Environment
I have created a separate environment in Anaconda named flexible_polyline to install the flexible polyline dependencies there.

Start by opening your terminal and activating the flexible_polyline environment.
Then install the flexpolyline package as described in the documentation.
pip install flexpolyline

Running the Example
Next, to run the given example, I will open a code editor and create a Python file. Let's call it flexpolyline_example.py.
Here is an example of what the content of flexpolyline_example.py looks like:

In the environment terminal, run the flexpolyline_example.py file to see the flexible polyline ID returned in the console.
python flexpolyline_example.py

Now we can replace the sample ID with the created one and see the traffic information in the browser.

Conclusion
You saw it yourself, it's as easy as that to create a flexible polyline for any road segment to monitor traffic and more. Please be aware that if you are not familiar with Python, you can use other languages listed in the HERE GitHub repository.
Have fun coding and see you in the next one!
Sign up for our newsletter
Why sign up:
- Latest offers and discounts
- Tailored content delivered weekly
- Exclusive events
- One click to unsubscribe