我正在尝试遍历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 Forms App中使用此代码。 在此先感谢Laziale
答案 0 :(得分: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);
}