简体   繁体   English

Google Places API - 地点详情请求未定义

[英]Google Places API - Places detail request undefined

I'm trying to retrieve place details using the Google Places Library.我正在尝试使用 Google 地方信息库检索地点详细信息。 When you click the place marker I want to be able to see the address, name, website etc.当您单击地点标记时,我希望能够看到地址、名称、网站等。

I'm using the following tutorial: http://code.google.com/apis/maps/documentation/javascript/places.html我正在使用以下教程: http : //code.google.com/apis/maps/documentation/javascript/places.html

All seems to be going pretty well.一切似乎都很顺利。 In my marker I can display the name and the rating but the address, website and phone number all appear as 'undefined'.在我的标记中,我可以显示名称和评级,但地址、网站和电话号码都显示为“未定义”。 Is this because Google doesn't have the information I'm requesting on these places?这是因为 Google 没有我在这些地方请求的信息吗? Or have I done something wrong?还是我做错了什么?

Here's the code I'm using: var map;这是我正在使用的代码: var map; var infowindow; 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);
    });
  }

Any help or ideas will be greatly appreciated.任何帮助或想法将不胜感激。

Cheers.干杯。

JA JA

You're doing a Place Search, which doesn't return all of the fields that you're using:您正在执行地方搜索,它不会返回您正在使用的所有字段:

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

In order to get the address, website, etc, you'll also need to call place.getDetails() , passing the Place's reference .为了获取地址、网站等,您还需要调用place.getDetails() ,传递 Place 的reference See:看:

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

Roughly, your code would change to:粗略地说,您的代码将更改为:

  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);
      });
    });
  }

Based on Mikes response above I would suggest moving the call into the listener like below.根据上面 Mikes 的响应,我建议将呼叫移动到如下所示的侦听器中。 This way the call to the API is only made when a user clicks the marker instead of hundreds of times in cases where one renders a lot of markers.这样,只有在用户单击标记时才会调用 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);
    });
});

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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