문제

I want to pass the geocoder address from Google Map to ManagedBean, but I have to select twice on the map, I think because the method (onPointSelect) is executed before the address is passed to the ManagedBean

ManagedBean :

private String mapAddress;

public void onPointSelect(PointSelectEvent event) {

        LatLng latlng = event.getLatLng();
        setMapAddress(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("myForm:address"));
        addMessage(new FacesMessage(FacesMessage.SEVERITY_INFO, "Point Selected", "Lat:" + latlng.getLat() + ", Lng:" + latlng.getLng() + " Address: " + getMapAddress()));    
    }

XHTML :

       <h:form id="myForm" >
        <p:growl id="messages" showDetail="true" life="2000"  />

        <p:gmap center="41.381542, 2.122893" zoom="15" type="ROADMAP" widgetVar="map"  style="width:600px;height:400px" onPointClick="handlePointClick(event);" >

            <p:ajax event="stateChange"  listener="#{test.onStateChange}"  />

            <p:ajax event="pointSelect"  listener="#{test.onPointSelect}"     update="messages" />

        </p:gmap> 
        <h:inputHidden  id="address" value="#{test.mapAddress}"/>
    </h:form>

    <script type="text/javascript" >
        //var ma = document.getElementById('myForm:address');
        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('myForm:address').value = results[0].formatted_address;

                        //jQuery("#address").val(results[0].formatted_address);
                        //jQuery("#addressBtn").click();
                        //alert( document.getElementById('myForm:address').value ); 

                    }
                });
            }
        }

    </script>
도움이 되었습니까?

해결책

code after modification :

private String mapAddress;
private double lat;
private double lng;

public void onPointSelect(ActionEvent event) {
        addMessage(new FacesMessage(FacesMessage.SEVERITY_INFO, "Point Selected", "Lat:" + getLat() + ", Lng:" + getLng() + " Address: " + getMapAddress()));
    }

XHTML :

<h:form  prependId="false">
            <p:growl id="messages" showDetail="true" life="2000"  />

            <p:gmap center="41.381542, 2.122893" id="map" zoom="15" type="ROADMAP" widgetVar="map"  style="width:600px;height:400px" onPointClick="handlePointClick(event);"  >

            </p:gmap> 
            <p:remoteCommand name="onPoint" actionListener="#{test.onPointSelect}" update="messages" />
            <h:inputHidden  id="address"  value="#{test.mapAddress}" >

            </h:inputHidden>
            <h:inputHidden id="lat" value="#{test.lat}" />
            <h:inputHidden id="lng" value="#{test.lng}" />

        </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)
                        {
onPoint([{name: 'address', value:results[0].formatted_address}, {name: 'lng', value:event.latLng.lng()}, {name: 'lat', value: event.latLng.lat()}]);

                        }
                    });
                }
            }
        </script>

다른 팁

You don't need to attach the <p:ajax>Element to the map. You could use:

 <p:remoteCommand name="onPoint" actionListener="#{test.onPointSelect}" update="messages" />

Must be embedded in <h:form>. You can trigger this Ajax Request in your JavaScript function the following way:

onPoint([{name:'latitude',value:latitude},{name:'longitude',value:longitude}]);

You can pass parameters from JavaScript to backing bean by key/value pairs and receive theese parameters in the backing bean with the help of the FacesContext:

    public void onPointSelect(PointSelectEvent event) {
        Map<String,String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        Double latitude = Double.parseDouble(params.get("latitude"));
        Double longitude = Double.parseDouble(params.get("longitude"));
            //etc.
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top