Call toString()
on a StringBuilder
to get a String
, and pass the String
to snippet()
.
How to add a StringBuilder object in snippet in MarkerOptions in android?
-
29-06-2023 - |
Question
I am trying to add address from Geocoder class to be displayed in the snippet of marker. But the snippet does not accept StringBuilder object. Can anyone tell me how to do it?
Following is my code
StringBuilder str;
try
{
geocoder = new Geocoder(HistoryLocation.this, Locale.ENGLISH);
List<Address> addresses = geocoder.getFromLocation(latitudeDouble, longitudeDouble, 1);
str = new StringBuilder();
if(geocoder.isPresent())
{
Toast.makeText(getApplicationContext(), "geocoder present", Toast.LENGTH_SHORT).show();
Address returnaddress = addresses.get(0);
String locality = returnaddress.getLocality();
String city = returnaddress.getCountryName();
String countrycode = returnaddress.getSubLocality();
String zip = returnaddress.getPostalCode();
str.append(locality + "");
str.append(city + "" +countrycode);
str.append(zip + "");
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(),
"geocoder not present", Toast.LENGTH_SHORT).show();
}
}
catch(IOException e)
{
Log.e("tag", e.getMessage());
}
String dateAndTime = date + "-" + time;
MarkerOptions mOptions = new MarkerOptions();
LatLng latlng = new LatLng(latitudeDouble, longitudeDouble);
mOptions.position(latlng);
mOptions.title(dateAndTime);
mOptions.snippet(str);;
userMarker = googleMap.addMarker(mOptions);
userMarker.setVisible(true);
Solution
OTHER TIPS
The MarkerOptions.snippet()
method takes a String parameter. Change:
mOptions.snippet(str);;
to:
mOptions.snippet(str.toString());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow