Convert Degrees/Minutes/Seconds to Decimal Coordinates

前端 未结 7 1295
青春惊慌失措
青春惊慌失措 2020-12-01 12:31

In one part of my code I convert from decimal coordinates to degrees/minutes/seconds and I use this:

double coord = 59.345235;
int sec = (int)Math.Round(coor         


        
7条回答
  •  情歌与酒
    2020-12-01 13:14

    Just to save others time, I wanted to add on to Byron's answer. If you have the point in string form (e.g. "17.21.18S"), you can use this method:

    public double ConvertDegreeAngleToDouble(string point)
    {
        //Example: 17.21.18S
    
        var multiplier = (point.Contains("S") || point.Contains("W")) ? -1 : 1; //handle south and west
    
        point = Regex.Replace(point, "[^0-9.]", ""); //remove the characters
    
        var pointArray = point.Split('.'); //split the string.
    
        //Decimal degrees = 
        //   whole number of degrees, 
        //   plus minutes divided by 60, 
        //   plus seconds divided by 3600
    
        var degrees = Double.Parse(pointArray[0]);
        var minutes = Double.Parse(pointArray[1]) / 60;
        var seconds = Double.Parse(pointArray[2]) / 3600;
    
        return (degrees + minutes + seconds) * multiplier;
    }
    

提交回复
热议问题