扩大由两个点创建的面积,按其距离的百分比

时间:2019-01-30 11:35:01

标签: java spring google-maps

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

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;
}

您能给我一个建议如何前进吗?

1 个答案:

答案 0 :(得分:2)

尝试这样的事情

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()));
}

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