Skip to content Skip to sidebar Skip to footer

Google Places Api- Address And Phone Number?

I'm trying to create a list and map via Google Places. The following displays makers and a list of local stores. Right now it only shows the name of the store. How would I include

Solution 1:

From the documentation, you can easily get a handle on the number and address of the place(s) you fetch by doing:

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

   google.maps.event.addListener(marker, 'click', function() {
    infowindow.setContent(place.name+place.formatted_address+place.formatted_phone_number);
    infowindow.open(map, this);
   });
   placesList.innerHTML += '<li>' + place.name + '</li>';
 }

Again from the docs:

The content of the InfoWindow may contain a string of text, a snippet of HTML, or a DOM element. To set the content, either specify it within the InfoWindowOptions or call setContent() on the InfoWindow explicitly.

EDIT- As rightly pointed out results for nearbySearch don't return formatted_address and formatted_phone_number in the result object and for that you'll have to make another getdetails request like you have already incorporated in your code. It might look something like (untested):

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

var request = { placeId: place.place_id };
service.getDetails(request, function(details, status) {
  google.maps.event.addListener(marker, 'click', function() {
    infowindow.setContent(details.name + "<br />" + details.formatted_address +"<br />" + details.website + "<br />" + details.rating + "<br />" + details.formatted_phone_number);
    infowindow.open(map, this);
   });
 });
 placesList.innerHTML += '<li>' + place.name + '</li>';
}

It's important that service is available to the scope of the functions you use it in. So declare it outside all the functions and initialize it appropriately like so:

var service; //declare globallyfunction intialize(){
   ...
   service = new google.maps.places.PlacesService(map); //initialize here

   ...
}

So you can format that content string accordingly and populate the infowindow. Hope it gets you started in the right direction.

Post a Comment for "Google Places Api- Address And Phone Number?"