Primefaces gmap获取位置



我有一个bean,我用它来显示拖拽标记的更新位置。下面是我的代码:

@SessionScoped
@ManagedBean
public class MarkerLocationer implements Serializable {
    private static final long serialVersionUID = 1L;
    private MapModel draggableModel;
    private Marker marker;
    public MarkerLocationer() {
        draggableModel = new DefaultMapModel();
        //Shared coordinates  
        LatLng coord1 = new LatLng(41.017599, 28.985704);

        //Draggable  
        draggableModel.addOverlay(new Marker(coord1, "Projenin olduğu yere sürükleyin."));
        for (Marker marker2 : draggableModel.getMarkers()) {
            marker2.setDraggable(true);
        }
    }
    public Marker getMarker() {
        return marker;
    }
    public void setMarker(Marker marker) {
        this.marker = marker;
    }
    public MapModel getDraggableModel() {
        return draggableModel;
    }
    public String lon, lat;
    public void onMarkerDrag(MarkerDragEvent event) {
        marker = event.getMarker();
        addMessage(new FacesMessage(FacesMessage.SEVERITY_INFO, "Proje lokasyonu belirlendi.", "X:" + marker.getLatlng().getLat() + " Y:" + marker.getLatlng().getLng()));
        lon = String.valueOf(marker.getLatlng().getLng());
        lat = String.valueOf(marker.getLatlng().getLat());

    }

    public void addMessage(FacesMessage message) {
        FacesContext.getCurrentInstance().addMessage(null, message);
    }
}

我在一个希望用户填写的表单中使用上面的bean。用户填写表格,在需要的位置拖动标记并保存信息。这是我的另一个bean,它将信息保存到数据库。

 @ManagedBean
@ViewScoped
public class AddProjectToDB {

    String lat = new MarkerLocationer().lat;
    String lon = new MarkerLocationer().lon;

    //genel
    private String projectName, projectExp, projectCoordLong, projectCoordLat;
    /**
     * Creates a new instance of AddProjectToDB
     */
    static private FileHandler fileTxt;
    static private SimpleFormatter formatterTxt;
    int getter;
    public String Save() throws Exception {
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/projetakip?useUnicode=true&characterEncoding=utf-8", "root", "");
            Statement stmt = con.createStatement();

            String SQL = "insert into `projects`(`id`,`projectName`,`projectExp`,`projectCoordLat`,`projectCoordLon`) values "
                    + "(NULL,'"
                    + projectName + "','"
                    + projectExp + "','"
                    + lat + "','"
                    + lon + "')";
            int Res = stmt.executeUpdate(SQL);
            ResultSet rs = stmt.getGeneratedKeys();
            getter = rs.getInt(1);
            rs.close();
            stmt.close();
        return "0";
    }
}
这是我的JSF:
<h:outputLabel value="Proje Adı: " />  
                            <p:inputTextarea value="#{addProjectToDB.projectName}" id="projectName" autoResize="true"/>  
                            <h:outputLabel value="Proje Detayı: " />  
                            <p:inputTextarea value="#{addProjectToDB.projectExp}" id="projectExp" autoResize="true"/>  
                            <h:outputLabel value="Proje Koordinatları: " />
                            <h:inputHidden value="#{addProjectToDB.projectCoordLat}" id="lat" />
                            <h:inputHidden value="#{addProjectToDB.projectCoordLong}" id="lon" />
                            <h:form>
                                <p:growl id="growl" showDetail="true"/>
                                <script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>
                                    <p:gmap center="41.017599,28.985704" zoom="13" type="HYBRID"  model="#{markerLocationer.draggableModel}" style="width:600px;height:400px"> 
                                        <p:ajax event="markerDrag" listener="#{markerLocationer.onMarkerDrag}" update="growl"/>
                                        </p:gmap>
                                    </h:form>
    <p:commandButton  value="Projeyi Kaydet" icon="ui-icon-disk" action="#{addProjectToDB.Save}"  oncomplete="infDialog.show()" />

我的问题是,当我试图保存表单,它给出的位置为空。但是,当我拖动标记时,我可以看到长和宽。我怎样才能从MarkerLocationer bean正确检索长和lati ?提前谢谢。


OK。我找到了解决办法。以下是我的基本做法:

            FacesContext facesContext = FacesContext.getCurrentInstance();
            Application application = facesContext.getApplication();
            ValueBinding binding = application.createValueBinding("#{markerLocationer}");
            MarkerLocationer userInfo = (MarkerLocationer) binding.getValue(facesContext);
            projectCoordLat = String.valueOf(userInfo.getMarker().getLatlng().getLat());
            projectCoordLong = String.valueOf(userInfo.getMarker().getLatlng().getLng());

您可以将MarkerLocationer注入AddProjectToDB并从中检索值。没有必要使用隐藏字段。这样的:

MarkerLocationer.java(更新)

@ManagedBean
@SessionScoped
public class MarkerLocationer implements Serializable {
    private static final long serialVersionUID = 4288587129736579882L;
    private MapModel draggableModel;
    public MarkerLocationer(){
        draggableModel = new DefaultMapModel();
        //Shared coordinates
        LatLng coord1 = new LatLng(41.017599, 28.985704);
        //Draggable
        draggableModel.addOverlay(new Marker(coord1, "Projenin olduğu yere sürükleyin."));
        for (Marker marker : draggableModel.getMarkers()) {
            marker.setDraggable(true);
        }
    }
    public MapModel getDraggableModel() {
        return draggableModel;
    }
    public void setDraggableModel(MapModel draggableModel) {
        this.draggableModel = draggableModel;
    }
    public void onMarkerDrag(MarkerDragEvent event) {
        // since marker's state is already kept in draggableModel you do not necessarily
    }
    public LatLng getMarkerPosition(){
        return draggableModel.getMarkers().get(0).getLatlng();
    }
}

AddProjectToDB.java(更新)

@ManagedBean
@ViewScoped
public class AddProjectToDB {
    @ManagedProperty(value = "#{markerLocationer}")
    private MarkerLocationer markerLocationer;
    public void setMarkerLocationer(MarkerLocationer markerLocationer) {
        this.markerLocationer = markerLocationer;
    }
    public void save() {
        double lat = markerLocationer.getMarkerPosition().getLat();
        double lon = markerLocationer.getMarkerPosition().getLng();
        //.. your stuff
    }
}

和JSF页面

<h:form>
    <p:growl id="growl" showDetail="true" />
    <script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>
    <p:gmap center="41.017599,28.985704" zoom="13" type="HYBRID" model="#{markerLocationer.draggableModel}"
        style="width:600px;height:400px">
        <p:ajax event="markerDrag" listener="#{markerLocationer.onMarkerDrag}" update="growl" />
    </p:gmap>
    <p:commandButton value="Projeyi Kaydet" icon="ui-icon-disk" action="#{addProjectToDB.save}"/>
</h:form>

最新更新