我正在使用asp.net中的Google Map多标记,在这里我需要从服务器端向客户端变量发送数组值。我正在使用“ ClientScript.RegisterArrayDeclaration”将数组传递给我的客户端变量。但这是行不通的。请帮助我。我需要在页面加载时传递值。 “ locations”是我的Java脚本变量
*
<html>
<head>
<title>Google Maps Multiple Markers</title>
<script src="http://maps.google.com/maps/api/js?key=My KEy" type="text/javascript"></script>
</head>
<body>
<center>
<div id="map" style="height: 500px; width: 700px;">
</div>
</center>
<script type="text/javascript">
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 8,
center: new google.maps.LatLng(9.918648010, 78.12000),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var marker, i;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});
google.maps.event.addListener(marker, 'click', (function (marker, i) {
return function () {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
</script>
</body>
</html>
*
protected void Page_Load(object sender, EventArgs e)
{
List<String> oGeocodeList = new List<String>
{
" ['Bala', 9.91864810526, 78.1203081843780, 4] ",
" ['ganesh', 9.91864, 78.12, 5] ",
" ['santhosh', 9.9186, 78.120, 3] ",
" ['test', 9.9186481, 78.12030, 2] ",
" ['test2', 9.918670, 78.12840, 1] "
};
var geocodevalues = string.Join(",", oGeocodeList.ToArray());
ClientScript.RegisterArrayDeclaration("locations", geocodevalues);
}
最佳答案
在您的示例中,在页面上定义locations
变量之前初始化映射。一种选择是在页面加载后初始化地图:
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 8,
center: new google.maps.LatLng(9.918648010, 78.12000),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
for (var i = 0; i < locations.length; i++) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});
google.maps.event.addListener(marker, 'click', (function (marker, i) {
return function () {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
}
google.maps.event.addDomListener(window, 'load', initMap);