2011-10-11 28 views
1

我不知道爲什麼r1變量是未定義的。 的'的latLng':mEvent.latLng事情正在確定其他功能...谷歌地圖地理編碼在一個聽衆

<!-- API V3 --> <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 

....

.....

....

google.maps.event.addListener(map, 'click', function(mEvent) { 

      var geo1 = new google.maps.Geocoder(); 
      geo1.geocode({ 'latLng': mEvent.latLng }, function(results, status) { 
       if (status == google.maps.GeocoderStatus.OK) 
       { 
        var r1 = results[0].formatted_address; 
       } 
       else 
       { 
        var r1 = '?'; 
       } 
      }); 

      //do things with mEvent.latLng and r1... 
+0

「結果」在分配給「r1」之前的價值是多少?你可以console.log? – Kai

回答

1

變量r1很可能是未定義的,因爲它超出了範圍。你需要將它的聲明提高一點。例如: -

google.maps.event.addListener(map, 'click', function(mEvent) { 

     var geo1 = new google.maps.Geocoder(); 
     var r1; 
     geo1.geocode({ 'latLng': mEvent.latLng }, function(results, status) { 
      if (status == google.maps.GeocoderStatus.OK) 
      { 
       r1 = results[0].formatted_address; 
      } 
      else 
      { 
       r1 = '?'; 
      } 
     }); 

     //do things with mEvent.latLng and r1... 

如果仍然發現(在Firefox)的一些問題,使用Firebug的或內置在其他瀏覽器的調試器。你可以插入「調試器」;關鍵字在調試器處於活動狀態時在某行停止。然後您將能夠檢查可用的變量。