Find a restricted bin from the Geopoints Mapbox
I'm trying to find a bounding box from the GeoPoints collection, but it doesn't scale properly. I am pasting the function I used to find the bounding box below
private BoundingBox createBoundingBox(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 BoundingBox(minLatitude, minLongitiude, maxLatitude - minLatitude,
maxLongitude - minLongitiude);
}
Can someone tell me what I am doing wrong here. The map zoom level is still 0.
+3
source to share
2 answers
It looks like you are on the right track, but your default minimum and maximum values ββare causing certain problems. Try the following:
public BoundingBox findBoundingBoxForGivenLocations(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 BoundingBox(north, east, south, west);
}
+15
source to share
Here is @ Brad Leege 's answer for ported to Kotlin with minor improvements:
fun findEnclosingBoundingBox(coordinates: ArrayList<LatLng>): BoundingBox {
var west = GeometryConstants.MAX_LONGITUDE
var east = GeometryConstants.MIN_LONGITUDE
var north = GeometryConstants.MIN_LATITUDE
var south = GeometryConstants.MAX_LATITUDE
coordinates.forEach { loc ->
north = Math.max(loc.latitude, north)
south = Math.min(loc.latitude, south)
west = Math.min(loc.longitude, west)
east = Math.min(loc.longitude, east)
}
// Add some extra "padding"
val padding = 0.01
north += padding
south -= padding
west -= padding
east += padding
return BoundingBox.fromLngLats(west, south, east, north)
}
0
source to share