I have a vehicle marker and a remaining distance marker, which need to be displayed on Google Maps. I want to update these two markers as the vehicle location updates. I am adding the remaining distance marker at the vehicle location whenever the new vehicle location is available. I want to display the remaining distance marker always at the relative position to the vehicle marker. Please refer to the below image.
-
Can you please provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example)? That would help alot. :'> – Yrll Aug 06 '23 at 23:51
-
@Yrll I don't have anything to reproduce here. I just want to position the distance banner always in relation with the vehicle marker. – hasan_shaikh Aug 07 '23 at 04:46
-
Thanks for clarifying. But what language are you using here? Is it Kotlin or Java? or what have you tried so far? I'm personally interested in this. So I hope you don't mind. :> – Yrll Aug 07 '23 at 05:20
-
how does java or kotlin matter here? – hasan_shaikh Aug 07 '23 at 05:30
-
@hasan_shaikh: something like [this](https://stackoverflow.com/a/62348458/6576302)? – C.F.G Aug 07 '23 at 06:46
-
@C.F.G I think what OP wanted is for the info window to be together with the vehicle marker instead of the polyline in which what the post you linked is about. – Yrll Aug 07 '23 at 08:33
1 Answers
From Google docs:
For showing distance info above car location just use
final LatLng carLatLng = new LatLng(-37.81319, 144.96298); /* car location */
String displayDistance = distance +"km"; /* calculate distance and store it in "distance" */
Marker carIcon = map.addMarker(
new MarkerOptions()
.position(carLatLng )
.icon(BitmapDescriptorFactory.fromResource(R.drawable.carIcon))) /*<-- car icon */
.title(displayDistance));
/*.snippet("subTitle"));*/
carIcon.showInfoWindow();
Note: The info window that is drawn is not a live view. The view is rendered as an image (using
View.draw(Canvas)
) at the time it is returned. This means that any subsequent changes to the view will not be reflected by the info window on the map. To update the info window later (for example, after an image has loaded), callshowInfoWindow()
. Furthermore, the info window will not respect any of the interactivity typical for a normal view such as touch or gesture events. However you can listen to a generic click event on the whole info window as described in the section below.
Source: https://developers.google.com/maps/documentation/android-sdk/infowindows and https://developers.google.com/maps/documentation/android-sdk/marker

- 817
- 8
- 16