Skip to content

Instantly share code, notes, and snippets.

@DevPicon
Created May 20, 2021 07:23
Show Gist options
  • Save DevPicon/494b6f3643da584bce6658a8cf1a47be to your computer and use it in GitHub Desktop.
Save DevPicon/494b6f3643da584bce6658a8cf1a47be to your computer and use it in GitHub Desktop.
fun addDestinationOnMap(
googleMap: GoogleMap,
shopperLocation: Location,
destinationLocation: LatLng
) {
lifecycle.coroutineScope.launchWhenCreated {
googleMap.clear()
val latLngBounds = LatLngBounds.Builder()
.include(LatLng(shopperLocation.latitude, shopperLocation.longitude))
.build()
val originCameraPosition = buildCameraPosition(latLngBounds)
val originCameraUpdate = CameraUpdateFactory.newCameraPosition(originCameraPosition)
val destinationLatLngBounds = LatLngBounds.Builder()
.include(LatLng(destinationLocation.latitude, destinationLocation.longitude))
.build()
val destinationCameraPosition = buildCameraPosition(destinationLatLngBounds)
val destinationCameraUpdate =
CameraUpdateFactory.newCameraPosition(destinationCameraPosition)
googleMap.moveCamera(originCameraUpdate)
googleMap.awaitAnimateCamera(destinationCameraUpdate, durationMs = 1500)
delay(500)
googleMap.addMarker {
position(destinationLocation)
title("Marker in destination")
}
googleMap.stopAnimation()
}
}
// KTX for the Maps SDK for Android library
implementation 'com.google.maps.android:maps-ktx:3.0.1'
// KTX for the Maps SDK for Android Utility Library
implementation 'com.google.maps.android:maps-utils-ktx:3.0.1'
implementation "com.google.android.gms:play-services-maps:$google_gms_maps_version"
implementation 'com.google.maps.android:android-maps-utils:2.0.3'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment