2013-03-09 3 views
10

Sto utilizzando il metodo di completamento automatico per inserire i suggerimenti e quando faccio clic sul luogo che desidero scegliere, voglio estrarre lo Lat e lo Lng che si trova sotto place.geometry.location come di seguito.Google Maps ottiene il valore LatLng

Firebug Screenshot

Come per la mia osservazione, i tasti ib e jb continuare a cambiare con ogni sessione. C'è un modo per estrarre il Lat e Lng in modo prevedibile?

$(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); 

    }); 
}); 

risposta

35

place.geometry.location è un LatLng, in modo da poter chiamare i suoi .lat() e .lng() metodi.

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

Vedrai che questi metodi sono presenti nell'ispezione di questo oggetto.

Non utilizzare mai proprietà non documentate come ib e jb scoperte. Google compila l'API di Maps utilizzando lo Closure Compiler o uno strumento simile, che genera nomi casuali per proprietà e variabili private.

+1

grazie mille: D –

+1

Piacere mio, sono contento che sia stato utile. –

+0

felice di trovare una soluzione anche al mio problema .. – WorM