【发布时间】:2020-06-15 07:38:37
【问题描述】:
我正在实现一个 Uber 克隆应用,并且我正在使用 Google Maps Api。
我想在地图上添加一些标记,但我不希望它有一个图标,只是一个显示预计到达时间的标签。知道如何实现吗?
我尝试将图标设置为 null,但是当我这样做时,我的标题也没有显示!
【问题讨论】:
标签: android google-maps google-maps-markers
我正在实现一个 Uber 克隆应用,并且我正在使用 Google Maps Api。
我想在地图上添加一些标记,但我不希望它有一个图标,只是一个显示预计到达时间的标签。知道如何实现吗?
我尝试将图标设置为 null,但是当我这样做时,我的标题也没有显示!
【问题讨论】:
标签: android google-maps google-maps-markers
你需要做的是把一个字符串变成一个图标,然后像图标一样显示,然后显示为标签。
所以要从文本创建一个 BitMapDescriptor(用于图标):
IconGenerator icg = new IconGenerator(this);
icg.setColor(Color.GREEN);
icg.setTextAppearance(R.style.TextAppearance_AppCompat_Small);
Bitmap bm = icg.makeIcon("ETA: 3m");
markerOpts.icon(BitmapDescriptorFactory.fromBitmap(bm));
// set position and other props on markerOpts and add
mMap.addMarker(markerOpts);
请注意,title 属性也可以根据需要添加(但不是必需的),并且可以按预期工作 - 单击图标并显示标题。
您还可以随时使用 Marker 对象更新图标(使用新的 ETA),并按上述方式设置图标。
这里讨论了其他方法(这就是上面的 sn-p 的来源):Map Markers with text in Google Maps Android API v2。
初始标记显示:
点击后带有标题:
【讨论】: