How to customise Google Location Suggest Api to get list of nearby places in Javascript

Posted By : Deepak Agrawal | 30-Oct-2014

Google location suggest api suggest nearBy locations names but we do not get access list of these places. Below code helps to get list of such places.

Sometimes we need to get access near by location to show data according to user location. Then this is the best way to get user location details.

 

Below code find top 5 nearest places to you for the search term 'oodles':

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=places&v=3.exp"></script>
<script>

var service = new google.maps.places.AutocompleteService();
var search="Oodles"

service.getPlacePredictions({
    input: search,
        offset: search.length
}, function listentoresult(list, status) {
        if (status != google.maps.places.PlacesServiceStatus.OK || list==null ) {
            return;
    }

         var results = document.getElementById('results');

    for (var i = 0, prediction; prediction = list[i]; i++) {
            results.innerHTML += '<li>' + prediction.description + '</li>';
      }
});

</script>

</head>
<body>
  <p>Query suggestions for 'Oodles':</p>
  <ul id="results"></ul>
</body>
</html>

 

Query suggestions for 'Oodles':

Thank you

About Author

Author Image
Deepak Agrawal

Deepak is an bright developer with experience in Groovy and Grails. He is well versed with AngularJS.

Request for Proposal

Name is required

Comment is required

Sending message..