[Huawei Developers] Freemind R:
In this short tutorial I will guide you step by step on how to plot a route between 2 cities (namely Toronto and Montreal in Canada) on a map using HMS Maps Kit and retrieving the data from Huawei Directions API.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/022/899/763/5190034000022899763.20200323180805.62194392111865123155156728867695:50510511142931:2800:4086993A5EC6FBDE830125B6F1AF88B27CB8FF22514C004F394F04534479D368.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/022/899/763/5190034000022899763.20200323180805.62194392111865123155156728867695:50510511142931:2800:4086993A5EC6FBDE830125B6F1AF88B27CB8FF22514C004F394F04534479D368.png[/IMG]​

Prerrequisites

I will assume you have the following knowledge:

Android development

Kotlin

Retrofit 2 and GSON libraries

Setting up your app to use HMS Maps Kit (if you don't please review this quick codelab)

Without further due, let's get started!

Showing the map

First of all, let's define our layout. It will simply be a SupportMapFragment, as follows:

<?xml version="1.0" encoding="utf-8"?>

<fragment xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/map"

android:name="com.huawei.hms.maps.SupportMapFragment"

android:layout_width="match_parent"

android:layout_height="match_parent">

</fragment>

As you can see, we're using com.huawei.hms.maps.SupportMapFragment as the fragment. We save this layout as activity_route_demo.

Now we can define our activity and set the layout content to it as usual:

class RouteDemoActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_route_demo)

}

}

If you set up this activity as the main activity and run the map, it should show the map. I suggest you do so now to troubleshoot any possible problems connecting with HMS before proceeding.

Marking route origin and destination on the map

Now we need to mark Toronto and Montreal on the map.

class RouteDemoActivity : AppCompatActivity() {

private val TORONTO = LatLng(43.6532, -79.3832)

private val MONTREAL = LatLng(45.5017, -73.5673)

private lateinit var map: HuaweiMap

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_route_demo_hms)

(supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment).getMapAsync {

map = it

setMarkers()

}

}

private fun setMarkers() {

val startMarker = MarkerOptions().position(TORONTO)

map.addMarker(startMarker)

val endMarker = MarkerOptions().position(MONTREAL)

map.addMarker(endMarker)

val cameraUpdate = CameraUpdateFactory.newLatLngZoom(TORONTO, 5f)

map.moveCamera(cameraUpdate)

}

}

We added both markers and moved and zoomed the camera on Toronto. Note that any actions on the map are done after the map has been initialized.

Defining data structures and call for Directions API

To query Huawei Directions API we will use Retrofit and Gson, so we add the corresponding dependencies:

dependencies {

implementation 'com.squareup.retrofit2:retrofit:2.7.2'

implementation 'com.squareup.retrofit2:converter-gson:2.7.2'

implementation 'com.google.code.gson:gson:2.8.6'

}

We specify base URL for Directions API (for more details, please check the documentation)

private val BASE_URL = "https://mapapi.cloud.huawei.com/mapApi/v1/routeService/"

We define the data structures for the Directions API query:

data class RouteServiceRequest(

@SerializedName("destination")

val destination: Location = Location(),

@SerializedName("origin")

val origin: Location = Location()

)

data class Location(

@SerializedName("lat")

val lat: Double = 0.0,

@SerializedName("lng")

val lng: Double = 0.0

)

And the response:

data class Routes(

@SerializedName("returnCode")

val returnCode: String = "",

@SerializedName("returnDesc")

val returnDesc: String = "",

@SerializedName("routes")

val routes: List<Route> = listOf()

)

data class Route(

@SerializedName("bounds")

val bounds: Bounds = Bounds(),

@SerializedName("paths")

val paths: List<Path> = listOf()

)

data class Path(

@SerializedName("distance")

val distance: Int = 0,

@SerializedName("duration")

val duration: Int = 0,

@SerializedName("durationInTraffic")

val durationInTraffic: Int = 0,

@SerializedName("endLocation")

val endLocation: Location = Location(),

@SerializedName("startLocation")

val startLocation: Location = Location(),

@SerializedName("steps")

val steps: List<Step> = listOf()

)

data class Bounds(

@SerializedName("northeast")

val northeast: Location = Location(),

@SerializedName("southwest")

val southwest: Location = Location()

)

data class Step(

@SerializedName("action")

val action: String = "",

@SerializedName("distance")

val distance: Int = 0,

@SerializedName("duration")

val duration: Int = 0,

@SerializedName("endLocation")

val endLocation: Location = Location(),

@SerializedName("orientation")

val orientation: Int = 0,

@SerializedName("polyline")

val polyline: List<Location> = listOf(),

@SerializedName("roadName")

val roadName: String = "",

@SerializedName("startLocation")

val startLocation: Location = Location()

)

This is not the end. For more content about this operation, you can visit THIS ARTICLE in HUAWEI Developer Forum.