Java – find the bounding box from a set of geopoints mapboxes

I'm trying to find the bounding box from the geopoints collection, but it doesn't scale correctly. I'm pasting the function I use to find the bounding box below

private Bounding@R_133_2419@ createBounding@R_133_2419@(final ArrayList<LatLng> list){
        double minLatitude = 90, minLongitiude = 180, maxLatitude = -90, maxLongitude = -180;
        double currentLat, currentLng;
        for(LatLng location : list){
            currentLat    = location.getLatitude();
            currentLng    = location.getLongitude();
            minLatitude   = Math.max(minLatitude, currentLat);
            minLongitiude = Math.max(minLongitiude, currentLng);
            maxLatitude   = Math.min(maxLatitude, currentLat);
            maxLongitude  = Math.min(maxLongitude, currentLng);
        }
       return new Bounding@R_133_2419@(minLatitude, minLongitiude, maxLatitude - minLatitude,
               maxLongitude - minLongitiude);
}

Anyone can tell me what I did wrong here. The zoom level of the map is still 0

resolvent:

It looks like you're on the right path, but your default minutes and maximum cause some trouble. Try the following:

public Bounding@R_133_2419@ findBounding@R_133_2419@ForGivenLocations(ArrayList<LatLng> coordinates)
{
    double west = 0.0;
    double east = 0.0;
    double north = 0.0;
    double south = 0.0;

    for (int lc = 0; lc < coordinates.size(); lc++)
    {
        LatLng loc = coordinates.get(lc);
        if (lc == 0)
        {
            north = loc.getLatitude();
            south = loc.getLatitude();
            west = loc.getLongitude();
            east = loc.getLongitude();
        }
        else
        {
            if (loc.getLatitude() > north)
            {
                north = loc.getLatitude();
            }
            else if (loc.getLatitude() < south)
            {
                south = loc.getLatitude();
            }
            if (loc.getLongitude() < west)
            {
                west = loc.getLongitude();
            }
            else if (loc.getLongitude() > east)
            {
                east = loc.getLongitude();
            }
        }
    }

    // OPTIONAL - Add some extra "padding" for better map display
    double padding = 0.01;
    north = north + padding;
    south = south - padding;
    west = west - padding;
    east = east + padding;

    return new Bounding@R_133_2419@(north, east, south, west);
}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>