Primefaces gmap获取位置

时间:2013-01-09 15:23:19

标签: java jsf primefaces

我有一个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()" />

我的问题是当我尝试保存表单时,它将该位置设为null。但是当我拖动标记时,我可以看到长和拉。如何正确地从MarkerLocationer bean中检索long和lati?提前谢谢。


行。我找到了解决方案。这是我基本上做的方式:

            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());

1 个答案:

答案 0 :(得分:1)

您可以将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>