Check LatLngBounds.Builder if null
Asked Answered
M

1

5

This is mycode:

LatLngBounds.Builder builder = new LatLngBounds.Builder();
for(int x = firstVisibleItem; x < lastVisibleItem; x++){
    builder.include(temp.getPosition());
}

However this line throws an error (java.lang.IllegalStateException: no included points :

double north = builder.build().northeast.latitude;

This is because the above loop didn't run at all so no points were included in the builder.

How can I check if the builder has at least one point?

I tried builder.build()!=null which throws the above error and builder!=null which is always True.

try{}catch(IllegalStateException e){ } works. Is it stupid to ask a !=null way? Mico-management? Thanks

Mcripley answered 10/2, 2014 at 1:12 Comment(0)
D
10

You can create a counter and use it to verify that you have at least one point.

int counter = 0;
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for(int x = firstVisibleItem; x < lastVisibleItem; x++){
    counter++;
    builder.include(temp.getPosition());
}
if (counter > 0) {
    //use a LatLngBounds.Builder to build the LatLngBounds object
}
Duality answered 10/2, 2014 at 3:45 Comment(2)
Good point, thanks. Event simpler: if(firstVisibleItem< lastVisibleItem)Mcripley
Glad to be helpful... and yes, you are right... this could be done too.Duality

© 2022 - 2024 — McMap. All rights reserved.