我正在尝试从GeoPoints集合中找到边界框,但缩放比例未正确。我要粘贴用于查找下面边框的函数
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);
}
有人可以告诉我我在做什么错。地图缩放级别仍为0。
最佳答案
看起来您走在正确的道路上,但是您的默认最小值和最大值却在造成一些麻烦。尝试以下操作:
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);
}