【问题标题】:Primefaces geocode getting 0.0, 0.0 gps coordinates from backing beanPrimefaces 地理编码从支持 bean 获取 0.0、0.0 gps 坐标
【发布时间】:2017-02-04 16:08:41
【问题描述】:

我想获取地理编码服务使用 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;
    }
}

【问题讨论】:

  • 尝试在 bean 上使用 @ViewScoped(包中带有“bean”的那个)
  • 成功了!非常感谢。所以这是 bean 生命周期...
  • 是的 - 我认为默认是 RequestScoped,所以你会一直得到一个新的。试试看 - 在 init() 中放一个 System.out

标签: java spring google-maps jsf primefaces


【解决方案1】:

在backing bean中添加geoModel的setter。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-26
    • 2012-07-04
    • 1970-01-01
    • 2020-01-16
    • 2016-10-20
    • 2017-03-10
    相关资源
    最近更新 更多