简体   繁体   English

围绕一个长点画一个正方形

[英]Drawing a square around a lat-long point

I'm trying to draw a square around a given point on the earth's surface. 我试图在地球表面的某个点周围画一个正方形

I'm using information I retrieved from here and here and ultimately came up with this:- 我正在使用从这里这里检索到的信息,最终得出了这个: -

        // Converting degrees to radians
        double latInDecimals = (Math.PI / 180) * latitude;
        double longInDecimals = (Math.PI / 180) * longitude;

        List<string> lstStrCoords = new List<string>();

        double changeInLat;
        double changeInLong;
        double lineOfLat;      

        // Calculating change in latitude for square of side 
        changeInLong = (side / 1000) * (360.0 / 40075);

        // Calculating length of longitude at that point of latitude
        lineOfLat = Math.Cos(longitude) * 40075;

        // Calculating change in longitude for square of side 'side'
        changeInLat = (side / 1000) * (360.0 / lineOfLat);

        // Converting changes into radians
        changeInLat = changeInLat * (Math.PI / 180);
        changeInLong = changeInLong * (Math.PI / 180);


        double nLat = changeInLat * (Math.Sqrt(2) / 2);
        double nLong = changeInLong * (Math.Sqrt(2) / 2);

        double coordLat1 = latInDecimals + nLat;
        double coordLong1 = longInDecimals + nLong;

        double coordLat2 = latInDecimals + nLat;
        double coordLong2 = longInDecimals - nLong;

        double coordLat3 = latInDecimals - nLat;
        double coordLong3 = longInDecimals - nLong;

        double coordLat4 = latInDecimals - nLat;
        double coordLong4 = longInDecimals + nLong;

        // Converting coords back to degrees

        coordLat1 = coordLat1 * (180 / Math.PI);
        coordLat2 = coordLat2 * (180 / Math.PI);
        coordLat3 = coordLat3 * (180 / Math.PI);
        coordLat4 = coordLat4 * (180 / Math.PI);

        coordLong1 = coordLong1 * (180 / Math.PI);
        coordLong2 = coordLong2 * (180 / Math.PI);
        coordLong3 = coordLong3 * (180 / Math.PI);
        coordLong4 = coordLong4 * (180 / Math.PI);

Now even though this works, the polygon that I get from joining these is a rectangle. 现在即使这样,我加入的多边形也是一个矩形。

在此输入图像描述

I'm confused as to what's wrong with my code. 我对我的代码有什么问题感到困惑。

A rectangle of one degree of latitude and longitude on the sphere has not the same length in km unless it is situated on the equator. 除非球座位于赤道上,否则球体上一个纬度和经度的矩形长度不同于km。 It gets narrower towards the poles. 它朝着两极走得更窄。 If you want to make both sides the same size you have to make a correction 如果你想让双面都相同,你必须进行修正

longitudinal_length = latitudinal_length / cos(latitude)

So you will need to divide your longitudinal length of your square by cos(latitude) . 因此,您需要将您的平方纵向长度除以cos(latitude)

Now, your square might still be crooked, but this depends on how the map is projected and this is a completely different story. 现在,您的广场可能仍然是弯曲的,但这取决于地图的投影方式,这是一个完全不同的故事。 You would need to know the projection formulas used by Google to make a correction. 您需要知道Google使用的投影公式进行更正。

You may find more complicated formulas that take account of the fact that the earth is not a perfect sphere, but I think that this should be sufficient for your position marker. 您可能会发现更复杂的公式,考虑到地球不是一个完美的球体这一事实,但我认为这应该足以满足您的位置标记。 Note also that you will get a division by zero at +/-90 degree. 另请注意,您将在+/- 90度处获得零除。 So placing a rectangle on a pole requires another approach. 因此,在杆上放置一个矩形需要另一种方法。

在此输入图像描述
From: IBM Knowledge Center / Geographic coordinate system / Figure 4. Different dimensions between locations on the graticule 来自: IBM知识中心 / 地理坐标系 /图4.经纬网上位置之间的不同尺寸

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM