We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All
GuidesMap KitAndroidEnhanced SDKRoute

Route

HuaweiMapEx provides the following route drawing functions:

  • Sets route sections based on the traffic condition and point indexes on the route. You can set a style for each section, including the fill color and stroke color.
  • Shows an arrow indicating the road direction. You can set whether to show the arrow.
  • Sets different styles for multiple routes (primary and alternative routes).
  • Supports the route click event.
  • Displays road names.
  • Sets route animations, including the extension animation and transparency animation.

Drawing a Route

Call the addRouteLine method of HuaweiMapEx to draw a route on the map.

The sample code is as follows:
Java
Kotlin
Collapse
Word wrap
Dark theme
Copy code
  1. public void addRouteLine(View view) {
  2. if (null == hMapEx) {
  3. return;
  4. }
  5. if (null != nRouteLine) {
  6. nRouteLine.remove();
  7. nRouteLine = null;
  8. }
  9. if (null != gRouteLine) {
  10. gRouteLine.remove();
  11. gRouteLine = null;
  12. }
  13. // Add a route.
  14. nRouteLine = hMapEx.addRouteLine(new RouteLineOptions().add(new LatLng(47.893478, 2.334595),
  15. new LatLng(48.993478, 3.434595),
  16. new LatLng(48.693478, 2.134595),
  17. new LatLng(48.793478, 2.334595)));
  18. gRouteLine = hMapEx.addRouteLine(new RouteLineOptions().add(
  19. new LatLng(47,3),
  20. new LatLng(47,4),
  21. new LatLng(48,4),
  22. new LatLng(48,5),
  23. new LatLng(48,4),
  24. new LatLng(49,4))
  25. );
  26. }
Collapse
Word wrap
Dark theme
Copy code
  1. fun addRouteLine(view: View?) {
  2. if (null == hMapEx) {
  3. return
  4. }
  5. if (null != nRouteLine) {
  6. nRouteLine!!.remove()
  7. nRouteLine = null
  8. }
  9. if (null != gRouteLine) {
  10. gRouteLine!!.remove()
  11. gRouteLine = null
  12. }
  13. // Add a route.
  14. nRouteLine = hMapEx.addRouteLine(RouteLineOptions().add(LatLng(47.893478, 2.334595),
  15. LatLng(48.993478, 3.434595),
  16. LatLng(48.693478, 2.134595),
  17. LatLng(48.793478, 2.334595)))
  18. gRouteLine = hMapEx.addRouteLine(RouteLineOptions().add(
  19. LatLng(47.0, 3.0),
  20. LatLng(47.0, 4.0),
  21. LatLng(48.0, 4.0),
  22. LatLng(48.0, 5.0),
  23. LatLng(48.0, 4.0),
  24. LatLng(49.0, 4.0))
  25. )
  26. }

Setting the Road Name

Call the setLabelsText method of RouteLine to set the road name.

The sample code is as follows:
Java
Kotlin
Collapse
Word wrap
Dark theme
Copy code
  1. private final List<String> textListLabel = new ArrayList<>();
  2. private final List<Integer> indexListLabel = new ArrayList<>();
  3. public void setLabelsText(View view) {
  4. try{
  5. if (null != nRouteLine) {
  6. // Set the road name.
  7. textListLabel.add("text");
  8. indexListLabel.add(1);
  9. indexListLabel.add(3);
  10. nRouteLine.setLabelsText(textListLabel,indexListLabel,"EN",false);
  11. }
  12. }catch (Exception e){
  13. }
  14. }
Collapse
Word wrap
Dark theme
Copy code
  1. private var textListLabel : ArrayList<String> = ArrayList()
  2. private var indexListLabel: ArrayList<Int> = ArrayList()
  3. fun setLabelsText(view : View?) {
  4. try{
  5. if (null != nRouteLine) {
  6. // Set the road name.
  7. textListLabel.add("text")
  8. indexListLabel.add(1)
  9. indexListLabel.add(3)
  10. nRouteLine!!.setLabelsText(textListLabel,indexListLabel,"EN",false)
  11. }
  12. }catch (e : Exception ){
  13. }
  14. }

Setting the Extension Animation for a Route

Create a LineExtendAnimation object and set its attributes. Call the setAnimation method of RouteLine to set the extension animation, and then call the startAnimation method to start the animation.

The sample code is as follows:
Java
Kotlin
Collapse
Word wrap
Dark theme
Copy code
  1. public void extendAnimation(View view) {
  2. if(nRouteLine == null){
  3. Toast.makeText(this, "Please add RouteLine first.", Toast.LENGTH_SHORT).show();
  4. return;
  5. }
  6. extendAnimation = new LineExtendAnimation();
  7. extendAnimation.setDuration(5000);
  8. extendAnimation.setInterpolator(LineAnimation.LineInterpolator.LINEARINTERPOLATOR);
  9. // Add the extension animation for the route.
  10. nRouteLine.setAnimation(extendAnimation);
  11. // Start the animation.
  12. nRouteLine.startAnimation();
  13. }
Collapse
Word wrap
Dark theme
Copy code
  1. lateinit var extendAnimation : LineExtendAnimation;
  2. fun extendAnimation(view: View?) {
  3. if (nRouteLine == null) {
  4. Toast.makeText(this, "Please add RouteLine first.", Toast.LENGTH_SHORT).show()
  5. return
  6. }
  7. extendAnimation = LineExtendAnimation()
  8. extendAnimation.duration = 5000
  9. extendAnimation.interpolator = LineAnimation.LineInterpolator.LINEARINTERPOLATOR
  10. // Add the extension animation for the route.
  11. nRouteLine!!.setAnimation(extendAnimation)
  12. // Start the animation.
  13. nRouteLine!!.startAnimation()
  14. }

Search in Guides
Enter a keyword.