Google Places API-未定义Places详细信息请求

Google Places API - Places detail request undefined

本文关键字:Places 请求 详细信息 API- Google 未定义      更新时间:2023-09-26

我正在尝试使用Google Places Library检索地点的详细信息。当你点击位置标记时,我希望能够看到地址、名称、网站等。

我正在使用以下教程:http://code.google.com/apis/maps/documentation/javascript/places.html

一切似乎都很顺利。在我的标记中,我可以显示姓名和评分,但地址、网站和电话号码都显示为"未定义"。这是因为谷歌没有我要求的这些地方的信息吗?或者我做错了什么?

以下是我正在使用的代码:var映射;var信息窗口;

  function initialize() {
    var pyrmont = new google.maps.LatLng(-33.8665433, 151.1956316);
    map = new google.maps.Map(document.getElementById('map_canvas'), {
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      center: pyrmont,
      zoom: 15
    });
    var request = {
      location: pyrmont,
      radius: 5000,
      types: ['bar']
    };
    infowindow = new google.maps.InfoWindow();
    service = new google.maps.places.PlacesService(map);
    service.search(request, callback);
  }
  function callback(results, status) {
    if (status == google.maps.places.PlacesServiceStatus.OK) {
      for (var i = 0; i < results.length; i++) {
        createMarker(results[i]);
      }
    }
  }
  function createMarker(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 + "<br />" + place.formatted_address +"<br />" + place.website + "<br />" + place.rating + "<br />" + place.formatted_phone_number);
      infowindow.open(map, this);
    });
  }

任何帮助或想法都将不胜感激。

干杯。

JA

您正在进行位置搜索,它不会返回您正在使用的所有字段:

http://code.google.com/apis/maps/documentation/javascript/places.html#place_search_responses

为了获得地址、网站等,您还需要致电place.getDetails(),通过Place的reference。参见:

http://code.google.com/apis/maps/documentation/javascript/places.html#place_details_requests

大致上,您的代码将更改为:

  function createMarker(place) {
    var placeLoc = place.geometry.location;
    var marker = new google.maps.Marker({
      map: map,
      position: place.geometry.location
    });
    var request = { reference: place.reference };
    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);
      });
    });
  }

基于上面的Mikes响应,我建议将调用移动到侦听器中,如下所示。通过这种方式,只有当用户单击标记时才会调用API,而不是在渲染大量标记的情况下单击数百次。

function createMarker(place) {
    var placeLoc = place.geometry.location;
    var marker = new google.maps.Marker({
      map: map,
      position: place.geometry.location
    });
    var request = { reference: place.reference };
    google.maps.event.addListener(marker, 'click', function() {
      service.getDetails(request, function(details, status) {
      infowindow.setContent(details.name + "<br />" + details.formatted_address +"<br />" + details.website + "<br />" + details.rating + "<br />" + details.formatted_phone_number);
      infowindow.open(map, this);
    });
});

}