2012-11-20 15 views
40

Sto cercando di ottenere latitudine e longitudine dalla mappa google di completamento automatico senza visualizzare la mappa. Nel mio script il completamento automatico funziona bene ma non riesco ad ottenere la latitudine e la longitudine.Google map api ottiene Latitudine e longitudine dal completamento automatico senza mappa

<script type="text/javascript"> 
    function initialize() { 

var options = { 
    types: ['(cities)'] 
}; 

var input = document.getElementById('searchTextField'); 
var autocomplete = new google.maps.places.Autocomplete(input, options); 
} 
    google.maps.event.addDomListener(window, 'load', initialize); 

    var geocoder = new google.maps.Geocoder(); 
    var address = autocomplete; 

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

    if (status == google.maps.GeocoderStatus.OK) { 
    var latitude = results[0].geometry.location.lat(); 
    var longitude = results[0].geometry.location.lng(); 
    alert(latitude); 
    } 
}); 

</script> 

risposta

0

L'API di Google Places fornisce anche API REST, compreso il completamento automatico dei luoghi. https://developers.google.com/places/documentation/autocomplete

Ma i dati recuperati dal servizio devono essere utilizzati per una mappa.

+3

Non sembra essere il caso ora. I documenti dell'API di Google dicono: "È possibile utilizzare il completamento automatico del luogo anche senza una mappa.Se si mostra una mappa, deve essere una mappa di Google. Quando si visualizzano le previsioni dal servizio di completamento automatico senza una mappa, è necessario includere l'opzione dal logo di Google. " Questo è preso dalla documentazione dell'API: https://developers.google.com/places/documentation/autocomplete –

100

È possibile utilizzare il codice qui sotto.

<script src="http://maps.googleapis.com/maps/api/js?libraries=places" type="text/javascript"></script> 

<script type="text/javascript"> 
    function initialize() { 
     var input = document.getElementById('searchTextField'); 
     var autocomplete = new google.maps.places.Autocomplete(input); 
     google.maps.event.addListener(autocomplete, 'place_changed', function() { 
      var place = autocomplete.getPlace(); 
      document.getElementById('city2').value = place.name; 
      document.getElementById('cityLat').value = place.geometry.location.lat(); 
      document.getElementById('cityLng').value = place.geometry.location.lng(); 
      //alert("This function is working!"); 
      //alert(place.name); 
      // alert(place.address_components[0].long_name); 

     }); 
    } 
    google.maps.event.addDomListener(window, 'load', initialize); 
</script> 

... e questa parte è all'interno del vostro modulo:

<input id="searchTextField" type="text" size="50" placeholder="Enter a location" autocomplete="on" runat="server" /> 
<input type="hidden" id="city2" name="city2" /> 
<input type="hidden" id="cityLat" name="cityLat" /> 
<input type="hidden" id="cityLng" name="cityLng" /> 

spero che aiuta.

4

Non è possibile ottenere latitudine/longitudine dall'API di completamento automatico poiché i dati generati da google da quell'API non contengono il campo di latitudine e logitudine. Esempio: -

{ 
    "predictions" : [ 
     { 
     "description" : "IIT Mumbai, Mumbai, Maharashtra, India", 
     "id" : "ac3235cda973818a89b5fe21ad0f5261ac9b6723", 
     "matched_substrings" : [ 
      { 
       "length" : 10, 
       "offset" : 0 
      } 
     ], 
     "reference" : "CkQ0AAAAHWg-RSngiYHHdz_yqyFKmfSoBwT-_PW0k8qQDhsbiVrk7BlPHCyJb58OthledMUTGYS5Vhec1Zj2L7w9Rq0zDxIQrbn05RX1QgWHkSXsmTk1TRoU45APW2BBRIUsA3t6XBy_8s0BPPA", 
     "terms" : [ 
      { 
       "offset" : 0, 
       "value" : "IIT Mumbai" 
      }, 
      { 
       "offset" : 12, 
       "value" : "Mumbai" 
      }, 
      { 
       "offset" : 20, 
       "value" : "Maharashtra" 
      }, 
      { 
       "offset" : 33, 
       "value" : "India" 
      } 
     ], 
     "types" : [ "establishment", "geocode" ] 
     } 
    ], 
    "status" : "OK" 
} 

È possibile utilizzare l'API di Google per ottenere la longitudine e latitudine dal tuo indirizzo. Come hai già detto, dovresti implementare un campo nascosto in cui inserire il risultato. Quindi puoi salvare la posizione insieme alle coordinate.

es https://maps.googleapis.com/maps/api/geocode/json?address=IIT%20Area,%20Mumbai,%20Maharashtra,%20India&sensor=false

Recentemente ho implementato questa funzione in uno dei miei progetti:

function getLatLngFromAddress(city, country){ 

    var address = city +", "+ country; 
    var geocoder = new google.maps.Geocoder(); 

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

    if (status == google.maps.GeocoderStatus.OK) { 
     $('#latitude').val(results[0].geometry.location.Pa); 
     $('#longitude').val(results[0].geometry.location.Qa); 

    } else { 
     console.log("Geocode was not successful for the following reason: " + status); 
    } 
    }); 
} 
+1

Basta usare Inserisci dettagli per place_id si ottiene https://developers.google.com/places/documentation/details – CallMeLaNN

+1

place.geometry.location.lat() – 4ndro1d

0

È possibile ottenere dalla stessa api senza alcun api supplementare o chiamata url.

HTML

<input class="wd100" id="fromInput" type="text" name="grFrom" placeholder="From" required/> 

Javascript

var input = document.getElementById('fromInput'); 
var defaultBounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(-33.8902, 151.1759), 
    new google.maps.LatLng(-33.8474, 1512631) 
) 

var options = { 
bounds: defaultBounds 
} 

var autocomplete = new google.maps.places.Autocomplete(input, options); 
var searchBox = new google.maps.places.SearchBox(input, { 
     bounds: defaultBounds 
}); 


google.maps.event.addListener(searchBox, 'places_changed', function() { 
    var places = searchBox.getPlaces(); 


console.log(places[0].geometry.location.G); // Get Latitude 
console.log(places[0].geometry.location.K); // Get Longitude 

//Additional information 
console.log(places[0].formatted_address); // Formated Address of Place 
console.log(places[0].name); // Name of Place 






    if (places.length == 0) { 
     return; 
    } 

    var bounds = new google.maps.LatLngBounds(); 
    console.log(bounds); 
    }); 
    } 
2

Sì, è possibile:

var place = autocomplete.getPlace(); 

document.getElementById('lat').value = place.geometry.location.lat(); 
document.getElementById('lon').value = place.geometry.location.lng(); 
12

bisogno solo:

var place = autocomplete.getPlace(); 
// get lat 
var lat = place.geometry.location.lat(); 
// get lng 
var lng = place.geometry.location.lng(); 
+0

Thx mate. Funziona a meraviglia. Solo per segnalare che 'autocomplete' è l'oggetto creato da' new google.maps.places.Autocomplete (nativeElement', options) ' – dewd

-2
@Override 
public void onPlaceSelected(Place place) { 
    LatLng autoCompleteLatLng = place.getLatLng(); 
    newLoc = new Location("New"); 
    newLoc.setLatitude(autoCompleteLatLng.latitude); 
    newLoc.setLongitude(autoCompleteLatLng.longitude); 

    geoLocate(newLoc);//mark it in map 
} 
Problemi correlati