Android Google Maps v2: how to add a marker with a multi-line fragment?

Does anyone know how to add multi-line snippet to Google Maps? What is my code for adding markers:

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

I want the fragment to look like this:

 | HEADER | |foo | |bar | 

but when I try to set snippetText to "foo \ n bar", I only see foo bar , and I have no idea how to make it multi-line. Can you help me?

+52
android google-maps
Dec 16 '12 at 19:16
source share
3 answers

It looks like you need to create your own โ€œinfo windowโ€ content to make this work:

  • Create an InfoWindowAdapter implementation that overrides getInfoContents() to return what you want to insert in the InfoWindow frame

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

This sample project demonstrates the technique. Replacing my "foo\nbar" fragments correctly handles a new line. However, most likely, you will simply come up with a layout that avoids the need for a new line, with separate TextView widgets for each line in your desired visual results.

+48
Dec 16 '12 at 19:34
source share

I did with the simplest way as shown below:

 private GoogleMap mMap; 

While adding a marker to 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, enter the InfoWindow adapter in the 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 this helps you.

+75
Jul 25 '15 at 17:38
source share

Based on the Hiren Patel answer , as suggested by Andrew S:

  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; } }); 
+12
Aug 6 '15 at 9:33
source share



All Articles