使用ASP.NET将谷歌地图添加到项目中,并从数据库中检索位置

Adding Google Maps to project and retrive locatons from database using ASP.NET

本文关键字:数据库 位置 检索 项目 NET ASP 谷歌地图 添加 使用      更新时间:2023-09-26

我的意思是,我有xml文件来检索数据库,我想把它作为xml文件,并有我想在谷歌地图上显示的字段,比如"城市、国家、街道、经度、纬度"。我想从这些字段中取值并在谷歌地图中显示。我想在页面加载事件中显示这个地图。问题是我不知道如何从xml文件中检索数据并将其传递给地图。

这是代码隐藏,

void GetTableFromXMlData(string strResult)
{
    CreateTable();
    XmlDataDocument xmlDataDoc = new XmlDataDocument();
    xmlDataDoc.LoadXml(strResult);
    foreach (XmlNode n in xmlDataDoc.DocumentElement.GetElementsByTagName("Property"))
    {
        DataRow dr = dtSearchResult.NewRow();
        dr["HotelID"] = n.Attributes["IDHotel"].Value;
        dr["HotelName"] = n.Attributes["Hotelname"].Value;
        dr["MinRate"] = n.Attributes["MinRate"].Value;
        dr["MaxRate"] = n.Attributes["MaxRate"].Value;
        dr["StarCategory"] = n.Attributes["StarCategory"].Value;
        dr["ImageIdentifier"] = n.Attributes["ImageIdentifier"].Value;
        dr["VPhotoPath"] = strVirtualPath + n.Attributes["ImageIdentifier"].Value + "_Exterior.jpg";
        if (n.HasChildNodes)
        {
            foreach (XmlNode childNode in n)
            {
                switch (childNode.Name)
                {
                    case "GEOData":///----->>>> here this the data i want to display it in a map.
                        {
                            dr["CountryCode"] = childNode.Attributes["CountryCode"].Value;
                            dr["CityName"] = childNode.Attributes["City"].Value;
                            dr["CityID"] = childNode.Attributes["IDCity"].Value;
                            dr["Zip"] = childNode.Attributes["Zip"].Value;
                            dr["Street"] = childNode.Attributes["Street"].Value;
                            dr["Longitude"] = childNode.Attributes["Longitude"].Value;
                            dr["Latitude"] = childNode.Attributes["Latitude"].Value;

                            break;
                        }
                    case "Distances":
                        {
                            foreach (XmlNode cChildNode in childNode)
                            {
                                if (cChildNode.Attributes["Type"].Value == "1")
                                    dr["DistanceToCity"] = cChildNode.Attributes["Distance"].Value;
                                else
                                    dr["DistanceToAirPort"] = cChildNode.Attributes["Distance"].Value;
                            }
                            break;
                        }
                    case "Descriptions":
                        {
                            dr["Descriptions"] = childNode.FirstChild.Attributes["Text"].Value;
                            ((Label)FindControl("lblHoDescription1")).Text = childNode.FirstChild.Attributes["Text"].Value;
                            break;
                        }
                    case "Meals":
                        {
                            dr["MinimumMeals"] = childNode.Attributes["MinimumMeals"].Value;
                            break;
                        }
                    default: break;
                }
            }
        }
        dtSearchResult.Rows.Add(dr);
    }
}

一个从xml到映射

示例

一旦您有了XML格式的数据,就完成了一半以上的工作。您现在需要做的是稍微使用一下Javascript。

  • 您必须使用function initialise()初始化谷歌地图
  • MapOption中定义mapsizetype
  • 创建Geocoder类的实例&infoWindow
  • 最后编写加载标记的函数,其中标记由纬度和经度的组合坐标表示,信息窗口显示其余数据,如街道、国家代码、城市名称、Zipcode

有关详细的解释,您可以查看larrp发布的同一教程。