5

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?

Akshit Rewari
  • 941
  • 2
  • 13
  • 26
  • 1
    you can check these https://stackoverflow.com/questions/17989389/label-at-the-top-of-the-marker-in-google-maps-api-v2 –  Aug 11 '17 at 07:08
  • @NavjotSingh: I have seen these.The title property shows only when user clicks on the marker – Akshit Rewari Aug 11 '17 at 08:30

1 Answers1

1

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();
Ajil O.
  • 6,562
  • 5
  • 40
  • 72
  • Thanks for your help. But actually I have mentioned in the question that I am already using the infoWindow to show a customised and detailed View about the marker object. I need to show one line label for all markers on the map which should be visible without clicking on the markers – Akshit Rewari Aug 11 '17 at 09:50
  • Thats what the `sourceMarker.showInfoWindow();` bit is supposed to do – Ajil O. Aug 11 '17 at 09:51
  • But my requirement is slightly different. There has to be a label visible for all markers and clicking on a marker should show the detailed infoWindow with details of that marker – Akshit Rewari Aug 11 '17 at 09:55
  • Hi @AkshitRewari did you found solution. If you found solution then could you share it? – Water Flower Sep 16 '20 at 02:25