2012-02-03 126 views
0

使用PrimeFaces p:gmap組件。如何將地址解析器地址獲取到支持bean?

我有一個帶有一組標記的gmap組件的頁面。
我想在gmapInfoWindow中顯示街道地址,並將它傳遞給backing bean。 當我點擊地圖標記時,我調用一個javascript函數來獲取反向地理編碼器地址。 我可以獲取地址並將其顯示在JavaScript警告對話框中,但我無法將其填充到支持bean變量或info標記中。

變量確實被填充,但直到下次單擊標記時纔會更新。 因此,地址始終是一個標記點​​擊後面。

有誰知道我可以如何獲取地址在當前頁面會話期間更新? 謝謝。

MapMarkerSelect上的支持bean代碼只有一個System.out.println語句來顯示mapAddress變量。
這裏是頁面代碼:

   <h:form prependId="false" > 
        <p:gmap id="gmap" center="#{mappingSessionBean.mapCenter}" zoom="#{mappingSessionBean.mapZoom}" type="HYBRID" rendered="true" 
          style="#{mappingSessionBean.polygonGmapStyle}" onPointClick="handlePointClick(event);" 
          model="#{mappingSessionBean.mapModel}" fitBounds="#{mappingSessionBean.fitBoundsFlag}" 
          widgetVar="map" > 
         <p:ajax id="gmapAjax" event="overlaySelect" immediate="true" onstart="handlePointClick(event);" listener="#{mappingSessionBean.onMapMarkerSelect}" /> 
         <p:gmapInfoWindow id="infoWindow" > 
          <p:outputPanel > 
           <h:panelGrid columns="1" > 
            <h:outputText id="infoWindowTitle" value="#{mappingSessionBean.selectedMarker.title}" /> 
            <h:outputText id="infoWindowAddress" value="#{mappingSessionBean.mapAddress}" rendered="true" /> 
            <p:commandButton value="Zoom In" action="#{mappingSessionBean.selectedViewInfoListener}" update="gmap" /> 
           </h:panelGrid> 
          </p:outputPanel> 
         </p:gmapInfoWindow> 
        </p:gmap> 
        <h:inputHidden id="address" value="#{mappingSessionBean.mapAddress}" /> 
       </h:form > 

     <script type="text/javascript" > 
      function handlePointClick(event) { 
       if(navigator.geolocation) 
       { 
        browserSupportFlag = true; 
        var latlng = event.latLng; 
        geocoder = new google.maps.Geocoder(); 
        geocoder.geocode({'latLng': latlng}, function(results, status) 
        { 
         if(status == google.maps.GeocoderStatus.OK) 
         { 
          alert(results[0].formatted_address); 
          document.getElementById('address').value = results[0].formatted_address; 
          document.getElementById('infoWindowAddress').value = results[0].formatted_address; 
         } 
         else 
         { 
          alert("Geocoder failed due to: " + status); 
         } 
        }); 
       } 
       else 
       { 
        alert("No Geolocation"); 
       } 
      } 
     </script> 

回答

0

這裏是一個通用的解決方案,我想你可以做的更好,如果你會找到一種方式來觸發inputHidden事件,而無需使用按鈕

BTW :jQuery提供primefaces所以你可以使用它無需任何額外的包括

,而不是

<h:inputHidden id="address" value="#{mappingSessionBean.mapAddress}" /> 

<f:ajax listener="#{mappingSessionBean.myajax}" execute="address"> 
     <h:inputHidden id="address" value="#{mappingSessionBean.mapAddress}" /> 
     <h:commandButton id="addressBtn" style="display:none"/> 
    </f:ajax> 

(可以代替執行= 「address」 的執行= 「@表」)

和JS代碼替換

document.getElementById('address').value = results[0].formatted_address; 

jQuery("#address").val(results[0].formatted_address); 
jQuery("#addressBtn").click(); // this will trigger the ajax listener 

和最後在你的bean中添加ajax監聽器本身的實現

public void myajax(AjaxBehaviorEvent event) { 
    System.out.println(getMapAddress()); 
}