[Huawei Developers] Sherlock F:
More information like this, you can visit HUAWEI Developer Forum​

Introduction

Flight booking app allows user to search and book flight. In this article, we will integrate app messaging and analytics into demo flight booking application.

For prerequisite and set up, refer to part 1.

Usecase

We will show the marker and dotted polyline between source and destination airport using Huawei Map kit.

Huawei Map Kit

1. Enable Map kit in AGC. Refer Service Enabling.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200925072222.29083937885453685855607327801119:50511013022141:2800:AA1126F345F193D9B612F1EA0130298EEB87FE0C70EA9CD6EBBF7A7C7AFE7D81.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200925072222.29083937885453685855607327801119:50511013022141:2800:AA1126F345F193D9B612F1EA0130298EEB87FE0C70EA9CD6EBBF7A7C7AFE7D81.png[/IMG]

2. In order to use Huawei map kit, we need to integrate following code in app-level build.gradle.

implementation 'com.huawei.hms:maps:5.0.1.300'

3. Add following permissions in Manifest.

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="com.huawei.appmarket.service.commondata.permission.GET_COMMON_DATA"/>

//To obtain current device location
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

4. To provide real time permission.

private void requestPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Log.i(TAG, "sdk >= 23 M");
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(this, strings, 1);
}
}
}

5. Add following code to in your xml file to set the map attributes.

<com.huawei.hms.maps.MapView
android:id="@+id/mapview_huawei"
android:layout_width="match_parent"
android:layout_height="200dp" />

6. To use map in your app, you need to implement OnMapReadyCallback.

public class PolylineholderFragment extends Fragment implements OnMapReadyCallback {

@Override
public void onMapReady(HuaweiMap huaweiMap) {

}
}

7. Load MapView in the onCreate() or onCreateView() method and call getMapAsync() to register the callback.

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flight_details);
mMapView = findViewById(R.id.mapview_huawei);
Bundle mapViewBundle = null;
if (savedInstanceState != null) {
mapViewBundle = savedInstanceState.getBundle("MapViewBundleKey");
}
mMapView.onCreate(mapViewBundle);
mMapView.getMapAsync(this);

}

8. To draw marker at source and destination airport.

LatLng origin = new LatLng(HelperUtilities.originAirport.getLatitude(), HelperUtilities.originAirport.getLongitude());

LatLng destination = new LatLng(HelperUtilities.destinationAirport.getLatitude(), HelperUtilities.destinationAirport.getLongitude());

huaweiMap.addMarker(new MarkerOptions().position(origin).icon(BitmapDescriptorFactory.fromResource(R.drawable.htl_location_accent)));

huaweiMap.addMarker(new MarkerOptions().position(destination).icon(BitmapDescriptorFactory.fromResource(R.drawable.htl_location_accent)));

9. To specify the camera position of specific location.

CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLng(origin);
huaweiMap.moveCamera(cameraUpdate);

10. To draw dotted polyline between origin and destination airport, we need to create a list of PatternItem and call method addPoyline() and pass the source and destination LatLng object.

int PATTERN_GAP_LENGTH_PX = 2;

final PatternItem DOT = new Dot();

PatternItem GAP = new Gap(PATTERN_GAP_LENGTH_PX);

int PATTERN_DASH_LENGTH_PX = 2;

PatternItem DASH = new Dash(PATTERN_DASH_LENGTH_PX);

List<PatternItem> PATTERN_POLYGON_BETA =
Arrays.asList(DOT, GAP, DASH, GAP);

mPolyline = huaweiMap.addPolyline(new PolylineOptions()
.add(origin, destination)
.color(Color.RED).pattern(PATTERN_POLYGON_BETA)
.width(2));

11. OnMapReady() imlementation looks like this.

int PATTERN_GAP_LENGTH_PX = 2;
int PATTERN_DASH_LENGTH_PX = 2;

@Override
public void onMapReady(HuaweiMap huaweiMap) {
if (null == huaweiMap) {
return;
}
if (null != mPolyline) {
mPolyline.remove();
mPolyline = null;
}

final PatternItem DOT = new Dot();
PatternItem GAP = new Gap(PATTERN_GAP_LENGTH_PX);
PatternItem DASH = new Dash(PATTERN_DASH_LENGTH_PX);

List<PatternItem> PATTERN_POLYGON_BETA =
Arrays.asList(DOT, GAP, DASH, GAP);

LatLng origin = new LatLng(HelperUtilities.originAirport.getLatitude(), HelperUtilities.originAirport.getLongitude());
LatLng destination = new LatLng(HelperUtilities.originAirport.getLatitude(), HelperUtilities.destinationAirport.getLongitude());

mPolyline = huaweiMap.addPolyline(new PolylineOptions()
.add(origin, destination)
.color(Color.RED).pattern(PATTERN_POLYGON_BETA)
.width(2));

CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLng(origin);
huaweiMap.moveCamera(cameraUpdate);

huaweiMap.addMarker(new MarkerOptions().position(origin).icon(BitmapDescriptorFactory.fromResource(R.drawable.htl_location_accent)));
huaweiMap.addMarker(new MarkerOptions().position(destination).icon(BitmapDescriptorFactory.fromResource(R.drawable.htl_location_accent)));

}

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200925073906.64771593482727992635888033145539:50511013064408:2800:285DBA9BA18ECB20B3626CAFF6E4850AE32231617B724D31940608344A44C8A9.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200925073906.64771593482727992635888033145539:50511013064408:2800:285DBA9BA18ECB20B3626CAFF6E4850AE32231617B724D31940608344A44C8A9.jpg[/IMG]

Conclusion

In this article, we have shown how Huawei Map kit can be used to draw dotted polyline between origin and destination airport in flight detail screen.

References

For detailed guide, refer to this link.