Define Persistent Roadblock
In this guide you will learn how to render an interactively defined road block on an interactive map and fly to the road block.
Once the map is loaded, zoom in and tap on a road to define a road block.
The selected road section is indicated with red roadblock polylines which may be on one or both sides of the road, depending on the road type.
Clicking on a different road section will move the roadblock there.
The map is interactive and fully 3D, supporting pan, pinch-zoom, rotate and tilt.
The roadblock
variable is defined in the class MainActivity : AppCompatActivity()
class to store the current section of road set as a roadblock by the user.
private var roadblock: TrafficEvent? = null
In the class MainActivity : AppCompatActivity()
, in the onCreate()
function the x,y position of a user touch event is obtained using onTouch
and set in cursorScreenPosition
so the map can be queried about traffic events, if any, at that location.
If there are 1 or more traffic events at the touch location, if (!trafficEvents.isNullOrEmpty())
then the roadblock is set on the first traffic event in the list (at index 0).
val trafficEvent = trafficEvents[0]
if (trafficEvent.isRoadblock())
Otherwise, if there are 1 or more road sections at the touch location,if (!streets.isNullOrEmpty())
then the roadblock is set on the first street in the list (at index 0)streets[0].coordinates?.let { addPersistentRoadblock(it) }
.
The addPersistentRoadblock()
function gets the current time and sets the roadblock end time 1 minute into the future.
The roadblock class member variable is initially set to null, as we have seen above. Here it is checked and if it is not null, roadblock?.let { roadblock ->
that means the user already set a previous roadblock.
In that case, the previous roadblock is removed first:
roadblock.referencePoint?.let { coordinates ->
traffic.removePersistentRoadblock(coordinates) }
Then the new roadblock is added, and saved in the class member variable:
roadblock = traffic.addPersistentRoadblock(
coords = arrayListOf(coordinates),
startUTC = startTime,
expireUTC = endTime,
transportMode = ERouteTransportMode.Car.value)
Finally, the camera flies to the bounding box enclosing the roadblock section to center it in the viewing area such that it fills the viewport.