Akshit Rewari
Akshit Rewari

Reputation: 941

How to show a label next to a marker on Google maps on Android?

I am working on an Android app which uses Google maps Android API.I am showing multiple markers on the map and I want to show a label next to each marker as shown in the image below but I'm unable to find out how to do it.

Image with google map marker and label

I have gone through the Google maps documentation at https://developers.google.com/maps/documentation/android-api/map-with-marker but I am unable to find details on how to show the label in this manner.I have tried the title property which opens up a popup but I need that to show a detailed view(InfoWindow) about my markers and I need to show the main title without opening the popup for any marker

Any idea how I can add a marker in the manner shown in attached image?

Upvotes: 5

Views: 3647

Answers (1)

Ajil O.
Ajil O.

Reputation: 6892

Not sure if this is what you had in mind, but here goes

Create the MarkerOptions object:

MarkerOptions options = new MarkerOptions()
                .title(name)
                .position(source)
                .icon(gettIconFromDrawable(myMarker))
                .snippet("Briefly describe \nyour content here");

Then add the marker to the map

Marker sourceMarker = mMap.addMarker(options);

Create InfoWindowAdapter to accommodate multiple lines in the snippet. (Original answer here)

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;
            }
        });

Finally, add this property to ensure the marker stays visible

sourceMarker.showInfoWindow();

Upvotes: 1

Related Questions