[Huawei Developers] Sherlock F:
Introduction

Online food ordering is process to deliver food from restaurants. In this article will do how to draw polyline, Integrate Location kit in food applications.

In this article, will guide you to selected hotel locations on Huawei map and shows direction from current location to destination.

Steps

1. Create App in Android.

2. Configure App in AGC.

3. Integrate the SDK in our new Android project.

4. Integrate the dependencies.

5. Sync project.

Location Module

Huawei location kit combines the GNSS, Wi-Fi, base station location helps you to obtain users location quickly. It will support different types of functionalities like Fused location, activity identification, geo-fence.

Function Restrictions

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200918094625.59857939138968386535115163054708:50510928022348:2800:838ED4B6F25457BF83F29493F7275B97B47A8A9303A3C21BD4E5920322E1224C.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200918094625.59857939138968386535115163054708:50510928022348:2800:838ED4B6F25457BF83F29493F7275B97B47A8A9303A3C21BD4E5920322E1224C.png[/IMG]

Use Case

1. Fused location: Quickly obtain the user location.

2. Activity Identification: Identifies user status using sensor, cellular network and magnetometer helps to adjust your app based on user behavior.

3. Geofence: This helps you to set boundary for specific area.

Integration

Create Application in Android Studio.

App level gradle dependencies.

apply plugin: 'com.android.application'
apply plugin: 'com.huawei.agconnect'

Gradle dependencies

implementation 'com.huawei.hms:location:4.0.3.301'
implementation 'com.squareup.okhttp3:okhttp:3.14.2'
implementation 'com.squareup.retrofit2:converter-gson:2.6.1'
implementation 'com.squareup.retrofit2:retrofit:2.6.2'
implementation 'com.google.code.gson:gson:2.8.6'

Root level gradle dependencies

maven {url 'https://developer.huawei.com/repo/'}
classpath 'com.huawei.agconnect:agcp:1.3.1.300'

Add the below permissions in Android Manifest file

<manifest xlmns:android>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
</manifest>

1.Create xml layout class define below snippet

<com.huawei.hms.maps.MapView
android:layout_marginTop="?actionBarSize"
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraZoom="8.5"
map:mapType="normal"
map:uiCompass="true"1)
map:uiZoomControls="true"/>

2. Create Instance for FuesdLocationProviderClient and SettingsClient.

3.Check location permissions dynamically.

public void RequestPermission() {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
String[] strings =
{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
ActivityCompat.requestPermissions((Activity) this, strings, 1);
}
} else {
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this,
"android.permission.ACCESS_BACKGROUND_LOCATION") != PackageManager.PERMISSION_GRANTED) {
String[] strings = {Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
"android.permission.ACCESS_BACKGROUND_LOCATION"};
ActivityCompat.requestPermissions((Activity) this, strings, 2);
}
}

}

4. Checking the location settings, if the location permission disabled then we Cannot obtain the location details.

try {
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(mLocationRequest);
LocationSettingsRequest locationSettingsRequest = builder.build();
settingsClient.checkLocationSettings(locationSettingsRequest)
.addOnSuccessListener(locationSettingsResponse -> fusedLocationProviderClient
.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.getMainLooper())
.addOnSuccessListener(v -> Toast.makeText(getApplicationContext(), "APi Fail1", Toast.LENGTH_SHORT).show())
.addOnFailureListener(e -> {
int statusCode = ((ApiException) e).getStatusCode();
switch (statusCode) {
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try {
ResolvableApiException rae = (ResolvableApiException) e;
rae.startResolutionForResult(LocationActivity.this, 0);
} catch (IntentSender.SendIntentException sie) {
}
break;
}
}));
} catch (Exception e) {
}

5. Create latlngModel object.

private latlngModel getLatLangModel() {
latlngModel latlngModel = new latlngModel();
latlngModel.Origin origin = new latlngModel.Origin();
origin.setLat(latlngcurrent.latitude);
origin.setLng(latlngcurrent.longitude);
latlngModel.Destination des = new latlngModel.Destination();
des.setLat(deslatLng.latitude);
des.setLng(deslatLng.longitude);
latlngModel.setDestination(des);
latlngModel.setOrigin(origin);
return latlngModel;
}

6. LocationRequest() LocationCallback class to obtain the LocationResult object.

mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if (null == mLocationCallback) {
mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
if (locationResult != null) {
List<Location> locations = locationResult.getLocations();
if (!locations.isEmpty()) {
for (Location location : locations) {
Toast.makeText(getApplicationContext(), String.valueOf(location.getTime()), Toast.LENGTH_SHORT).show();
latlngcurrent = new LatLng(location.getLatitude(), location.getLongitude());
}
}
}
}

@Override
public void onLocationAvailability(LocationAvailability locationAvailability) {
if (locationAvailability != null) {
Toast.makeText(getApplicationContext(), "APi Fail", Toast.LENGTH_SHORT).show();
}
}
};

}

Map Kit Draw Polyline

Polyline to draw the directions from origin to destination, this will help to show directions in three ways such as Driving, Bicycle and walking.

1. Create Retrofit interface class.

public interface Api {
@POST("driving?key=" + Constants.API_KEY)
Call<DirectionResponse> getDriveDistanceDuration(@Body latlngModel bodys);

@POST("walking?key=" + Constants.API_KEY)
Call<DirectionResponse> getWalkDistanceDuration(@Body latlngModel bodys);

@POST("bicycling?key=" + Constants.API_KEY)
Call<DirectionResponse> getBicycleDistanceDuration(@Body latlngModel bodys);
}

2. Create Retrofit Client class.

private static Retrofit retrofit;

public static Retrofit getRetrofitInstance() {
if (retrofit == null) {
retrofit = new retrofit2.Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}

3. Call Direction Api this will return Direction Response.

private void callDirectionApi(Call<DirectionResponse> responseCall) {
responseCall.enqueue(new Callback<DirectionResponse>() {
@Override
public void onResponse(Call<DirectionResponse> call, Response<DirectionResponse> response) {
if (response.isSuccessful()) {
showDirection(response.body());
}
}

@Override
public void onFailure(Call<DirectionResponse> call, Throwable t) {
Toast.makeText(getApplicationContext(), "Api Failure", Toast.LENGTH_SHORT).show();
}
});
}

4. Using Direction Response draw polyline.

private void showDirection(DirectionResponse response) {
ArrayList<LatLng> pathList = new ArrayList<>();
if (response.getRoutes() != null) {
if (response.getRoutes().get(0).getPaths() != null) {
for (int i = 0; i < response.getRoutes().get(0).getPaths().size(); i++) {
if (response.getRoutes().get(0).getPaths().get(i).getSteps() != null) {
for (int j = 0; j < response.getRoutes().get(0).getPaths().get(i).getSteps().size(); j++) {
if (response.getRoutes().get(0).getPaths().get(i).getSteps().get(j).polyline != null) {
for (int k = 0; k < response.getRoutes().get(0).getPaths().get(i).getSteps().get(j).polyline.size(); k++) {
pathList.add(new LatLng(response.getRoutes().get(0).getPaths().get(i).getSteps().get(j).polyline.get(k).getLat(),
response.getRoutes().get(0).getPaths().get(i).getSteps().get(j).polyline.get(k).getLng()));
}
}
}
}
}
}
}
if (response.getRoutes() != null) {
curentPolyline = hMap.addPolyline(new PolylineOptions()
.addAll(pathList)
.width(4)
.color(getResources().getColor(R.color.colorAccent)));
}

}

Result:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200918105310.34349148332938560996220251715579:50510928022348:2800:44ED93C79CD83A3FE04A8A38DBBD704A9A078BB490788C6AC1EC7F95214D5C4E.gif"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200918105310.34349148332938560996220251715579:50510928022348:2800:44ED93C79CD83A3FE04A8A38DBBD704A9A078BB490788C6AC1EC7F95214D5C4E.gif[/IMG]

Conclusion

In this Article, I have explained how to integrate Location Kit on food application and draw route between origin to destination.

Reference:

Map kit: https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/android-sdk-introduction-0000001050158633

Location kit: https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides-V5/introduction-0000001050706106-V5