簡體   English   中英

Primefaces地理編碼從備用Bean獲取0.0、0.0 gps坐標

[英]Primefaces geocode getting 0.0, 0.0 gps coordinates from backing bean

我想獲取使用primefaces地理編碼由地理編碼服務生成的點的緯度和經度,但我遇到了麻煩。 但是,當我嘗試從支持bean中獲取這些坐標時,我會收到0.0值。

如何在它們具有適當值(登錄到控制台后立即)而不是零的時間內訪問它們?

請注意,我是Spring和PF的新手。

這是我的素面前端,我想在其中訪問支持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中添加geoModel的設置器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM