2011-01-30 94 views
0

如何編輯以下代碼以將函數初始化和codeAddress合併爲一個?結合兩個JavaScript函數一個接一個地運行?

{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> 

在此先感謝

回答

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); 
      } 
      }); 
     } 
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(); 
+0

也有作品。謝謝 – Anthony 2011-01-30 00:49:52

+0

感謝您的投票。 – 2011-01-30 01:00:29

1

你嘗試:

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

有點...我試過這也有效。謝謝 – Anthony 2011-01-30 00:48:33

相關問題