2012-12-11 22 views
0

我試圖循環訪問xmlnodecollection並獲取一些Google地圖標記的值。 我正在嘗試使用字典,但它並不真正適用於集合中的多個節點。 我希望能夠爲同一個鍵保存更多的一個鍵,值對,並且如果有人有一些想法我可以做什麼,那麼更歡迎來幫助我。 以下是我有:c#爲同一個鍵值數組多個值

 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窗體應用程序中使用此代碼。 在此先感謝,Laziale

+4

使用'system.linq.xml'和'ToLookup' – Jodrell

回答

0

您應該創建一個包含所需屬性的類,然後創建一個列表,而不是執行您當前正在執行的操作。這很簡單,它會更容易閱讀。

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); 
    } 
相關問題