0

我无法使用地理编码和 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();");

地图必须初始化,因为它显示了空的谷歌地图外壳,但它没有填充。

Firebug 在页面底部显示: <script type="text/javascript"> codeAddress(); </script>

4

1 回答 1

0

我已经使用下面的代码实现了这一点。注意:变量 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>
于 2013-07-04T04:56:27.043 回答