[Huawei Developers] Freemind R:
Preface

HUAWEI Map Kit includes a route planning function, which offers a set of HTTPS-based APIs. These APIs are used to plan walking, cycling, and driving routes, as well as calculate route distances. They return route data in JSON format, and comprise the route planning capability.

Route planning APIs are as follows:

Walking route planning API: Provides the function for planning walking routes within distances of 100 kilometers or less.

Cycling route planning API: Provides the function for planning cycling routes within distances of 100 kilometers or less.

Driving route planning API: Provides the function for planning driving routes.

Up to 3 routes can be returned for each request.

Up to 5 waypoints can be specified.

Routes can be planned for future travel.

Routes can be planned based on real-time traffic conditions.

Use Cases

Ride hailing: Real-time route planning and route planning for future travel can provide accurate price estimates for ride-hailing orders. The estimated time of arrival (ETA) can be calculated for multiple routes in batches during order dispatch, for dramatically enhanced efficiency.

Logistics: Driving and cycling route planning provides accurate routes, ETAs, and estimated road tolls for trunk and branch road logistics and logistics delivery.

Tourism: When booking hotels and designing tourism routes, users can determine the distance between hotels, scenic spots, and transport stations with greater ease, thanks to high-level route planning capabilities, and enjoy efficient, hassle-free travel at all times.

Preparations

Before using the route planning function, first obtain the API key in AppGallery Connect.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201113030356.84145390254882346478421536862899:50511130071535:2800:BD1801E3708D888EC952212FF76D33BA6EC9545EBB1D0355C305DDED2E24D849.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201113030356.84145390254882346478421536862899:50511130071535:2800:BD1801E3708D888EC952212FF76D33BA6EC9545EBB1D0355C305DDED2E24D849.png[/IMG]

Note

If the API key contains special characters, you need to encode it using encodeURI. For example, if the original API key is ABC/DFG+, the conversion result is ABC%2FDFG%2B.

Apply for the network access permission in the AndroidManifest.xml file.

<!-- Network permission -->
<uses-permission android:name="android.permission.INTERNET" />

Development Procedure

1. Initialize a map for displaying planned routes.

private MapFragment mMapFragment;
private HuaweiMap hMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_directions);

mMapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.mapfragment_mapfragmentdemo);
mMapFragment.getMapAsync(this);
}

2. Obtain the current user location and use it as the start point for route planning.

private void getMyLocation() {
Task<Location> locationTask = LocationServices.getFusedLocationProviderClient(this).getLastLocation();
locationTask.addOnCompleteListener(param0 -> {
if (param0 != null) {
Location location = param0.getResult();
double Lat = location.getLatitude();
double Lng = location.getLongitude();
myLocation = new LatLng(Lat, Lng);
Log.d(TAG, " Lat is : " + Lat + ", Lng is : " + Lng);

CameraUpdate CameraUpdate = CameraUpdateFactory.newLatLng(myLocation);
hMap.moveCamera(CameraUpdate);
}
}).addOnFailureListener(param0 -> Log.d(TAG, "lastLocation is error"));
}

3. Add a map long-press event listener to listen to the end point for route planning.

hMap.setOnMapLongClickListener(latLng -> {
if (null != mDestinationMarker) {
mDestinationMarker.remove();
}
if (null != mPolylines) {
for (Polyline polyline : mPolylines) {
polyline.remove();
}
}
enableAllBtn();
MarkerOptions options = new MarkerOptions().position(latLng).title("dest");
mDestinationMarker = hMap.addMarker(options);
mDestinationMarker.setAnchor(0.5f,1f);

StringBuilder dest = new StringBuilder(String.format(Locale.getDefault(), "%.6f", latLng.latitude));
dest.append(", ").append(String.format(Locale.getDefault(), "%.6f", latLng.longitude));
((TextInputEditText)findViewById(R.id.dest_input)).setText(dest);
mDest = latLng;
});

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201113030418.07975409565412019300007495229077:50511130071535:2800:51CFC25C65487FB68D11C4F5D40D6B90032157724826AD264272A32B7F12C0B6.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201113030418.07975409565412019300007495229077:50511130071535:2800:51CFC25C65487FB68D11C4F5D40D6B90032157724826AD264272A32B7F12C0B6.png[/IMG]

4. Generate a route planning request based on the specified start point and end point.

private JSONObject buildRequest() {
JSONObject request = new JSONObject();
try {
JSONObject origin = new JSONObject();
origin.put("lng", myLocation.longitude);
origin.put("lat", myLocation.latitude);
JSONObject destination = new JSONObject();
destination.put("lng", mDest.longitude);
destination.put("lat", mDest.latitude);
request.put("origin", origin);
request.put("destination", destination);
} catch (JSONException e) {
e.printStackTrace();
}
return request;
}

5. Draw planned routes on the map based on the route planning response.

JSONObject route = new JSONObject(result);
JSONArray routes = route.optJSONArray("routes");
JSONObject route1 = routes.optJSONObject(0);
JSONArray paths = route1.optJSONArray("paths");
JSONObject path1 = paths.optJSONObject(0);
JSONArray steps = path1.optJSONArray("steps");
for (int i = 0; i < steps.length(); i++) {
PolylineOptions options = new PolylineOptions();
JSONObject step = steps.optJSONObject(i);
JSONArray polyline = step.optJSONArray("polyline");
for (int j = 0; j < polyline.length(); j++) {
JSONObject polyline_t = polyline.optJSONObject(j);
options.add(new LatLng(polyline_t.getDouble("lat"), polyline_t.getDouble("lng")));
}
Polyline pl = hMap.addPolyline(options.color(Color.BLUE).width(3));
mPolylines.add(pl);
}

Demo Effects

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201113030511.04497121035333001517433234654322:50511130071535:2800:F23F97CA6AC5EC405BAE2BE44DC67413716ECCB089168B0CD38DBF45B36E557F.gif"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201113030511.04497121035333001517433234654322:50511130071535:2800:F23F97CA6AC5EC405BAE2BE44DC67413716ECCB089168B0CD38DBF45B36E557F.gif[/IMG]