【问题标题】:How to update pin information on google map using xamarin如何使用 xamarin 更新谷歌地图上的 pin 信息
【发布时间】:2021-09-01 10:13:27
【问题描述】:

需要将直连数据库替换为API

我使用这段代码直接连接到 MySQL db 并更改 pin 信息:

 public async void DatabaseConnection(List<CustomPin> pins)
    {
        string ConnectionString = "server=192.168.0.1;uid=user;port=4444;pwd=pass;database=dbName;";
        MySqlConnection Conn = new MySqlConnection(ConnectionString);

        try
        {
            Conn.Open();
            string query = "SELECT * FROM sel_alert_level s;";
            MySqlCommand myCommand = new MySqlCommand(query, Conn);
            MySqlDataReader myReader;

            myReader = myCommand.ExecuteReader();
            try
            {
                while (myReader.Read())
                {

                    int codeNum = myReader.GetInt32(4);
                    int level = myReader.GetInt32(3);
                    int mapCode = myReader.GetInt32(0);

                    foreach (var item in pins)
                    {
                        if (item.CodeNum == codeNum)
                        {
                            item.AlertLevel = level;
                            item.CodeNum = codeNum;
                            item.MapCode = mapCode;

                            //await DisplayAlert("Alert", mapCode.ToString(), "ok");
                        }
                    }
                    //await DisplayAlert("Database Connection", "Connected .." + Environment.NewLine + myReader.GetInt32(0) + Environment.NewLine + myReader.GetString(1) + Environment.NewLine + myReader.GetString(2) + Environment.NewLine + myReader.GetInt32(3) + Environment.NewLine + myReader.GetInt32(4), "OK");
                }
            }
            finally
            {
                myReader.Close();
                Conn.Close();
            }
        }

        catch (Exception ex)
        {
            await DisplayAlert("Database Connection", "Not Connected ..." + Environment.NewLine + ex.ToString(), "OK");
        }
    }

使用此代码,我成功更新了 pin 信息。

现在我使用 API Response 创建了相同的方法,以及像DatabaseConnection(); 一样的操作只是尝试更新信息,但对我不起作用:(

 public async void APIConnection(List<CustomPin> pins)
    {
        try
        {
            WaterBindingData waterData = await _restServiceData.GetWaterDataForecast(GenerateRequestUriStations(Constants.EndPoint), GenerateRequestUri(Constants.EndPoint));


            foreach (var water in waterData.WaterStation.Stations)
            {
                foreach (var item in pins)
                {
                    if (item.CodeNum == water.CodeNum)
                    {

                        item.AlertLevel = water.AlertLevelStation;
                        item.CodeNum = water.CodeNum;
                        item.MapCode = water.MapCode;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            await DisplayAlert("Data Alert", "Error:" + Environment.NewLine + ex.ToString(), "OK");
        }
    }

我这里没有任何错误。 waterData 带有数据,但引脚中的数据没有改变..我不知道为什么......

现在我的信息没有改变..

MapCode and other variables not changed.

我在构造函数中这样调用这两个方法:

 DatabaseConnection(customMap.CustomPins);
 APIConnection(customMap.CustomPins);

所以...当我启动项目时,我收到如下消息:

/Applications/Visual Studio.app/Contents/Resources/lib/monodevelop/bin/MSBuild/Current/bin/Microsoft.Common.CurrentVersion.targets(5,5): Warning MSB3276: Found conflicts between different versions of the same dependent assembly. Please set the "AutoGenerateBindingRedirects" property to true in the project file. For more information, see http://go.microsoft.com/fwlink/?LinkId=294190. (MSB3276) (MaritsaTundzhaForecast.iOS)

我检查了this link,但我没有properties 选项,因为我使用的是mac。我的项目只有options

这可能不会改变图钉中的内容吗?它不起作用的原因是什么?

我检查了循环中的 if 语句,她工作了:

【问题讨论】:

  • 你调试过你的代码吗?你确定foreach循环和if语句中的代码实际上正在执行吗?
  • 我更新了问题,看最后一张截图。 if 语句工作正常..
  • sql和webapi唯一的不同就是一个是异步方法,一个不是。
  • 有没有办法解决这个问题?

标签: ios google-maps xamarin


【解决方案1】:

由于是异步方法,建议你尝试在APIConnection方法中赋值customMap.CustomPins

类似的东西


//constructor
List<CustomPin> pins = xxxxx;
APIConnection(pins);

 public async void APIConnection(List<CustomPin> pins)
    {
        try
        {
            WaterBindingData waterData = await _restServiceData.GetWaterDataForecast(GenerateRequestUriStations(Constants.EndPoint), GenerateRequestUri(Constants.EndPoint));


            foreach (var water in waterData.WaterStation.Stations)
            {
                foreach (var item in pins)
                {
                    if (item.CodeNum == water.CodeNum)
                    {

                        item.AlertLevel = water.AlertLevelStation;
                        item.CodeNum = water.CodeNum;
                        item.MapCode = water.MapCode;
                    }
                }
            }

             customMap.CustomPins = pins;    //assign the value in this line
        }
        catch (Exception ex)
        {
            await DisplayAlert("Data Alert", "Error:" + Environment.NewLine + ex.ToString(), "OK");
        }
    }

【讨论】:

  • 嗨,在构造函数中举个例子,List pins = xxxxx;我使用:CustomPin pin1 = new CustomPin { Type = PinType.Place, Position = new Position(41.59043006333251, 24.766286971618303), Name = "Xamarin", Label = "р.Бяла", Address = "гр.Смолян", CodeNum = 1 , 警报级别 = 2 };
  • 使用后:customMap.CustomPins = new List { pin1, pin2... ... 之后我使用 customMap.Pins.Add(pin1); customMap.Pins.Add(pin2);
  • 只是先初始化列表,不要在构造函数的customMap.Pins中添加pin,尝试在方法APIConnection中赋值。
  • 我要调用 APIConnection(pins);但我在构造函数中收到错误。当我使用 APIConnection(customMap.CustomPins);我没有收到错误但方法不起作用
  • 错误信息是什么?
猜你喜欢
  • 2021-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-29
  • 1970-01-01
  • 1970-01-01
  • 2017-09-13
相关资源
最近更新 更多