Pregunta

I have an app that reverse geocodes latitude & longitude into readable addresses. Here is a sample of the code below:

    protected List<Address> doInBackground(Double... params) {
            try {

                List<Address> addresses = geoCoder.getFromLocation(mylat,
                        mylong, 1);
                StringBuilder sBuilder = new StringBuilder();
                if (addresses.size() > 0) {
                    Address address = addresses.get(0);

                    for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
                        /**
                         * Returns a line of the address numbered by the
                         * given index (starting at 0), or null if no such
                         * line is present.
                         */
                        sBuilder.append(address.getAddressLine(i)).append(
                                "\n");
                        ////////////
                        ////////////
                        // sBuilder.append(address.getLocality()).append("\n");
                        //sBuilder.append(address.getPostalCode()).append("\n");        
                    }// end for
                    addressString = sBuilder.toString();

                }// end if
                return geoCoder.getFromLocation(params[0], params[1], 1);
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }// end catch
        }// end doInBackground method

I'd like to modify it so if the PostalCode is null, then print nothing, if it receives a value, display it wit hthe rest of the other details. The issue is I live in a country that do NOT use postal codes, which is why that part of the code is currently commented out. I'd like the app to display postal codes if you're in a country that does use them (most do!) and ignore absence of one if you're in my country or a similar one that do not use postal codes.

Can anyone recommend what change I need to make to allow this? I believe I need to add logic to deal with the appending of string here:

sBuilder.append(address.getPostalCode()).append("\n");

Thanks. :)

¿Fue útil?

Solución

I think you should just add one condition over there.

if (address.getPostalCode() != null)
{
    sBuilder.append(address.getPostalCode()).append("\n");
}
else
{
    // do nothing.
}

Or is it not the thing you want?

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top