ClientScript.RegisterStartupScript+谷歌地图
本文关键字:谷歌地图 RegisterStartupScript+ ClientScript | 更新日期: 2023-09-27 18:20:59
我在使用地理编码和clientscript.registerstartupscript使我的谷歌地图正常工作时遇到了问题。我知道我的谷歌javascript很好(在其他页面上使用),但它甚至没有达到它。我在网站的其他地方也有这个功能,不知道为什么它不起作用!非常令人沮丧!!请参阅下面的代码:
.aspx:
<script>
var geocoder;
var map;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-34.397, 150.644);
var mapOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
codeAddress();
}
function codeAddress() {
var address = document.getElementById('address').value;
geocoder.geocode({ 'address': address }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<body onload="initialize()">
.aspx.cs:
ClientScript.RegisterStartupScript(this.GetType(), "script1",
"codeAddress();");
映射必须初始化,因为它显示了空的谷歌映射外壳,但它没有填充。
Firebug在页面底部显示了以下内容:<script type="text/javascript">
codeAddress();
</script>
我使用以下代码实现了这一点。注意:变量消息的值&locationList是从codeehind获得的,使用一个公共变量并在html中赋值。
<html>
<head id="Head1">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var map;
var infowindow;
var geocoder;
var message = "<font style='font-family:Arial;font-size:12px;color:blue;'>206B Signal Hill Drive, <br/>Statesville, NC 28625 USA</font>"; //<%= Message%>
var locationList = "35.793286,-80.855383"; //<%= LocationList%>
var args = locationList.split(",");
function initialize() {
geocoder = new google.maps.Geocoder();
var myLatlng = new google.maps.LatLng(args[0], args[1]);
var myOptions = {
zoom: 15,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var location = new google.maps.LatLng(args[0], args[1])
var marker = new google.maps.Marker({
position: location,
map: map
});
marker.setTitle(message.replace(/(<([^>]+)>)/ig, ""));
attachSecretMessage(marker, 0);
}
function attachSecretMessage(marker, number) {
infowindow = new google.maps.InfoWindow(
{ content: message,
size: new google.maps.Size(50, 50)
});
infowindow.open(map, marker);
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map, marker);
});
}
</script>
</head>
<body onload="initialize();">
<form method="post" id="Form1">
<div id="map_canvas" style="width: 763px; height: 340px;padding-top:100px;">
</div>
</form>
</body>
</html>