Primefaces geocode从支持bean获取0.0,0.0 gps坐标

时间:2017-02-04 16:08:41

标签: java spring google-maps jsf primefaces

我想获得地理编码服务使用primefaces地理编码生成的点的经度和经度,我被卡住了。但是,当我试图从支持bean获取这些坐标时,我正在接收0.0值。

如何在有适当值的时间(登录到控制台后立即)访问它们,而不是零?

请注意我是Spring和PF的新手。

这是我的primefaces前端,我想要访问支持bean类的适当变量值:

<script type="text/javascript">
    function geocode() {
        PF('geoMap').geocode(document.getElementById('address').value);
    }
</script>

<h:form prependId="false">
    <h3 style="margin-top:0">Geocode</h3>
    <h:panelGrid columns="3" style="margin-bottom:10px" cellpadding="5">
        <p:outputLabel for="address" value="Address:" />
        <p:inputText id="address" />
        <p:commandButton value="Geocode" icon="ui-icon-search" onclick="geocode()" type="button" />
    </h:panelGrid>

    <p:gmap id="geoGmap" widgetVar="geoMap" center="#{geocodeView.centerGeoMap}" zoom="2" type="ROADMAP" model="#{geocodeView.geoModel}" style="width:100%;height:400px">
        <p:ajax event="geocode" listener="#{geocodeView.onGeocode}" update="@this" />
    </p:gmap>

    <!-- Here for example I want to show this values -->
    <p:commandButton value="Show values" action="" update="display" oncomplete="PF('dlg').show()" />

    <p:dialog header="Value" modal="true" resizable="false" showEffect="fade" widgetVar="dlg">
        <h:panelGrid columns="1" id="display">
            <h:outputText value="Latitude: #{geocodeView.latitude}" />
            <h:outputText value="Longitude: #{geocodeView.longitude}" />
        </h:panelGrid>
    </p:dialog>
</h:form>

支持bean类:

import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import org.primefaces.event.map.GeocodeEvent;
import org.primefaces.model.map.DefaultMapModel;
import org.primefaces.model.map.GeocodeResult;
import org.primefaces.model.map.LatLng;
import org.primefaces.model.map.MapModel;
import org.primefaces.model.map.Marker;

@ManagedBean
public class GeocodeView {

private MapModel geoModel;
private String centerGeoMap = "41.850033, -87.6500523";
private double latitude;
private double longitude;

@PostConstruct
public void init() {
    geoModel = new DefaultMapModel();
}

public void onGeocode(GeocodeEvent event) {
    List<GeocodeResult> results = event.getResults();

    if (results != null && !results.isEmpty()) {
        LatLng center = results.get(0).getLatLng();
        centerGeoMap = center.getLat() + "," + center.getLng();

        for (int i = 0; i < results.size(); i++) {
            GeocodeResult result = results.get(i);
            Marker currentMarker = new Marker(result.getLatLng(), result.getAddress());
            geoModel.addOverlay(currentMarker);

            // Here are problematic values
            latitude = currentMarker.getLatlng().getLat();
            longitude = currentMarker.getLatlng().getLng();

            System.out.println(latitude);
            System.out.println(longitude);
            }
        }
    }

    public MapModel getGeoModel() {
        return geoModel;
    }

    public String getCenterGeoMap() {
        return centerGeoMap;
    }

    public double getLatitude() {
        return latitude;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public double getLongitude() {
        return longitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }
}

1 个答案:

答案 0 :(得分:0)

在辅助bean中添加geoModel的setter。