Prasath S
Prasath S

Reputation: 4416

Filter the places list which inside the map projection?

I have a places List Which contains Hospital object(List<Hospital>). Hospital objects contain Name, latitude, longitude, and mobile number as an attribute. If the user adjusts the screen I get the current map projection LatLngBounds from that I got covered area. So Now I want to filter the Hospitals Which inside the that LatLngBounds, How can I implement getAvailableHospital() method? Thank you!

Hospital Object Class

Class Hospital
{
    private String Name;
    private String Latitude;
    private String Longtitude;
    private String PhoneNumber
}

Inside the Map Fragment

@Override
    public void onCameraIdle() {
        LatLngBounds bounds = mMap.getProjection().getVisibleRegion().latLngBounds;
        getAvailabelHospitals(bounds.northeast.latitude, bounds.northeast.longitude, bounds.southwest.longitude, bounds.southwest.latitude);
    }

Upvotes: 0

Views: 69

Answers (2)

Prasath S
Prasath S

Reputation: 4416

List<fshop> Rlist;

Rlist=new ArrayList<>();
        for(fshop f : mlist)
        {
            LatLng point = new LatLng (Double.parseDouble(f.getLat()), Double.parseDouble(f.getLng()));
            if(bounds.contains(point))
            {
                Rlist.add(f);
            }
        }

Upvotes: 0

kielni
kielni

Reputation: 4999

LatLngBounds has a contains method that takes a LatLng and returns true if the LatLng is within the bounds.

To generate a list of hospitals within the bounds, check whether each item in your list is contained within the bounds. For example:

List<Hospital> inBounds = hospitals
      .stream()
      .filter(h -> bounds.contains(new LatLng(h.latitude(), h.longitude()))
      .collect(Collectors.toList());

Upvotes: 2

Related Questions