For some reason the implementation of getMaxAddressLineIndex has recently changed. Now this method returns 0 for line 1.
I have an existing code, which used to work: i<address.getMaxAddressLineIndex(
). However, it is broken somehow.
I don't know if it is due to the new Google Api or something else.
Can someone please confirm me here what is going on?
I think
getAddressLine()
has changed. It used to return the various elements of the address in separate calls to...getAddressLine(0)
,...getAddressLine(1)
and so on up togetAddressLine(n)
wheren
is...getMaxAddressLineIndex()
.Now it seems to return the entire address concatenated and comma-separated into the call to
...getAddressLine(0)
, and...getMaxAddressLineIndex()
always returns zero (if an address is returned, or -1 if no address returned).For example, in the old version, the
lat/long
of the Houses of Parliament in London would return (for the first address returned) 4 address lines:Now it returns one line:
That seems to be what happens to me. I've tried it on a Moto G5 Plus with Android 7 and a Samsung tablet with Android 6. My Android emulators still work the old way. Someone tell me if I've got that wrong!
Note: in the past you could test
i < address.getMaxAddressLineIndex()
rather than<=
. This just meant you didn't get the last element, which always seemed to be the abreviated country name (e.g. "USA"). Now that.getMaxAddressLineIndex()
always seems to return zero, that won't work. But note you get "USA" appended to the string returned to.getAddressLine(0)
.The same happened to me. I verified that Developer Reference (https://developer.android.com/reference/android/location/Address.html#getMaxAddressLineIndex()) now states:
int getMaxAddressLineIndex ()
Returns the largest index currently in use to specify an address line. If no address lines are specified, -1 is returned.
So it seems to reflect the new behaviour.
Anyway, to be safe, I'm going to enclose
getAddressLine(i)
insidetry\catch
I had the same issue and this just a workaround.
Hope this help
As user8533943 has pointed out above the implementation has been changed surreptitiously. However I am of the opinion that
has changed it's implementation. It returns a list of addresses and each address consists of address lines. But as far as I could see when I compiled with version 26 of the SDK there aren't "address lines" being returned but just one line for every address.
So if you have just one address then
would in fact return 0
Update your implementation as follows to use a <= comparison.