从一条直线在 google 地图上创建一个直角矩形

creating a right angled rectangle on google maps from a line

我有一个应用程序,用户可以在其中画一条线,然后软件会自动创建一个矩形,该线与直角矩形的两个相邻边的中点相交。见下文,红线是用户绘制的,紫色矩形是计算得出的。我的算法几乎可以工作,在某些方向上矩形不是直角的。我想知道为什么,我的代码中有相当多的近似值,根据我使用的比例(几公里)应该没问题。我不应该担心地球的曲率。

有人知道如何改进我的代码吗? 这是我的参考资料: calculate points lat and long to meters

        public static MapRectangle CreatMapRectangle(BalingZone zone, double thickness)
    {
        MapsCoordinate rectpoint2;
        MapsCoordinate rectPoint1 ;
        MapsCoordinate rectPoint3 ;
        MapsCoordinate rectPoint4 ;

        var point1 = zone.Coordinates[0];
        var point2 = zone.Coordinates[1];

        var latitudeDiff = LatitudeDiffToMeters(point2.Latitude - point1.Latitude);
        var longitudeDiff = LongitudeDiffToMeters(point1.Longitude - point2.Longitude, point1.Latitude);
        var slopeB = longitudeDiff / latitudeDiff;

        double latOffset = thickness * (slopeB / Math.Sqrt(1 + slopeB * slopeB));
        double longOffset = thickness * (1 / Math.Sqrt(1 + slopeB * slopeB));


        double p3Lat = CalculateLatitude(point1.Latitude, latOffset);
        double p3Long = CalculateLongitude( point1.Longitude, p3Lat , longOffset);
        rectPoint1 = new MapsCoordinate(p3Lat, p3Long);


        double p4Lat = CalculateLatitude(point1.Latitude, -latOffset);
        double p4Long = CalculateLongitude(point1.Longitude, p4Lat, -longOffset);
        rectpoint2 = new MapsCoordinate(p4Lat, p4Long);


        double p5Lat = CalculateLatitude(point2.Latitude, latOffset);
        double p5Long = CalculateLongitude( point2.Longitude, p5Lat , longOffset);
        rectPoint4 = new MapsCoordinate(p5Lat, p5Long);

        double p6Lat = CalculateLatitude(point2.Latitude, -latOffset);
        double p6Long = CalculateLongitude( point2.Longitude, p6Lat , -longOffset);
        rectPoint3 = new MapsCoordinate(p6Lat, p6Long);

        return new MapRectangle(rectPoint4, rectPoint3, rectPoint1, rectpoint2, thickness);
    }

    //use the quick and dirty estimate that 111,111 meters (111.111 km) in the y direction is 1 degree (of latitude)
    // and 111,111 * cos(latitude) meters in the x direction is 1 degree (of longitude).

    private static double LatitudeDiffToMeters(double latitudeDiff)
    {
        return 111111.0 * latitudeDiff;
    }

    private static double LongitudeDiffToMeters(double longitudeDiff, double latitude)
    {
        return 111111.0*Math.Cos(latitude)*longitudeDiff;
    }


    private static double CalculateLatitude(double latitude, double offset)
    {
        return latitude + offset/111111.0;
    }

    private static double CalculateLongitude(double longitude, double latitude, double offset)
    {
        return longitude + offset/(111111.0*Math.Cos(latitude));
    }
}

不确定为什么这个问题被否决了! 不管怎样,我的代码有一个错误,我在数学函数中使用了度数而不是弧度。我还更新了计算以假设地球是球形的(以便更好地靠近两极工作)

private static double LatitudeDiffToMeters(double latitudeDiff)
    {
        return (R*latitudeDiff*Math.PI)/180;
    }

    private static double LongitudeDiffToMeters(double longitudeDiff, double latitude)
    {
        return (longitudeDiff*Math.PI* R * Math.Cos (Math.PI * latitude / 180))/180;
    }


    private static double CalculateLatitude(double latitude, double offset)
    {
        return latitude + ((offset/R) * (180/Math.PI));
    }

    private static double CalculateLongitude(double longitude, double latitude, double offset)
    {
        return longitude + offset / (R * Math.Cos (Math.PI * latitude / 180)) * (180 / Math.PI);
    }