2011-05-25 183 views
5

我試圖實現谷歌地圖和我遇到的問題是,當我調用函數getLatLng,它返回一個未定義的值,我不明白爲什麼。JavaScript函數返回undefined

initialize(); 

    var map; 
    var geocoder; 

    function initialize() { 

     geocoder = new google.maps.Geocoder(); 
     var address = "Rochester, MN"; 
     var myLatLng = getLatLng(address); 
     console.log("myLatLng = "+myLatLng); 

    } 

    function getLatLng(address) { 

     var codedAddress; 

     geocoder.geocode({'address': address}, function(results, status) { 

      if(status == google.maps.GeocoderStatus.OK) { 
       codedAddress = results[0].geometry.location; 
       console.log("codedAddress 1 = "+codedAddress); 
      } else { 
       alert("There was a problem with the map"); 
      } 
      console.log("codedAddress 2 = "+codedAddress); 
     }); 

     console.log("codedAddress 3 = "+codedAddress); 
     return codedAddress; 
    } 

在Firebug控制檯,這裏是我得到這個確切順序輸出:

codedAddress 3 = undefined 
myLatLng = undefined 
codedAddress 1 = (44.0216306, -92.46989919999999) 
codedAddress 2 = (44.0216306, -92.46989919999999) 

爲什麼codedAddress 3和myLatLng首先顯示在控制檯中了?

回答

1

這是我得到了什麼通過谷歌地圖文檔的工作:

var geocoder; 
    var map; 

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

    function codeAddress(address) { 
    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); 
     } 
    }); 
    } 

Here's a jsFiddle to see it in action.很明顯,你可以更新這個使用jQue如果你需要的話。

1

您錯過了初始化函數的關閉}

+0

不,我實際上有一個關閉括號,但我只是遺漏了一些其他的東西,忘了複製那個括號。 – Catfish 2011-05-25 02:53:12

6

geocode是異步的(即it sends a request to Google's servers),所以你需要一個回調函數傳遞給getLatLng,而不是立即返回:

function getLatLng(address, callback) { 
    var codedAddress; 

    geocoder.geocode({'address': address}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
      codedAddress = results[0].geometry.location; 
      console.log("codedAddress 1 = "+codedAddress); 
     } else { 
      alert("There was a problem with the map"); 
     } 
     console.log("codedAddress 2 = "+codedAddress); 

     callback(codedAddress); 
    }); 
} 
+0

這可能是一個愚蠢的問題,但我已經添加了函數參數的回調函數,並在geocode語句的末尾顯示,但現在當我調用getLatLng時,像這樣:var myLatLng = getLatLng(address,回調);'我在控制檯沒有任何東西,沒有地圖顯示。 – Catfish 2011-05-25 03:09:15

+1

你傳遞了​​什麼回調函數?它需要具有'var myLatLng = getLatLng(地址,函數(codedAddress){/ *做某事* /})的形式'' – Emmett 2011-05-25 03:11:22

+0

我不確定我理解。在「做某件事」中需要做什麼?我只是想把getLatLng的值賦給一個變量。 – Catfish 2011-05-25 03:14:08