Android Google Maps v2: How to add marker with multiline snippet?

Related searches

Does anybody know how to add multiline snippet to Google Maps marker? That's my code for adding markers:

map.getMap().addMarker(new MarkerOptions()
    .position(latLng()).snippet(snippetText)
    .title(header).icon(icon));

I want snippet to look like this:

| HEADER |
|foo     |
|bar     |

but when I'm trying to set snippetText to "foo \n bar", I see just foo bar and I don't have any ideas how to make it multiline. Can you help me?

It looks like you will need to create your own "info window" contents to make that work:

  1. Create an implementation of InfoWindowAdapter that overrides getInfoContents() to return what you want to go into the InfoWindow frame

  2. Call setInfoWindowAdapter() on your GoogleMap, passing an instance of your InfoWindowAdapter

This sample project demonstrates the technique. Replacing my snippets with "foo\nbar" correctly processes the newline. However, more likely, you will just come up with a layout that avoids the need for the newline, with separate TextView widgets for each line in your desired visual results.

Markers | Maps SDK for Android, Does anybody know how to add multiline snippet to Google Maps marker? That's my code for adding markers: map.getMap().addMarker(new MarkerOptions()� Does anybody know how to add multiline snippet to Google Maps marker? That's my code for adding markers: map.getMap().addMarker(new MarkerOptions() .position(latLng()).snippet(snippetText) .title(header).icon(icon)); I want snippet to look like this: | HEADER | |foo | |bar | but when I'm try

I have done with easiest way like below:

private GoogleMap mMap;

While adding marker on Google Map:

LatLng mLatLng = new LatLng(YourLatitude, YourLongitude);

mMap.addMarker(new MarkerOptions().position(mLatLng).title("My Title").snippet("My Snippet"+"\n"+"1st Line Text"+"\n"+"2nd Line Text"+"\n"+"3rd Line Text").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));

After that put below code for InfoWindow adapter on Google Map:

mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {

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

      @Override
      public View getInfoContents(Marker marker) {

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

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

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

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

      return info;
    }
});

Hope it will help you.

Info Windows | Maps SDK for Android, Android Google Maps v2: How to add marker with multiline snippet? @ CommonsWare I'm reading the Maps V2 Chapter from your 4.6 book and I have a � Questions: Does anybody know how to add multiline snippet to Google Maps marker? That’s my code for adding markers: map.getMap().addMarker(new MarkerOptions() .position(latLng()).snippet(snippetText) .title(header).icon(icon)); I want snippet to look like this: | HEADER | |foo | |bar | but when I’m trying to set snippetText to “foo bar”, I see just foo bar and I

Building on Hiren Patel's answer as Andrew S suggested:

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

Android Question, The default marker uses a standard icon, common to the Google Maps look and feel. The following example demonstrates how to add a marker to a map. I'm using google maps apiv2 and I'v added a marker on the map. The marker has a long snippet, so I want to insert some carriage return into the string. I've tried , \r and System.getProperty("line.

Based on Hiren Patel solution. This code creates a TextView from layout, not from zero. One significant difference: if you have clusters, you won't see a void label when clicking at a cluster.

override fun onMapReady(googleMap: GoogleMap) {
    this.googleMap = googleMap
    ...

    // Use this anonymous class or implement GoogleMap.InfoWindowAdapter.
    googleMap.setInfoWindowAdapter(object : GoogleMap.InfoWindowAdapter {
        override fun getInfoContents(marker: Marker): View? {
            return null
        }

        override fun getInfoWindow(marker: Marker): View? =
            if (marker.title == null)
                null
            else {
                val inflater = LayoutInflater.from(context)
                val view = inflater.inflate(R.layout.layout_marker, null, false)
                view.label.text = marker.title
    
                view
            }
    })

layout_marker.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="4dp"
    android:paddingBottom="4dp"
    android:textColor="$f0f0f0"
    android:textSize="12sp"
    tools:text="Marker"
    android:background="#00aaee"
    />

The simplest way to add an info window is to set the title() and snippet() The API will first call getInfoWindow(Marker) and if null is returned, it will then call� How can we add an object to a marker in the new Google Maps Android API v2?So if we click on the InfoWindow, we can do something with the object?. public void addSpotOnMap(Spot spot) { getMap().addMarker(new MarkerOptions() .position(new LatLng(spot.getParseGeoPoint().getLatitude(), spot.getParseGeoPoint().getLongitude())) .title(spot.getName()) .snippet(spot.getCategory()) .draggable(false)); }

Same code, but in Kotlin:

    mMap?.setInfoWindowAdapter(object : InfoWindowAdapter {
        override fun getInfoWindow(arg0: Marker): View? {
            return null
        }
        
        override fun getInfoContents(marker: Marker): View {
            val info = LinearLayout(applicationContext)
            info.orientation = LinearLayout.VERTICAL
            val title = TextView(applicationContext)
            title.setTextColor(Color.BLACK)
            title.gravity = Gravity.CENTER
            title.setTypeface(null, Typeface.BOLD)
            title.text = marker.title
            val snippet = TextView(applicationContext)
            snippet.setTextColor(Color.GRAY)
            snippet.text = marker.snippet
            info.addView(title)
            info.addView(snippet)
            return info
        }
    })

B4A Tutorial (old) Google Maps Android v2 tutorial http://stackoverflow.com/ questions/s-v2-how-to-add-marker-with-multiline-snippet Is it possible to add an extra attribute to a marker object? No. Marker is final. Also, the Marker objects you create vanish quickly, as they are only used for some IPC over to the Google Play Services app. The Marker object you get in your OnInfoWindowClickListener appears to be a reconstituted copy.

I'm developing in google maps APIv2. The issue that I'm facing now is I only able to add in one line of word in info windows/snippet. But the output that I want is able to display in break line for

You must explicitly set the marker to be draggable either with MarkerOptions.draggable(boolean) prior to adding it to the map, or Marker.setDraggable(boolean) once it has been added to the map. You can listen for drag events on the marker, as described in Marker drag events. The below snippet adds a draggable marker at Perth, Australia.

How to I make the markers in Android Google Maps API v2 become clickable so they will either bring up a menu with options or just start a new activity? I believe I made the markers in my app currently in a "newb" method. I didn't assign them a name or a method to be able to link it in with the rest of the required code.

Comments
  • If you do use getInfoWindow() to return an entirely customized view, note that there seems to be a bug when using a RelativeLayout for it. I could only get mine to work when switching to a LinearLayout. I filed a bug report: code.google.com/p/gmaps-api-issues/issues/detail?id=4874
  • @Till: Normally, when you use a layout resource with a RelativeLayout as the root, when you inflate it, you want to use the three-parameter inflate() method, passing in the parent ViewGroup and false for the last two parameters. However, we are not passed a parent in getInfoContents(), so we cannot do that. While I am a bit surprised about a NullPointerException, I am not surprised at all that the RelativeLayout does not work correctly. I would have expected some of the layout rules for the RelativeLayout's children to be ignored or otherwise misbehave.
  • @CommonsWare I'm reading the Maps V2 Chapter from your 4.6 book and I have a question that is on topic with this question so I think is a good place to ask. I wander if is there any way to set a distinct infoWindow for each marker, as long as setInfoWindowAdapter() is applied to hole map?
  • I've got the code from the sample project working. so I have a custom infowindow but I am stuck on how to add multiple lines still. I've added in extra textviews after title and snippet in the xml layout but how do I get text to appear in the new textviews?
  • @cbrook: Call setText() on the TextViews, no different than TextViews in an activity, fragment, ListView row, etc.
  • This solution is actually working. +1 for adding a code sample.
  • Thanks! +1 for snippet.
  • Working great, title and multiple snippet lines! +1
  • This should be accepted answer because of Code Snippet. Saved my time. even I like mMap variable name because that also I haven't changed.
  • Warning: I replace "MapsActivity.this" instead of mContext to run with no error.