adamb
adamb

Reputation: 427

Google Places API - Get address, phone number etc

I am trying to get further information for places which I am showing on a map, such as the place address, phone number etc. You can see my code here:

http://jsfiddle.net/aboother/4q3jcg1s/

I have done a 'console_log(place)' as below:

function createMarker(place) {
    var placeLoc = place.geometry.location;
    var marker = new google.maps.Marker({
        map: map,
        position: place.geometry.location
    });

    console.log(place);

    var request = { reference: place.reference };
    service.getDetails(request, function(details, status) {
        google.maps.event.addListener(marker, 'click', function() {
            console.log(place);
            infowindow.setContent(place.name + "<br />" + place.vicinity);
            infowindow.open(map, this);
        });
    });
}

but I can't see the address or phone number in this object even though I know you can get this information. I can only see 'vicinity', 'name' and other basic information. Can anyone help?

Upvotes: 2

Views: 2867

Answers (1)

user4765675
user4765675

Reputation:

Well you used the service: service.nearbySearch() to search nearby based on some criteria. Similarly there is the following service: service.getDetails() to get all the perspective details. I am not exactly sure but from my understanding the service uses the placeID to match and get all the details pertaining to that location.

In regards to your code, instead of directly creating a marker for each place, pass it to the getDetails() service and then create a marker. I logged the place and all the details are there, you use the data to your needs.

Below is the modified code:

function callback(results, status) {
    if(status == google.maps.places.PlacesServiceStatus.OK) {
        for(var i = 0; i < results.length; i++) {            
            service.getDetails(results[i], function(place, status) {
                if (status == google.maps.places.PlacesServiceStatus.OK) {
                    var marker = new google.maps.Marker({
                        map: map,
                        position: place.geometry.location
                    });
                    console.log(place);
                    google.maps.event.addListener(marker, 'click', function() {
                        infowindow.setContent(place.name);
                        infowindow.open(map, this);
                    });
                }    
            });
        }
    }
}

Here is the fiddle: Use whichever console you used to expand each location and you will see all their data, from phone numbers, to their review: http://jsfiddle.net/r61tqtxw/1/

Upvotes: 3

Related Questions