【问题标题】:How can I get geocoder address to backing bean?如何获取支持 bean 的地理编码器地址?
【发布时间】:2012-02-26 12:57:27
【问题描述】:

使用 PrimeFaces p:gmap 组件。

我有一个带有带有一组标记的 gmap 组件的页面。
我想在 gmapInfoWindow 中显示街道地址并将其传递给支持 bean。 当我点击地图标记时,我调用一个 javascript 函数来获取反向地理编码器地址。 我可以获取地址并将其显示在 javascript 警报对话框中,但我无法让它填充支持 bean 变量或信息标记。

变量确实被填充了,但直到我下次单击标记时才会更新。 因此,地址总是落后一个标记。

有谁知道我如何在当前页面会话期间获取要更新的地址? 谢谢。

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>

【问题讨论】:

    标签: jsf-2 primefaces


    【解决方案1】:

    这是一个通用的解决方案,如果你能找到一种不使用按钮在 inputHidden 上触发事件的方法,我想你会做得更好

    b.t.w : 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>
    

    (您可以将 execute="address" 替换为 execute="@form")

    并在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());
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-16
      • 1970-01-01
      相关资源
      最近更新 更多