Google Places API - Places detail request undefined

前端 未结 2 1326
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-05 12:16

I\'m trying to retrieve place details using the Google Places Library. When you click the place marker I want to be able to see the address, name, website etc.

I\'m

相关标签:
2条回答
  • 2020-12-05 12:43

    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

    In order to get the address, website, etc, you'll also need to call place.getDetails(), passing the Place's reference. See:

    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);
          });
        });
      }
    
    0 讨论(0)
  • 2020-12-05 12:57

    Based on Mikes response above I would suggest moving the call into the listener like below. 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.

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

    }

    0 讨论(0)
提交回复
热议问题