我正在使用后面的代码将一些元素从数据库添加到 javascript 数组中,代码如下:
protected void LoadPlaces() {
try {
DataTable dt = cs.ExecuteQry(@"SELECT location_coords from igr.locationdata ", null);
String Script = "";
foreach(DataRow dr in dt.Rows) {
string Coord = "PlacePoints=[";
bool IsNotFirst = false;
foreach(string C in dr[0].ToString().Trim().Split('\n')) {
if (IsNotFirst) Coord += ",\n";
Coord += @"new google.maps.LatLng(" + C.Trim().Split(';')[0].Split(':')[1] + "," + C.Trim().Split(';')[1].Split(':')[1] + @") ";
IsNotFirst = true;
}
Coord += "];";
Script += Coord;
//ScriptManager.RegisterStartupScript(Page, GetType(), "AddPointsToMap", Coord, true);
}
ScriptManager.RegisterClientScriptBlock(Page, GetType(), "SetMap", Script, true);
} catch (Exception ex) {
Alert(ex.Message);
}
}
我从 Page_Load() 调用了这个函数,但是当我尝试访问 map 变量时,它是未定义的,我该如何解决这个问题时间javascript变量映射没问题。Javascript代码是
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
AddPlace = function (Place) {
if (Place == null) return;
var mark = new google.maps.Polygon({
paths: Place,
strokeColor: '#F50',
strokeOpacity: 0.5,
strokeWeight: 2,
fillColor: '#FAA',
clickable: true,
fillOpacity: 0.7
});
mark.setMap(map);
Places.push(mark);
google.maps.event.addListener(Place, 'click', function (event) {
var vertices = this.getPath();
var strvertices = '';
for (var i = 0; i < vertices.getLength(); i++) {
strvertices.value += 'A' + i + ':' + vertices.getAt(i).lat() + ';';
strvertices.value += 'B' + i + ':' + vertices.getAt(i).lng();
strvertices.value += '\n';
}
console.Debug(strvertices);
});
}