2013-03-09 10 views
10

オートコンプリートを使用してプレースメント候補を取得しました。選択したい場所をクリックするとLatLngが抽出されます。これはplace.geometry.locationの下にあります。私の観察を1としてGoogleマップでLatLngの値を取得

Firebug Screenshot

、キーibjbは、セッションごとに変化し続けます。予測可能にLatLngを抽出する方法はありますか?

$(document).ready(function() { 

    var mapOptions = { 
     center : new google.maps.LatLng(-33.8688, 151.2195), 
     zoom : 13, 
     mapTypeId : google.maps.MapTypeId.ROADMAP 
    }; 
    var map = new google.maps.Map(document.getElementById('map'), mapOptions); 

    $('#searchTextField').bind('keydown keypress', function() { 
     setTimeout(function() { 
      var inputQuery = $('#searchTextField').val(); 

      if (inputQuery.length >= 2) { 
       //console.log(inputQuery); 

       /* 
       var service = new google.maps.places.AutocompleteService(); 

       service.getPlacePredictions({ 
        input : inputQuery 
       }, callback); 
       */ 

       var input = document.getElementById('searchTextField'); 
       var options = { 
        types : ['geocode'] 
       }; 

       var autocomplete = new google.maps.places.Autocomplete(input, options); 

       // Acting on Selecting a place 
       google.maps.event.addListener(autocomplete, 'place_changed', function() { 
        //infowindow.close(); 
        var place = autocomplete.getPlace(); 

        console.log(place); 
        console.log(place.formatted_address); 
        console.log(place.name); 
        console.log(place.geometry.location); 
        console.log(place.geometry.location[0]); 

        // Show the map to the current location selected 
        if (place.geometry.viewport) { 
         map.fitBounds(place.geometry.viewport); 
        } else { 
         map.setCenter(place.geometry.location); 
         map.setZoom(17); 
         // Why 17? Because it looks good. 
        } 

        var marker = new google.maps.Marker({ 
         position : place.geometry.location, 
         map : map, 
         draggable : true, 
        }); 

        $.each(place.geometry.location, function(key, value) { 
         console.log(key + ": " + value); 
        }); 
       }); 
      } 

     }, 0); 

    }); 
}); 

答えて

35

place.geometry.locationLatLngあるので、あなたはその.lat().lng()メソッドを呼び出すことができます。

var location = place.geometry.location; 
var lat = location.lat(); 
var lng = location.lng(); 

これらのメソッドがこのオブジェクトの検査に含まれていることがわかります。

あなたが発見したibjbのような文書化されていないプロパティを使用しないでください。 Googleは、Closure Compilerまたは同様のツールを使用してMaps APIをコンパイルします。これにより、プライベートプロパティと変数のランダムな名前が生成されます。

+1

ありがとうございました:D –

+1

私の喜びは、助けになってうれしいです。 –

+0

あまりにも私の問題への解決策を見つけることが幸せ.. – WorM

関連する問題