Java 按两点距离的百分比展开两点创建的区域

Java 按两点距离的百分比展开两点创建的区域,java,spring,google-maps,Java,Spring,Google Maps,我有两个创建长方体的地理点(每个点都是一个角)。我想创建一个稍大一点的盒子。以下代码段是创建该框的代码 private Box createRouteBox(DirectionsRoute route){ Box box; Bounds bounds = route.bounds; LatLng northeast = bounds.northeast; LatLng southwest = bounds.southwest; Point northe

我有两个创建长方体的地理点(每个点都是一个角)。我想创建一个稍大一点的盒子。以下代码段是创建该框的代码

private Box createRouteBox(DirectionsRoute route){
    Box box;
    Bounds bounds = route.bounds;

    LatLng northeast = bounds.northeast;
    LatLng southwest = bounds.southwest;

    Point northeastPoint = new Point(northeast.lng, northeast.lat);
    Point southwestPoint = new Point(southwest.lng, southwest.lat);

    box = new Box(northeastPoint, southwestPoint);  

    return box;
}

你能给我一个前进的建议吗?

试试这样的方法

private Box expandedBox(Point northeastPoint, Point southwestPoint){

    // percentage of distance to set as padding to the box
    Double offset = 0.1;
    Double distance = pointDistance(northeastPoint, southwestPoint);
    Double padding = (distance * offset);

    // get current x, y
    Double neX = northeastPoint.getX();
    Double neY = northeastPoint.getY();
    Double swX = southwestPoint.getX();
    Double swY = southwestPoint.getY();

    // init new x, y
    Double neX2, neY2, swX2, swY2;


    if(neX > swX){
        neX2 = neX + padding;
        swX2 = swX - padding;
    } else {
        neX2 = neX - padding;
        swX2 = swX + padding;
    }

    if(neY > swY){
        neY2 = neY + padding;
        swY2 = swY - padding;
    } else {
        neY2 = neY - padding;
        swY2 = swY + padding;
    }

    northeastPoint = new Point(neX2, neY2);
    southwestPoint = new Point(swX2, swY2);

    return new Box(northeastPoint, southwestPoint);
}

private Double pointDistance(Point p1, Point p2) {
    return Math.sqrt((p2.getY() - p1.getY()) * (p2.getY() - p1.getY()) + (p2.getX() - p1.getX()) * (p2.getX() - p1.getX()));
}

您可以将偏移更改为所需距离的百分比。

尝试类似的操作

private Box expandedBox(Point northeastPoint, Point southwestPoint){

    // percentage of distance to set as padding to the box
    Double offset = 0.1;
    Double distance = pointDistance(northeastPoint, southwestPoint);
    Double padding = (distance * offset);

    // get current x, y
    Double neX = northeastPoint.getX();
    Double neY = northeastPoint.getY();
    Double swX = southwestPoint.getX();
    Double swY = southwestPoint.getY();

    // init new x, y
    Double neX2, neY2, swX2, swY2;


    if(neX > swX){
        neX2 = neX + padding;
        swX2 = swX - padding;
    } else {
        neX2 = neX - padding;
        swX2 = swX + padding;
    }

    if(neY > swY){
        neY2 = neY + padding;
        swY2 = swY - padding;
    } else {
        neY2 = neY - padding;
        swY2 = swY + padding;
    }

    northeastPoint = new Point(neX2, neY2);
    southwestPoint = new Point(swX2, swY2);

    return new Box(northeastPoint, southwestPoint);
}

private Double pointDistance(Point p1, Point p2) {
    return Math.sqrt((p2.getY() - p1.getY()) * (p2.getY() - p1.getY()) + (p2.getX() - p1.getX()) * (p2.getX() - p1.getX()));
}

您可以将偏移量更改为所需距离的百分比。

您使用的API是什么?
DirectionsRoute
来自谷歌地图和
Box
Point
来自spring framework geoOK我知道您有一个有效的答案……您使用的API是什么?
DirectionsRoute
来自谷歌地图和
Box
来自spring框架geoOK,正如我看到的,您有一个有效的答案。。。