当我从数据库中加载数据时,我想在地图上创建多个图钉,现在我只得到 1 个图钉

本文关键字:创建 地图 数据库 加载 数据 | 更新日期: 2023-09-27 18:32:05

我正在使用 xamarin 形式的地图,遇到了一个我似乎无法解决的问题。我想添加多个引脚(到多个位置),在我的数据库中解析我有 3 个不同位置的数据,但是当我在应用程序上测试它时,地图上只出现了 1 个引脚。

这是我的代码:

var getItems = await parseAPI.getInfo (Application.Current.Properties ["sessionToken"].ToString ()); //I load my data.
foreach (var currentItem in getItems["results"]) {
    mLong = currentItem ["long"].ToString (); //my long from my data
    mLat = currentItem ["lat"].ToString (); //my lat from my data
}
var storeLong = Double.Parse (mLong, CultureInfo.InvariantCulture); //converting the long from string to int.
var storeLat = Double.Parse (mLat, CultureInfo.InvariantCulture); //converting the lat from string to int.
if (mLong != null) {
    var pin = new Pin ();
    pin.Position = new Position (storeLat,storeLng); //here i add the data i have.
    pin.Label = "test";
    pin.Address = "test";
    theMap.Pins.Add (pin); //adding my pins to my map. 
}

也许我需要以某种方式创建一个list <String>

当我从数据库中加载数据时,我想在地图上创建多个图钉,现在我只得到 1 个图钉

您不是为每个getItems创建一个引脚,而是最后一个,请尝试扩展 foreach 以包装整个代码片段:

var getItems = await parseAPI.getInfo (Application.Current.Properties ["sessionToken"].ToString ()); //I load my data.
foreach (var currentItem in getItems["results"]) 
{
    mLong = currentItem ["long"].ToString (); //my long from my data
    mLat = currentItem ["lat"].ToString (); //my lat from my data
    var storeLong = Double.Parse (mLong, CultureInfo.InvariantCulture); //converting the long from string to int.
    var storeLat = Double.Parse (mLat, CultureInfo.InvariantCulture); //converting the lat from string to int.
    if (mLong != null) 
    {
        var ourPosition = new Position (13, -15);
        theMap.MapType = MapType.Hybrid;
        theMap.MoveToRegion (
            MapSpan.FromCenterAndRadius (
                ourPosition, Distance.FromMeters (300)));
        var pin = new Pin ();
        pin.Position = new Position (storeLat,storeLng); //here i add the data i have.
        pin.Label = "test";
        pin.Address = "test";
        pin.Clicked += onButtonClicked1;
        theMap.Pins.Add (pin); //adding my pins to my map. 
    }
}