谷歌地图JavaScript-地方详细信息结果

时间:2014-11-16 03:59:25

标签: javascript google-maps google-api libraries

在代码下方,place.name显示了通过地理位置找到的地点的名称。我应该可以使用place.formatted_address列出地址,但这不起作用。我错过了什么?

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>

1 个答案:

答案 0 :(得分:1)

您没有请求地方详细信息,您运行附近搜索

根据记录,search-response只会包含 textSearch 的<{1}} - 属性