结合两个javascript函数一个接一个地运行?

时间:2011-01-30 00:30:33

标签: javascript

如何编辑以下代码以将函数initialize和codeAddress合并为一个?

{literal}
             var geocoder;
             var map;
             function initialize() {
               geocoder = new google.maps.Geocoder();
               var latlng = new google.maps.LatLng(-34.397, 150.644);
               var myOptions = {
                 zoom: 8,
                 center: latlng,
                 mapTypeId: google.maps.MapTypeId.ROADMAP
               }
               map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
             }

             function codeAddress() {
               var address = "{/literal}{$listing.City}, {$listing.State}{literal}";
               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);
                 }
               });
             }

        {/literal}
        </script>

提前致谢

3 个答案:

答案 0 :(得分:1)

我认为你只需要在initialize()函数中调用codeAddress()函数:

         function initialize() {
           geocoder = new google.maps.Geocoder();
           var latlng = new google.maps.LatLng(-34.397, 150.644);
           var myOptions = {
             zoom: 8,
             center: latlng,
             mapTypeId: google.maps.MapTypeId.ROADMAP
           }
           map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
           codeAddress();

答案 1 :(得分:1)

你有没有尝试过:

var both = function() {
    initialize();
    codeAddress();
};

答案 2 :(得分:1)

你是说这个吗?

         var geocoder;
         var map;
         function myNewFunction() {
           geocoder = new google.maps.Geocoder();
           var latlng = new google.maps.LatLng(-34.397, 150.644);
           var myOptions = {
             zoom: 8,
             center: latlng,
             mapTypeId: google.maps.MapTypeId.ROADMAP
           }
           map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
           var address = "{/literal}{$listing.City}, {$listing.State}{literal}";
           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);
             }
           });
         }