【问题标题】:Bing Maps pushpins not appearing必应地图图钉未出现
【发布时间】:2014-07-15 09:48:07
【问题描述】:

我希望从服务器端添加必应地图图钉。我尝试了下面的代码,但图钉没有出现。关于如何改进下面的代码并使其工作有什么建议吗?

我在下面的程序中使用 asp.net 和 C# 以及 Bing Maps Ajax Control V7.0

C#:

protected void Page_Load(object sender, EventArgs e)
{
    GetLocation(sender,e);
}

protected void GetLocation(object sender, EventArgs e)
{
    DataTable tblLocation = new DataTable();
    string query = "SELECT Latitude, Longitude FROM Location ";
    if (textbox1.Text != "")
    {
        textbox1.Text = Convert.ToString(Convert.ToInt32(textbox1.Text) + 1);
    }
    else
    {
        textbox1.Text = "1";
    }
    query += " WHERE num = " + textbox1.Text;
    SqlDataAdapter adapter = new SqlDataAdapter();
    string connString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
    SqlConnection conn = new SqlConnection(connString);
    conn.Open();
    adapter.SelectCommand = new SqlCommand(query, conn);
    adapter.Fill(tblLocation);
    conn.Close();

    if (tblLocation.Rows.Count > 0)
    {
        longitude.Text = tblLocation.Rows[0]["Longitude"].ToString();
        latitude.Text = tblLocation.Rows[0]["Latitude"].ToString();
        string script = "UpdatePushPin( + " + latitude.Text + " , " + longitude.Text + ");";
        ScriptManager.RegisterStartupScript(this, this.GetType(), "Key", script, true);
    }
    else
    {
        textbox1.Text = Convert.ToString(Convert.ToInt32(textbox1.Text) - 1);
    }
}

Asp.net:

    <script type='text/javascript' src='http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0'></script>
<script type='text/javascript'>
    var map;
    function GetMap() {
        map = new Microsoft.Maps.Map(document.getElementById("mapDiv"),
            {credentials: "mycredentials",
            center: new Microsoft.Maps.Location(12.2, 103.7),
            mapTypeId: Microsoft.Maps.MapTypeId.road,
            zoom: 12
        });
    }
    function UpdatePushPin(x, y) {
        map.entities.clear();
        var pushpin = new Microsoft.Maps.Pushpin(new Microsoft.Maps.Location(x, y));
        map.entities.push(pushpin);
    }
</script>

【问题讨论】:

    标签: c# asp.net bing-maps


    【解决方案1】:

    我怀疑您的纬度和经度元素的 id 由于 ASP.NET 向它们添加了信息而不同。如果您检查生成的 HTML,您可能会看到 ID 不仅仅是“纬度”或“经度”,而更像是 ctl00$MainContent$longitude。要解决此问题,您可以将 ASP.NET 控件的 ClientID 传递到您正在生成的脚本中,如下所示:

    string script = string.Format(@"
    <script type='text/javascript'>
    function updatePushPin() {
    map.entities.clear();
    map.setView({zoom: 12, center: new Microsoft.Maps.Location(document.getElementById('{0}').text, document.getElementById('{1}').text)});
    
    var center = map.getCenter();
    var pin = new Microsoft.Maps.Pushpin(center);
    map.entities.push(pin);
    
    }
    </script>", latitude.ClientID, longitude.ClientID);
    

    【讨论】:

    • 抱歉回复晚了。我也怀疑,所以我改变了我的代码。我也试过你的,但它不起作用。以上是我编辑的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-12
    • 1970-01-01
    相关资源
    最近更新 更多