I'm trying to use the Places library in the Google Maps JavaScript API to get a list of suggestions for pleting a partial UK address. Here's a demo of my progress to date. The code that calls the Google Places API is:
var service = new google.maps.places.AutopleteService();
var query = document.getElementById("address").value;
var restrictions = {country: 'uk'};
service.getPlacePredictions({
input: query,
types: ['address'],
ponentRestrictions: restrictions}, displaySuggestions);
If I submit a partial address such as "Lowther Road", I get back suggestions such as:
- Lowther Road, Bournemouth, UK
- Lowther Road, Dunstable, UK
- Lowther Road, Stanmore, UK
My problem is that I want a list of matching addresses, not places. Even if I include the house number, e.g. by submitting the partial address "79 Lowther Road", the suggestions returned are still not plete addresses, because the postcodes are missing
- 79 Lowther Road, Bournemouth, UK
- 79 Lowther Road, Dunstable, UK
- 79 Lowther Road, Stanmore, UK
Is it possible to submit a partial address to Google, and get a list of matching (plete) addresses?
I'm trying to use the Places library in the Google Maps JavaScript API to get a list of suggestions for pleting a partial UK address. Here's a demo of my progress to date. The code that calls the Google Places API is:
var service = new google.maps.places.AutopleteService();
var query = document.getElementById("address").value;
var restrictions = {country: 'uk'};
service.getPlacePredictions({
input: query,
types: ['address'],
ponentRestrictions: restrictions}, displaySuggestions);
If I submit a partial address such as "Lowther Road", I get back suggestions such as:
- Lowther Road, Bournemouth, UK
- Lowther Road, Dunstable, UK
- Lowther Road, Stanmore, UK
My problem is that I want a list of matching addresses, not places. Even if I include the house number, e.g. by submitting the partial address "79 Lowther Road", the suggestions returned are still not plete addresses, because the postcodes are missing
- 79 Lowther Road, Bournemouth, UK
- 79 Lowther Road, Dunstable, UK
- 79 Lowther Road, Stanmore, UK
Is it possible to submit a partial address to Google, and get a list of matching (plete) addresses?
Share Improve this question edited Jun 24, 2018 at 18:34 xomena 32.3k6 gold badges96 silver badges125 bronze badges asked Apr 10, 2018 at 16:24 DónalDónal 188k177 gold badges586 silver badges844 bronze badges1 Answer
Reset to default 6You can get plete addresses if you execute a place details request for each prediction that you got via AutopleteService. You should create an instance of the google.maps.places.PlacesService
and execute getDetails()
for each place ID that you have in the predictions.
Note that get details works asynchronously, so you should use Promises to get all details and display them in the <ul>
list.
Have a look at this sample that I created based on your code
// This example retrieves autoplete predictions programmatically from the
// autoplete service, and displays them as an HTML list.
// This example requires the Places library. Include the libraries=places
// parameter when you first load the API. For example:
// <script src="https://maps.googleapis./maps/api/js?key=YOUR_API_KEY&libraries=places">
function search() {
var displaySuggestions = function(predictions, status) {
if (status != google.maps.places.PlacesServiceStatus.OK) {
alert(status);
return;
}
document.getElementById("results").innerHTML = '';
var arrPr = [];
predictions.forEach(function (prediction) {
arrPr.push(getPredictionDetails(prediction.place_id));
});
Promise.all(arrPr).then(function(results) {
results.forEach(function(result) {
//console.info('Result: ' + JSON.stringify(result, 4));
var li = document.createElement('li');
li.appendChild(document.createTextNode(result.formatted_address));
document.getElementById('results').appendChild(li);
});
}).catch(err => {
console.log(err);
});
};
function getPredictionDetails(placeId) {
let promise = new Promise( (resolve, reject) => {
placeService.getDetails({
placeId: placeId
}, function(result, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
resolve(result);
} else {
reject(status);
}
});
});
return promise;
}
var query = document.getElementById("address").value;
var restrictions = {country: 'uk'};
var service = new google.maps.places.AutopleteService();
var placeService = new google.maps.places.PlacesService(document.getElementById("results"));
service.getPlacePredictions({ input: query, types: ['address'], ponentRestrictions: restrictions }, displaySuggestions);
}
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#right-panel {
font-family: 'Roboto','sans-serif';
line-height: 30px;
padding-left: 10px;
}
#right-panel select, #right-panel input {
font-size: 15px;
}
#right-panel select {
width: 100%;
}
#right-panel i {
font-size: 12px;
}
<div id="right-panel">
<input id="address" placeholder="UK address" value="79 Lowther Road"/>
<button type="button" onclick="search()">Submit</button>
<p>Results:</p>
<ul id="results"></ul>
</div>
<!-- Replace the value of the key parameter with your own API key. -->
<script type="text/javascript" src="https://maps.googleapis./maps/api/js?key=AIzaSyDztlrk_3CnzGHo7CFvLFqE_2bUKEq1JEU&libraries=places"
async defer></script>
You can also check this example at https://jsfiddle/xomena/z9tndphr/2/
I hope this helps!