Jack
Jack

Reputation: 1

Google Place API- Place search pagination

I need your help regarding google place api, I get sample code in here: https://developers.google.com/maps/documentation/javascript/examples/place-search-pagination

Which show only place name (place.name), but along with place name I also want place formatted_address, formatted_phone_number. https://developers.google.com/places/documentation/details

But when I add (place.formatted_address) it return undefined.

I am not familiar with java-script well, help me to show address details with place name in same script:

developers.google.com/maps/documentation/javascript/examples/place-search-pagination

<code>
<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
      html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
    </style>
    <title>Place search pagination</title>
    <script src="https://maps.googleapis.com/maps/api/js?libraries=places"></script>
    <script>
var map, placesList;

function initialize() {
  var pyrmont = new google.maps.LatLng(23.765698, 90.357581);

  map = new google.maps.Map(document.getElementById('map-canvas'), {
    center: pyrmont,
    zoom: 17
  });

  var request = {
    location: pyrmont,
    radius: 500000,
    types: ['bank']
  };

  placesList = document.getElementById('places');

  var service = new google.maps.places.PlacesService(map);
  service.nearbySearch(request, callback);
}

function callback(results, status, pagination) {
  if (status != google.maps.places.PlacesServiceStatus.OK) {
    return;
  } else {
    createMarkers(results);

    if (pagination.hasNextPage) {
      var moreButton = document.getElementById('more');

      moreButton.disabled = false;

      google.maps.event.addDomListenerOnce(moreButton, 'click',
          function() {
        moreButton.disabled = true;
        pagination.nextPage();
      });
    }
  }
}

function createMarkers(places) {
  var bounds = new google.maps.LatLngBounds();

  for (var i = 0, place; place = places[i]; i++) {
    var image = {
      url: place.icon,
      size: new google.maps.Size(71, 71),
      origin: new google.maps.Point(0, 0),
      anchor: new google.maps.Point(17, 34),
      scaledSize: new google.maps.Size(25, 25)
    };

    var marker = new google.maps.Marker({
      map: map,
      icon: image,
      title: place.name,
      position: place.geometry.location
    });

    placesList.innerHTML += '<tr><td>' + place.geometry.location + '</td><td>' + place.name + '</td><td>' + place.formatted_address + '</td></tr>';

    bounds.extend(place.geometry.location);
  }
  map.fitBounds(bounds);
}

google.maps.event.addDomListener(window, 'load', initialize);

    </script>
    <style>
      #results {
        font-family: Arial, Helvetica, sans-serif;
        position: absolute;
        right: 5px;
        top: 0%;
        margin-top: 15px;
        height: 580px;
        width: 500px;
        padding: 5px;
        z-index: 5;
        border: 1px solid #999;
        background: #fff;
        overflow-y: scroll;
      }
      h2 {
        font-size: 22px;
        margin: 0 0 5px 0;
      }
      table {
        list-style-type: none;
        padding: 0;
        margin: 0;
        width: 100%;
      }
      tr {
        background-color: #f1f1f1;
        padding: 10px;
        text-overflow: ellipsis;
        white-space: nowrap;
        overflow: hidden;
      }
      li:nth-child(odd) {
        background-color: #fcfcfc;
      }
      #more {
        width: 100%;
        margin: 5px 0 0 0;
      }
    </style>
  </head>
  <body>
    <div id="map-canvas"></div>
    <div id="results">
      <h2>Results</h2>
      <table id="places"></table>
      <button id="more">More results</button>
    </div>
  </body>
</html>

</code>

Upvotes: 0

Views: 2015

Answers (1)

Scott
Scott

Reputation: 3732

The Details API is different from the Places API.

First you need to get the Place_id for your location Second you use that Place_id in the Details API to get your formatted_address

You might also try using "vicinity" which may also show the mailing address, albeit without the postal code.

Upvotes: 0

Related Questions