0

I'm building an mobile web app that allows you to record behavior at a given location. In effort to make entry easier for the user, I'm wanting to use Google Places to generate a quick list of nearby businesses into a form.

I've read documentation here: Using Google Places API in Android but most of what I can find here and elsewhere is specific to Android and not HTML/JS

Any help would be much appreciated.

Community
  • 1
  • 1
Jeremy P. Beasley
  • 679
  • 1
  • 7
  • 22

1 Answers1

0

You need to have the places library loaded (it's an extra parameter to the normal call to google.maps.api library).

<script src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=visualization,drawing,places"></script>

Then create a PlacesService with:

var PlacesService = new google.maps.places.PlacesService(map);

There are many ways to search for places. In this example I'll search within my current map bounds

PlacesService.nearbySearch({
    bounds: map.getBounds(),
    //types: ['lawyer']
}, function(results, status, pagination) {
    if (status == google.maps.places.PlacesServiceStatus.OK) {
        for(var i=0;i<results.length;i++) {
                place=results[i];
                var marker=new google.maps.Marker({position: place.geometry.location, icon:place.icon, map:map});
            }
        if (pagination.hasNextPage) {
            // in case you get more markers to fetch
            pagination.nextPage();
        }
    } else {
        console.log(results, status);
    }
});
ffflabs
  • 17,166
  • 5
  • 51
  • 77