在Google地图上相对位置上添加两个标记。

huangapple go评论75阅读模式
英文:

Add two markers at relative position to each other on Google map

问题

我有一个车辆标记和一个剩余距离标记,需要显示在Google地图上。我希望在车辆位置更新时更新这两个标记。
每当新的车辆位置可用时,我会在车辆位置添加剩余距离标记。
我希望始终将剩余距离标记显示在相对于车辆标记的位置。
请参考下面的图像。

英文:

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.

在Google地图上相对位置上添加两个标记。

答案1

得分: 1

从Google文档中:

要在汽车位置上方显示距离信息,只需使用以下代码:

final LatLng carLatLng = new LatLng(-37.81319, 144.96298); /* 汽车位置 */
String displayDistance = distance + "km"; /* 计算距离并存储在 "distance" 中 */
Marker carIcon = map.addMarker(
    new MarkerOptions()
        .position(carLatLng)
        .icon(BitmapDescriptorFactory.fromResource(R.drawable.carIcon))) /* <-- 汽车图标 */
        .title(displayDistance));
        /*.snippet("subTitle"));*/
carIcon.showInfoWindow();

>**注意:**绘制的信息窗口不是实时视图。该视图在返回时被渲染为图像(使用 View.draw(Canvas)),这意味着对视图的任何后续更改都不会反映在地图上的信息窗口中。要稍后更新信息窗口(例如,在图像加载后),请调用 showInfoWindow()。此外,信息窗口将不会遵循正常视图的触摸或手势事件等交互性。但是,您可以监听整个信息窗口上的通用点击事件,如下面的部分所述。

来源:https://developers.google.com/maps/documentation/android-sdk/infowindows
https://developers.google.com/maps/documentation/android-sdk/marker

英文:

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 +&quot;km&quot;; /* calculate distance and store it in &quot;distance&quot; */
Marker carIcon = map.addMarker(
    new MarkerOptions()
        .position(carLatLng )
        .icon(BitmapDescriptorFactory.fromResource(R.drawable.carIcon))) /*&lt;-- car icon */
        .title(displayDistance));
        /*.snippet(&quot;subTitle&quot;));*/
carIcon.showInfoWindow();

在Google地图上相对位置上添加两个标记。

>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), call showInfoWindow(). 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

huangapple
  • 本文由 发表于 2023年8月4日 01:48:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76830486.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定