如何在android中的MarkerOptions中的片段中添加StringBuilder对象?

时间:2014-04-08 11:42:47

标签: android google-maps google-maps-markers stringbuilder

我正在尝试从Geocoder类添加地址以显示在标记的片段中。但该代码段不接受StringBuilder对象。谁能告诉我怎么做?

以下是我的代码

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

2 个答案:

答案 0 :(得分:1)

致电toString()上的StringBuilder获取String,然后将String传递给snippet()

答案 1 :(得分:1)

MarkerOptions.snippet()方法采用String参数。变化:

mOptions.snippet(str);;

为:

mOptions.snippet(str.toString());