Android High German map draws a polygon, draws a rectangle with a known center point width and height, and the actual distance from kilometers to map coordinates distance

Mapabc is used in the project to draw a rectangular area. The customer can only manually select the center point and fill in the width and height of the area, and the unit of cut is km. It is required to display the planned area in real time.

Take notes.

The first step is to find the map drawing polygon api

polygon = map.addPolygon(new PolygonOptions()
        .addAll(createRectangle(marker.getPosition(), 1, 1))//绘制多边形顶点坐标
        .fillColor(0x1A1677FF)//填充颜色
        .strokeColor(0xDE1677FF)//边界颜色
        .strokeWidth(5));//边界宽度

We know the vertex coordinates and the width and height km entered by the customer. We need to calculate the vertex coordinates (rectangle) of the polygon:

 /**
     * 生成一个长方形的四个坐标点
     */
    private List<LatLng> createRectangle(LatLng center, double halfWidth,
                                         double halfHeight) {

        float distance = (float) Math.sqrt(halfWidth * halfWidth + halfHeight * halfHeight);//计算直角三角形斜边 。
        float degree= getDegree(0.0,0.0,-halfWidth,0.0,-halfWidth,halfHeight);//计算三角形锐角角度
        //生成四边形四个点坐标
        return Arrays.asList(
                getLatlng(distance, center,90-degree),
                getLatlng(distance, center, 90+degree),
                getLatlng(distance, center, 270-degree),
                getLatlng(distance, center, 270+degree)
        );
    }

Step 1. Calculate the distance from the vertex of the rectangle to the center point by the width and height

float distance = (float) Math.sqrt(halfWidth * halfWidth + halfHeight * halfHeight);

Step 2. Calculate the angle between the y axis and the diagonal of the rectangle by positioning the virtual coordinate axis

float degree= getDegree(0.0,0.0,-halfWidth,0.0,-halfWidth,halfHeight);

            Use mathematics to calculate the included angle by means of virtual coordinate system

/**
 * 在坐标系中计算两条相交线夹角
 * @param vertexPointX 交点坐标
 * @param vertexPointY
 * @param point0X A点坐标
 * @param point0Y
 * @param point1X  b点坐标
 * @param point1Y
 * @return
 */
private int getDegree(Double vertexPointX, Double vertexPointY, Double point0X, Double point0Y, Double point1X, Double point1Y) {
    //向量的点乘
    Double vector = (point0X - vertexPointX) * (point1X - vertexPointX) + (point0Y - vertexPointY) * (point1Y - vertexPointY);
    //向量的模乘
    double sqrt = Math.sqrt(
            (Math.abs((point0X - vertexPointX) * (point0X - vertexPointX)) + Math.abs((point0Y - vertexPointY) * (point0Y - vertexPointY)))
                    * (Math.abs((point1X - vertexPointX) * (point1X - vertexPointX)) + Math.abs((point1Y - vertexPointY) * (point1Y - vertexPointY)))
    );
    //反余弦计算弧度
    double radian = Math.acos(vector / sqrt);
    //弧度转角度制
    return (int) (180 * radian / Math.PI);
}

Step 3. Calculate the coordinates of the vertices on the map from the center point of the map, the actual distance (the distance from the vertices of the rectangle to the center point), and the included angle.

getLatlng(distance, center,90-degree)

/**
 *
 * @param distance 距离
 * @param latlngA 中心点坐标
 * @param angle 夹角
 * @return
 */
public static LatLng getLatlng(float distance, LatLng latlngA, double angle) {
    return new LatLng(latlngA.latitude + (distance * Math.cos(angle * Math.PI / 180)) / 111,
            latlngA.longitude + (distance * Math.sin(angle * Math.PI / 180)) / (111 * Math.cos(latlngA.latitude * Math.PI / 180))
    );
}

 

The picture I draw by default is displayed in the positive direction. Tilt if necessary. Just do tricks in the third parameter of getLatlng(distance, center,90-degree)

 

If the reference point is a vertex of a rectangle, it is easier to refer to the above method.

Guess you like

Origin blog.csdn.net/qq_36355271/article/details/108781514