如何在Android上的Google地图上显示标记旁边的标签?

5
我正在开发一个使用Google Maps Android API的Android应用程序。我在地图上显示多个标记,并希望像下面的图片中显示每个标记旁边的标签,但我不知道如何实现。
我已经查阅了Google Maps文档(https://developers.google.com/maps/documentation/android-api/map-with-marker),但是我无法找到如何以这种方式显示标签的详细信息。我尝试了title属性,但它只能打开弹出窗口,而我需要显示我的标记的详细视图(InfoWindow)并显示主要标题,而无需为任何标记打开弹出窗口。
您有什么想法可以按照所示的方式添加标记吗?

1
你可以查看这些 https://stackoverflow.com/questions/17989389/label-at-the-top-of-the-marker-in-google-maps-api-v2 - user5633496
@NavjotSingh:我看过这些。当用户点击标记时,标题属性才会显示。 - Akshit Rewari
1个回答

1
我不确定这是否符合您的想法,但是让我来试一下。
创建MarkerOptions对象:
MarkerOptions options = new MarkerOptions()
                .title(name)
                .position(source)
                .icon(gettIconFromDrawable(myMarker))
                .snippet("Briefly describe \nyour content here");

然后将标记添加到地图上。
Marker sourceMarker = mMap.addMarker(options);

创建 InfoWindowAdapter 以适应片段中的多行内容。(原始答案在此处)
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {

            @Override
            public View getInfoWindow(Marker arg0) {
                return null;
            }

            @Override
            public View getInfoContents(Marker marker) {

                Context context = getApplicationContext(); //or getActivity(), YourActivity.this, etc.

                LinearLayout info = new LinearLayout(context);
                info.setOrientation(LinearLayout.VERTICAL);

                TextView title = new TextView(context);
                title.setTextColor(Color.BLACK);
                title.setGravity(Gravity.CENTER);
                title.setTypeface(null, Typeface.BOLD);
                title.setText(marker.getTitle());

                TextView snippet = new TextView(context);
                snippet.setTextColor(Color.GRAY);
                snippet.setText(marker.getSnippet());

                info.addView(title);
                info.addView(snippet);

                return info;
            }
        });

最后,添加此属性以确保标记保持可见。
sourceMarker.showInfoWindow();

谢谢您的帮助。但实际上我在问题中已经提到,我已经使用infoWindow来显示有关标记对象的自定义和详细视图。我需要在地图上为所有标记显示一行标签,这些标签应该在不点击标记的情况下可见。 - Akshit Rewari
这就是 sourceMarker.showInfoWindow(); 的作用。 - Ajil O.
但是我的要求略有不同。所有标记都必须可见,点击标记后应该显示详细的信息窗口,其中包含该标记的详细信息。 - Akshit Rewari
嗨@AkshitRewari,你找到解决方案了吗?如果你找到了解决方案,能分享一下吗? - Water Flower

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接