0

我正在尝试遍历 xmlnodecollection 并获取谷歌地图标记的一些值。我正在尝试使用字典,但它不适用于集合中的多个节点。

我希望能够为同一个键保存一个以上的键值对。这是我所拥有的:

        Dictionary<string, string> mapValues = new Dictionary<string, string>();
        foreach (XmlNode node in listProperties)
        {
            row = tblResults.NewRow();                
            row["Id"] = node.Attributes[0].Value;           
            row["Latitude"] = node["Location"].Attributes[0].Value;
            row["Longitude"] = node["Location"].Attributes[1].Value;
            row["City"] = node["Location"].Attributes[2].Value;
            row["Address"] = node["Location"].Attributes[3].Value;
            row["ZipCode"] = node["Location"].Attributes[4].Value;
            row["State"] = node["Location"].Attributes[5].Value;
            mapValues.Add("Latitude", node["Location"].Attributes[0].Value);
            mapValues.Add("Longitude", node["Location"].Attributes[1].Value);
            mapValues.Add("City", node["Location"].Attributes[2].Value);
            mapValues.Add("Address", node["Location"].Attributes[3].Value);
            mapValues.Add("ZipCode", node["Location"].Attributes[4].Value);
            mapValues.Add("State", node["Location"].Attributes[5].Value);

            tblResults.Rows.Add(row);
        }
       GenerateMap(mapValues);

然后在 GenerateMap 我想使用这些值并将标记放在地图对象上:

  private void GenerateMap(Dictionary<string, string> mapInfo)
        {
            gMapControl1.SetCurrentPositionByKeywords("USA");
            gMapControl1.MinZoom = 3;
            gMapControl1.MaxZoom = 17;
            gMapControl1.Zoom = 4;

            gMapControl1.Manager.Mode = GMap.NET.AccessMode.ServerAndCache;
            gMapControl1.Position = new GMap.NET.PointLatLng(29.60862, -82.43821);
            gMapControl1.MapProvider = GMap.NET.MapProviders.GoogleMapProvider.Instance;
            GMap.NET.WindowsForms.GMapOverlay address_overlay = new GMap.NET.WindowsForms.GMapOverlay(gMapControl1, "Address1");

            foreach (KeyValuePair<string, string> info in mapInfo)
            {
                PointLatLng pnl = new PointLatLng(Convert.ToDouble(info.Value[0]), Convert.ToDouble(info.Value[1]));
                GMapMarkerGoogleRed marker = new GMapMarkerGoogleRed(pnl);
                MarkerTooltipMode mode = MarkerTooltipMode.OnMouseOver;
                marker.ToolTipMode = mode;
                marker.ToolTipText = info.Value[2] + ", " + info.Value[3] + ", " + info.Value[4] + ", " + info.Value[5];
                address_overlay.Markers.Add(marker);
            }
            gMapControl1.Overlays.Add(address_overlay);
        }

我怎样才能做到这一点?我在 Windows 窗体应用程序中使用此代码。

4

1 回答 1

1

您应该创建一个包含您需要的属性的类,然后创建一个列表,而不是做您当前正在做的事情。它很简单,而且更容易阅读。

public class MapValues
{
    public string Latitude { get; set; }
    public string Longitude{ get; set; }
    public string City{ get; set; }
    public string Address{ get; set; }
    public string ZipCode{ get; set; }
    public string State{ get; set; }

    public MapValues(string latitude, string longitude, string city, string address, string zipCode, string state)
    {
        this.Latitude = latitude;
        this.Longitude= longitude;
        this.City= city;
        this.Address= address;
        this.ZipCode= zipCode;
        this.State= state;
    }
}

将您的代码更改为以下内容:

    List<MapValues> mapValues = new List<MapValues>();
    foreach (XmlNode node in listProperties)
    {
        row = tblResults.NewRow();                
        row["Id"] = node.Attributes[0].Value;           
        row["Latitude"] = node["Location"].Attributes[0].Value;
        row["Longitude"] = node["Location"].Attributes[1].Value;
        row["City"] = node["Location"].Attributes[2].Value;
        row["Address"] = node["Location"].Attributes[3].Value;
        row["ZipCode"] = node["Location"].Attributes[4].Value;
        row["State"] = node["Location"].Attributes[5].Value;

        mapValues.Add(
            new MapValues(
               row["Latitude"],
               row["Longitude"],
               row["City"],
               row["Address"],
               row["ZipCode"],
               row["State"]));

        tblResults.Rows.Add(row);
    }
    GenerateMap(mapValues);

您更新的方法:

    private void GenerateMap(List<MapValues> mapInfo)
    {
        gMapControl1.SetCurrentPositionByKeywords("USA");
        gMapControl1.MinZoom = 3;
        gMapControl1.MaxZoom = 17;
        gMapControl1.Zoom = 4;

        gMapControl1.Manager.Mode = GMap.NET.AccessMode.ServerAndCache;
        gMapControl1.Position = new GMap.NET.PointLatLng(29.60862, -82.43821);
        gMapControl1.MapProvider = GMap.NET.MapProviders.GoogleMapProvider.Instance;
        GMap.NET.WindowsForms.GMapOverlay address_overlay = new GMap.NET.WindowsForms.GMapOverlay(gMapControl1, "Address1");

        foreach (MapValues info in mapInfo)
        {
            PointLatLng pnl = new PointLatLng(Convert.ToDouble(info.Latitude), Convert.ToDouble(info.Longitude));
            GMapMarkerGoogleRed marker = new GMapMarkerGoogleRed(pnl);
            MarkerTooltipMode mode = MarkerTooltipMode.OnMouseOver;
            marker.ToolTipMode = mode;
            marker.ToolTipText = info.City + ", " + info.Address + ", " + info.ZipCode + ", " + info.State;
            address_overlay.Markers.Add(marker);
        }
        gMapControl1.Overlays.Add(address_overlay);
    }
于 2012-12-11T17:52:17.227 回答