You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#中CoordinateSharp包MGRS转经纬度同步失效问题

坐标转换同步问题(MGRS转经纬度失效)

我使用CoordinateSharp包进行坐标转换,经纬度转MGRS功能正常:修改经纬度NumericUpDown框时,MGRS文本框(textBox3)会更新为正确值;点击下一个航点按钮时,数值也会相应变化。但修改MGRS文本框(textBox3)时,无法将对应的经纬度值同步更新到经纬度NumericUpDown框中。


textBox3的TextChanged事件代码

private void textBox3_TextChanged(object sender, EventArgs e)
{
    textBox1.Text = textBox3.Text;
            
    if (cycle)
    {
        cycle = true;
        string mgrsValue = textBox3.Text;
        try
        {
            if (Coordinate.TryParse(mgrsValue, out Coordinate c))
            {
                LatDeg.Value = (decimal)c.Latitude.DecimalDegree;
                LongDeg.Value = (decimal)c.Longitude.DecimalDegree;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("An error occurred: " + ex.Message);
        }
        cycle = false;
    }
}

纬度NumericUpDown的ValueChanged事件代码

private void LatDeg_ValueChanged(object sender, EventArgs e)
{
    try
    {
        if (!cycle)
        {
            double latitude = (double)LatDeg.Value;
            double longitude = (double)LongDeg.Value;
            Coordinate c = new Coordinate(latitude, longitude);
            textBox3.Text = c.MGRS.ToString();
            Hide_BTNs();
        }
        cycle = false;
    }
    catch (Exception ex)
    {
        MessageBox.Show("An error occurred: " + ex.Message, "Error");
    }
}

经度NumericUpDown的ValueChanged事件代码

private void LongDeg_ValueChanged(object sender, EventArgs e)
{
    try
    {
        if (!cycle)
        {
            double latitude = (double)LatDeg.Value;
            double longitude = (double)LongDeg.Value;
            Coordinate c = new Coordinate(latitude, longitude);
            textBox3.Text = c.MGRS.ToString();
            Hide_BTNs();
        }
        cycle = false;
    }
    catch (Exception ex)
    {
        MessageBox.Show("An error occurred: " + ex.Message, "Error");
    }
}

下一个航点按钮点击事件代码(仅供参考)

private void NextWP_Click(object sender, EventArgs e)
{
    cycle = true;

    WPNumber.Value = WPNumber.Value + 1;
    int no = (int)WPNumber.Value;

     cycle = false;
}

补充说明

cycle布尔变量用于区分操作场景:点击下一个航点按钮后,对应航点的经纬度会显示在数值框中,并转换为MGRS显示在textBox3中;修改数值框可更新textBox3,但反向操作无效。

已尝试方法

制作了一个带文本框的面板,点击转换按钮时可将输入的MGRS转换为对应的经纬度,该功能正常。但textBox3使用相同代码却无法生效,不知原因。


内容的提问来源于stack exchange,提问作者smfa3

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 19:50:28