Java 从一组地质点映射框中查找边界框

Java 从一组地质点映射框中查找边界框,java,android,mapbox,Java,Android,Mapbox,我正在尝试从一组地质点中查找边界框,但它没有正确缩放。我正在粘贴用于查找下面边界框的函数 private BoundingBox createBoundingBox(final ArrayList<LatLng> list){ double minLatitude = 90, minLongitiude = 180, maxLatitude = -90, maxLongitude = -180; double currentLat, currentLn

我正在尝试从一组地质点中查找边界框,但它没有正确缩放。我正在粘贴用于查找下面边界框的函数

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);
}
private BoundingBox createBoundingBox(最终数组列表){
双最小纬度=90,最小经度=180,最大纬度=-90,最大经度=-180;
双电流LAT,电流LNG;
用于(车床位置:列表){
currentLat=location.getLatitude();
currentLng=location.getLongitude();
最小纬度=数学最大值(最小纬度,当前纬度);
minLongitiude=数学最大值(minLongitiude,currentLng);
maxLatitude=Math.min(maxLatitude,currentLat);
maxLongitude=Math.min(maxLongitude,currentLng);
}
返回新的边界框(最小纬度、最小经度、最大纬度-最小纬度、,
最大经度-最小经度);
}

有人能告诉我我做错了什么吗。地图缩放级别仍然为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);
}
公共边界框FindBoundingBoxFriendLocations(ArrayList坐标)
{
双西=0.0;
双东=0.0;
双北=0.0;
双南=0.0;
对于(int lc=0;lc北)
{
北=位置getLatitude();
}
else if(位置getLatitude()<南)
{
南=位置getLatitude();
}
if(loc.getLongitude()东方)
{
east=loc.getLongitude();
}
}
}
//可选-添加一些额外的“填充”,以更好地显示地图
双填充=0.01;
北=北+填充;
南=南-填充;
西=西-填充;
东=东+填充;
返回新的边界框(北、东、南、西);
}
以下是移植到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)
}
fun findEnclosingBoundingBox(坐标:ArrayList):BoundingBox{
var west=GeometryConstants.MAX_经度
var east=GeometryConstants.MIN_经度
var north=GeometryConstants.MIN_纬度
var south=GeometryConstants.MAX_纬度
coordinates.forEach{loc->
北=数学最大值(位置纬度,北)
南=数学最小值(位置纬度,南)
西=数学最小值(位置经度,西)
东方=数学最小值(位置经度,东方)
}
//添加一些额外的“填充”
val padding=0.01
北+=填充
南-=填充
西-=填充
东+=填充
从LNGLATS返回边界框(西、南、东、北)
}

Kotlin解决方案。对@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)
}
private fun zoomToMultipleGeoCoordinates(边界列表:列表,isPaddingRequired:Boolean){
var north=Double.MIN\u值
var south=双最大值
var east=双精度最小值
var west=双最大值
boundingList.forEach{loc->
北=位置纬度最低(北)
南=位置纬度最南端(南)
西面=位置经度。最西面(西面)
东方=位置经度最小(东方)
}
如果需要(isPaddingRequired){
val padding=0.01
北+=填充
南-=填充
西-=填充
东+=填充
}
从LNGLATS返回边界框(西、南、东、北)
}

谢谢@brad leege!嘿@Brad Leege,你能告诉我如何为记号笔的工具提示添加自定义背景吗?谢谢@Brad Leege!为什么要在居中的用户位置之后?使用上面的方法找到边界框,方法是将arraylist中的地理坐标集传递给函数,它将返回边界框,以便您可以在地图上设置它Double west=0.0//最大双倍东=0.0//minX双北=0.0//maxY double south=0.0//对吗?