简体   繁体   中英

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM