Java – If empty, check LatLngBounds.Builder

If empty, check LatLngBounds.Builder… here is a solution to the problem.

If empty, check LatLngBounds.Builder

Here is my code :

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

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

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

This is because the loop above is not running at all, so no points are included in the builder.

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

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

try{}catch(IllegalStateException e){ } valid。 Isn’t it stupid to ask a !=null approach? Micromanagement? Thanks

Solution

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
}

Related Problems and Solutions