[英]Bing Maps pushpins not appearing
我希望從服務器端添加Bing Maps圖釘。 我嘗試了下面的代碼,但圖釘沒有出現。 關於如何改進下面的代碼並使之工作,有什么建議嗎?
我將以下程序與Bing Maps Ajax Control V7.0一起使用asp.net和C#
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>
我懷疑您的經度和緯度元素的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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.