[英]C# array multiple values for same key
I am trying to loop through xmlnodecollection and get some values for google maps markers.我正在尝试遍历 xmlnodecollection 并获取谷歌地图标记的一些值。 I am trying with dictionary, but its not really working for multiple nodes in the collection.
我正在尝试使用字典,但它不适用于集合中的多个节点。
I want to be able to save more then one key-value pair for the same key.我希望能够为同一个键保存多个键值对。 Here is what I have:
这是我所拥有的:
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);
Then in the GenerateMap I want to use those values and put the marker on the map object:然后在 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);
}
How can I achieve that?我怎样才能做到这一点? I am using this code in Windows Forms App.
我在 Windows 窗体应用程序中使用此代码。
You should create a class than contains the properties you need, then create a list of that instead of doing what you're currently doing.您应该创建一个包含您需要的属性的类,然后创建一个列表,而不是做您当前正在做的事情。 It's simple and it'll be easier to read.
这很简单,阅读起来会更容易。
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;
}
}
Change your code to the following:将您的代码更改为以下内容:
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);
Your updated method:您更新的方法:
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.