我在使用地理编码和clientscript.registerstartupscript让我的谷歌地图正常工作时遇到了麻烦。我知道我的google 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();");地图必须初始化,因为它显示了空的google地图shell,但没有填充。
Firebug在页面底部显示以下内容:<script type="text/javascript"> codeAddress(); </script>
发布于 2013-07-04 12:56:27
我已经使用下面的代码实现了这一点。注意:变量message & locationList的值是从代码后台获取的,使用公共变量并在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>https://stackoverflow.com/questions/17461480
复制相似问题