问题
I'm using the google.maps.places.AutocompleteService
to get suggestions for a places search, but I can't geocode some of the predictions.
An example of this: When I search for 'storms river mouth', one of the predictions I get back is 'Storms River Mouth Rest Camp, South Africa', but this address cannot be geocoded to get the lattude/longitude, eg: http://maps.googleapis.com/maps/api/geocode/json?address=Storms%20River%20Mouth%20Rest%20Camp,%20South%20Africa&sensor=true
Is there any way to get lattitude/longitude values for the autocomplete predictions?
Alternatively, I don't understand why google autocomplete is returning predictions that I cannot geocode.
Here is a basic example of the logic and code i'm working with:
var geocoder = new google.maps.Geocoder();
var service = new google.maps.places.AutocompleteService(null, {
types: ['geocode']
});
service.getQueryPredictions({ input: query }, function(predictions, status) {
// Show the predictions in the UI
showInAutoComplete(predictions);
};
// When the user selects an address from the autcomplete list
function onSelectAddress(address) {
geocoder.geocode({ address: address }, function(results, status) {
if (status !== google.maps.GeocoderStatus.OK) {
// This shouldn't never happen, but it does
window.alert('Location was not found.');
}
// Now I can get the location of the address from the results
// eg: results[0].geometry.location
});
}
[edit] - View a working example here: http://demos.badsyntax.co/places-search-bootstrap/example.html
回答1:
Use getPlacePredictions()
instead of getQueryPredictions()
. This will return a reference
for the place, which you can use to retrieve details by using placesService.getDetails()
. The details will contain the geometry for the place.
Note: placesService is a google.maps.places.PlacesService-object.
回答2:
Predictions returned by AutocompleteService has a PlaceId property. You can pass PlaceId instead of address to the geocoder according to documentation https://developers.google.com/maps/documentation/javascript/geocoding.
var service = new google.maps.places.AutocompleteService();
var request = { input: 'storms river mouth' };
service.getPlacePredictions(request, function (predictions, status) {
if(status=='OK'){
geocoder.geocode({
'placeId': predictions[0].place_id
},
function(responses, status) {
if (status == 'OK') {
var lat = responses[0].geometry.location.lat();
var lng = responses[0].geometry.location.lng();
console.log(lat, lng);
}
});
}
});
回答3:
Try this:
function onSelectAddress(address, callback) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
callback(results[0].geometry.location);
} else {
alert("Can't find address: " + status);
callback(null);
}
});
}
Then, the call and the callback:
onSelectAddress('your address here', function(location){
//Do something with location
if (location)
alert(location);
});
Sorry for my english. I got a question for you: Can you show me the method showInAutoComplete() ??? I'm showing the predictions on a list of href, but I don't know how save the 'clicked' address value.
回答4:
Here is the code I have written that is inspired from @Dr.Molle
function initializePlaces(q) {
googleAutocompleteService = new google.maps.places.AutocompleteService();
if(q){
googleAutocompleteService.getPlacePredictions({
input: q
}, callbackPlaces);
}else { //no value entered loop }
}
function callbackPlaces(predictions, status) {
if (status != google.maps.places.PlacesServiceStatus.OK) {
alert(status);
return;
}
for (var i = 0; i < predictions.length; i++) {
googlePlacesService = new google.maps.places.PlacesService(document.getElementById("q"));
googlePlacesService.getDetails({
reference: predictions[i].reference
}, function(details, status){
if(details){
console.log(details.geometry.location.toString());
}
});
console.log(predictions[i].description);
}
};
google.maps.event.addDomListener(window, 'load', initializePlaces);
$(document).on('keyup', 'input#q', function(e){
initializePlaces($(this).val());
});
Issue I see is a new PlacesService object on every key press that might be an overkill - I don't know the work around although.
Posting it here in-case someone is looking for it.
回答5:
If you need to return all results at once in proper order, use this:
var service = new google.maps.places.AutocompleteService();
service.getPlacePredictions({
input: '*** YOUR QUERY ***'
}, function(predictions, status) {
var data = [];
if (status != google.maps.places.PlacesServiceStatus.OK) {
console.error(status);
processResults(data);
return;
}
var s = new google.maps.places.PlacesService(document.createElement('span'));
var l = predictions.length;
for (var i = 0, prediction; prediction = predictions[i]; i++) {
(function(i) {
s.getDetails(
{
reference: prediction.reference
},
function (details, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
data[i] = details;
} else {
data[i] = null;
}
if (data.length == l) {
processResults(data);
}
}
);
})(i);
} });
function processResults(data) {
console.log(data);
}
回答6:
Maybe this could could help you.
var autocomplete = new google.maps.places.Autocomplete(this.input);
//this.input is the node the AutocompleteService bindTo
autocomplete.bindTo('bounds', this.map);
//this.map is the map which has been instantiated
google.maps.event.addListener(autocomplete, 'place_changed', function() {
var place = autocomplete.getPlace();
//then get lattude and longitude
console.log(place.geometry.location.lat());
console.log(place.geometry.location.lng());
}
来源:https://stackoverflow.com/questions/14414445/google-maps-api-v3-cant-geocode-autocompleteservice-predictions