demand

Line segments: Calculate the actual length area of line segments on the map: Calculate the actual area of irregular polygons on the map

The solution

# # # # 1. The actual length of the line segment Direct use of gold AMapUtils. CalculateLineDistance (latLng, latLng) method. # # # # 2. Arbitrary polygon area calculation: blog.csdn.net/xxdddail/ar…

Specific code:

/** * Calculates the area of any polygon * @param latLngLines latitude and longitude coordinates * @return
     */
    public float calculateArea(List<LatLng> latLngLines) {
        List<double[]> pointFList = new ArrayList<>();
        for(int i = 0; i < latLngLines.size(); i++) { LatLng latLng = latLngLines.get(i); // pointFList. Add (amaputil.wgs2flat (latlng.longitude, latlng.latitude)); } int iCycle, iCount; iCycle = 0; double iArea = 0; iCount = pointFList.size();for (iCycle = 0; iCycle < iCount; iCycle++) {
            iArea = iArea + (pointFList.get(iCycle)[0] * pointFList.get((iCycle + 1) % iCount)[1] - pointFList.get((iCycle + 1) % iCount)[0] * pointFList.get(iCycle)[1]);
        }

        return (float) Math. Abs (0.5 * iArea); }Copy the code

Latitude and longitude to plane cartesian coordinate system

Public static final double EARTH_RADIUS = 6378.137; /** * Latitude and longitude are converted to plane cartesian coordinates in meters ** @param lon longitude * @Param lat latitude * @return*/ public static double[] WGS2flat(double lon, double lat) {double L = CalcRad(lon); double l = L - CalcRad(120); double B = CalcRad(lat); double cosb = Math.cos(B); double sinb = Math.sin(B); double a = EARTH_RADIUS * 1000; // Double b = 6356752.3142451793; double t = Math.tan(B); // double r = 3600 * 180 / Math.PI; double e2 = (Math.pow(a, 2) - Math.pow(b, 2)) / Math.pow(a, 2); double e12 = (Math.pow(a, 2) - Math.pow(b, 2)) / Math.pow(b, 2); double n2 = e12 * Math.pow(cosb, 2); double N = a / Math.sqrt(1 - e2 * Math.pow(sinb, 2)); Double x = 6367449.1458 * b-32009.8185 * cosb * sinb - 133.9975 * cosb * Math. Pow (sinb, Math. Pow (sinb, 5); double X = x + N / 2 * t * Math.pow(cosb, 2) * Math.pow(l, 2) + N / 24 * t * Math.pow(cosb, 4) * (5 - Math.pow(t, 2) + 9 * n2 + 4 * Math.pow(n2, 2)) * Math.pow(l, 4); double Y = N * cosb * l + N / 6 * Math.pow(cosb, 3) * (1 - Math.pow(t, 2) + n2) * Math.pow(l, 3); double[] coord = {X, Y};returncoord; } /** * calculate radians ** @param d Latitude and longitude values in degrees * @returnPublic static double CalcRad(double d) {public static double CalcRad(double d) {returnD * Math.PI / 180.0; }Copy the code