2010-11-30 8 views
2

ご覧のとおり、アラートを送信する行があります。それは "undefined"です。一方、私はちょうどresults[0].geometry.locationに警告すると、(41.321, 41.556)を警告します。経度のための緯度とresults[0].geometry.location.lng()を得るために、私id_latとid_longsの一つに、私はちょうどそれを警告し、(intとして)それを設定したいこのGoogleマップのJavaScriptの緯度を警告するにはどうすればよいですか?

...

$("#geocodesubmit").click(function(){ 
     $("#id_lat").val(""); 
     $("#id_long").val(""); 
     var address = $("#addressinput").val(); 
     geocoder.geocode({ 'address': address}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
      $("#badlocation_holder").hide(); 
      $("#map_canvas").show(); 
      $("#map_canvas_holder").show().css("background-color", "#E6E6FA").animate({"background-color":"#f5f5f5"}, 800); 
      ; 
      var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
      map.setCenter(results[0].geometry.location); 

      alert(results[0].geometry.location[1]); //this is undefined. 

      $("#id_lat").val(results[0].geometry.location[0]); 
      $("#id_long").val(results[0].geometry.location[1]); 


      var marker = new google.maps.Marker({ 
       map: map, 
       position: results[0].geometry.location, 
       draggable:true 
      }); 
      } else { 
       $("#map_canvas_holder").hide(); 
       $("#badlocation_holder").show().css("background-color","#F08080").animate(
       {"background-color":"#f5f5f5"},800); 
      } 
     }); 
     return false; 
    }); 

答えて

30

使用results[0].geometry.location.lat()

+0

すべての回答がこの時点で正しいと思います。ありがとう – Daniel

5

If you look at the APIlocationは(つまり、ここでの主な問題です)配列ではない、それは財産ですので、.locationはそれにアクセスするための正しい方法ではない.location[0]です。

公式ドキュメントはbut here are updated docs on that location (a LatLng type)、特定のタイプのために見つけるのは少し難しいです:

toString()はアラート(lat, long)に取得している、.lat()はこのように、緯​​度と経度のため.lng()にもありますものです。

alert(results[0].geometry.location.lat()); 
alert(results[0].geometry.location.lng()); 
関連する問題