Bing地图图钉没有出现

时间:2014-07-15 09:48:08

标签: c# asp.net bing-maps

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

我使用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>

1 个答案:

答案 0 :(得分:0)

我怀疑你的纬度和经度元素的id是不同的,因为ASP.NET会向他们添加信息。如果您检查生成的HTML,您可能会发现ID不是简单的&#39;纬度&#39;或者经度&#39;但更有可能像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);