Benjamin Jones
Benjamin Jones

Reputation: 997

Google Maps JavaScript-Place Details Results

Below code, place.name shows the name of the place found with geolocation. I should be able to use place.formatted_address to list the address, however this is not working. What am I missing?

https://developers.google.com/maps/documentation/javascript/places?csw=1#place_details_requests

 <script src="https://maps.googleapis.com/maps/api/js?libraries=places"></script>
    <script>

var map, placesList;

function initialize() {
  var pyrmont = new google.maps.LatLng(40.062664, -83.069101);

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

  var request = {
    location: pyrmont,
    radius: 500,
    types: ['store']
  };

  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,
      phone:place.formatted_address,
      position: place.geometry.location
    });

    placesList.innerHTML += '<li>' + place.name + '</li>';
        placesList.innerHTML += '<li>' + place.formatted_address + '</li>';
    bounds.extend(place.geometry.location);
  }
  map.fitBounds(bounds);
}

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

    </script>

Upvotes: 0

Views: 189

Answers (1)

Dr.Molle
Dr.Molle

Reputation: 117334

You don't request the Place-details, you run a nearbySearch.

As documented a search-response will only contain the formatted_address-property for a textSearch

Upvotes: 1

Related Questions