我正在尝试从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。
答案 0 :(得分:15)
看起来你走的是正确的道路,但你的默认分钟和最大值会造成一些麻烦。试试以下内容:
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);
}
答案 1 :(得分:0)
这里@Brad Leege's answer进行了一些小的改进,已移植到Kotlin:
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)
}
答案 2 :(得分:0)
科特林溶液。对@Pulkit Goyal的改进
private fun zoomToMultipleGeoCoordinates(boundingList: List<GeoCoordinates>, isPaddingRequired: Boolean) {
var north = Double.MIN_VALUE
var south = Double.MAX_VALUE
var east = Double.MIN_VALUE
var west = Double.MAX_VALUE
boundingList.forEach { loc ->
north = loc.latitude.coerceAtLeast(north)
south = loc.latitude.coerceAtMost(south)
west = loc.longitude.coerceAtMost(west)
east = loc.longitude.coerceAtLeast(east)
}
if (isPaddingRequired) {
val padding = 0.01
north += padding
south -= padding
west -= padding
east += padding
}
return BoundingBox.fromLngLats(west, south, east, north)
}