Google Maps API Places Library SearchBox

后端 未结 2 1731
旧巷少年郎
旧巷少年郎 2021-01-27 09:22

I am attempting to add infowindows to the markers with PlaceDetails that are returned after a google searchbox search. When I click on the markers no infowindows open. I cannot

2条回答
  •  爱一瞬间的悲伤
    2021-01-27 10:09

    To get the Place Details to use in the InfoWindow, you need to make a second request to the Places API (when the marker is clicked). Inside the createMarker function (which has function closure on the "place"):

    var request =  {
       reference: place.reference
    };
    google.maps.event.addListener(marker,'click',function(){
       service.getDetails(request, function(place, status) {
          if (status == google.maps.places.PlacesServiceStatus.OK) {
              var contentStr = '
    '+place.name+'

    '+place.formatted_address; if (!!place.formatted_phone_number) contentStr += '
    '+place.formatted_phone_number; if (!!place.website) contentStr += '
    '+place.website+''; contentStr += '
    '+place.types+'

    '; infowindow.setContent(contentStr); infowindow.open(map,marker); } else { var contentStr = "
    No Result, status="+status+"
    "; infowindow.setContent(contentStr); infowindow.open(map,marker); } }); });

    Example

提交回复
热议问题