【发布时间】:2017-09-20 06:53:58
【问题描述】:
我在 C# 的循环中调用 JavaScript 函数。每次调用都会向函数发送描述、经度和纬度。该函数在地图中创建一个单独的标记。我想看到的是在循环中发送相应信息的所有标记。我看到的是已发送的最后一个项目的标记。
C#代码
protected void MapButton_Click(object sender, EventArgs e)
{
///////////////////////////////////////////////
// Get all Games that are being played today //
// then send HomeTeam, AwayTeam and //
// Coordinates to MapIt. //
///////////////////////////////////////////////
///////////////////////////////////////////////
// Step 1: Call Stored Procedure that will //
// return the TeamNames and //
// Coordinates where the games will //
// be played today. //
///////////////////////////////////////////////
string strcon = WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
using (SqlConnection conn = new SqlConnection(strcon))
{
SqlCommand cmd = new SqlCommand("GameDayCoordinates", conn);
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
DataTable MapItTbl = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(MapItTbl);
///////////////////////////////////////////////
// Step 2: Send each set of Teams and //
// Coordinates to MapIt. Within a //
// loop. //
// Send the Google Map Coordinates, and this //
// function will mark the places on Google //
// Maps via a JavaScript function. //
///////////////////////////////////////////////
int count = MapItTbl.Rows.Count;
string[] splitSeparator = new string[] { ", ",", "};
LocObject[] LocationInfo = new LocObject[count];
for (int i = 0; i < MapItTbl.Rows.Count; i++)
{
DataRow row = MapItTbl.Rows[i];
string Coordinates = row["Coordinate"].ToString();
if (Coordinates != "NULL") {
string[] strArr = Coordinates.Split(splitSeparator, StringSplitOptions.None);
ScriptManager.RegisterStartupScript((Page)this, base.GetType(), i + "GMaps" + DateTime.UtcNow, string.Format("GameMap('{0}','{1}','{2}','{3}');", row.ItemArray.GetValue(0).ToString(), strArr[0], strArr[1], i), true);
}
}
}
}
被调用的 JavaScript 函数
function GameMap(description, lat, lng, index) {
var marker = new Array();
var i;
var max = 100;
var myLatLng = { lat: 32.787407, lng: -82.269194 };
var MapObj;
MapObj = document.getElementById('map1');
var map1 = new google.maps.Map(MapObj, {
zoom: 7,
center: myLatLng,
mapTypeId: google.maps.MapTypeId.HYBRID
});
var GameLatlng = new google.maps.LatLng(parseFloat(lat), parseFloat(lng));
document.write(description);
document.write(index);
marker = new google.maps.Marker({
position: GameLatlng,
title: description
});
//for (i = 0; i < 8; i++) {
marker.setMap(map1);
//}
}
如何让所有标记(而不仅仅是最后一个标记)显示出来?
【问题讨论】:
-
var marker = new Array();,,, 然后marker = new google.maps.Marker({... 所以,marked 永远不会用作数组,那么为什么要这样初始化它呢? -
曾尝试在某一时刻将其用作数组,以期唯一标识该标记。事实证明这无关紧要。
-
有没有办法确保在我离开 JavaScript 函数后标记将保持显示?创建标记后,是否会在新调用 JavaScript 函数时覆盖它?
-
是的......你把标记放在地图上......这就是它的显示方式
-
然后,要么标记在后续调用中被覆盖,要么进程只识别最后创建的标记。
标签: javascript c# google-maps google-maps-markers