Android Google Maps v2: How to add marker with multiline snippet?
Solution 1:
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.
Solution 2:
It looks like you will need to create your own "info window" contents to make that work:
Create an implementation of
InfoWindowAdapter
that overridesgetInfoContents()
to return what you want to go into theInfoWindow
frameCall
setInfoWindowAdapter()
on yourGoogleMap
, passing an instance of yourInfoWindowAdapter
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.
Solution 3:
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;
}
});