简体   繁体   English

Primefaces gmap获取位置

[英]Primefaces gmap get location

I have a bean which I use to show the updated-location of dragged-marker. 我有一个Bean,用于显示拖动标记的更新位置。 Here is my code: 这是我的代码:

@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);
    }
}

I use the bean above in a form that I want user to fill. 我以希望用户填写的形式使用上述bean。 User fills the form, drags the marker in desired place and saves the information. 用户填写表格,将标记拖动到所需位置并保存信息。 Here is my other bean that saves the info to database. 这是我的另一个将信息保存到数据库的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";

    }
}

And here is my JSF: 这是我的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()" />

My problem is when I try to save the form, it gives the location as null. 我的问题是,当我尝试保存表单时,它将位置设置为null。 However I can see the long and lati as I drag the marker. 但是,当我拖动标记时,我可以看到多长的纬度。 How can I retrieve long and lati from MarkerLocationer bean correctly? 如何从MarkerLocationer bean正确检索long和lati? Thanks in advance. 提前致谢。


OK. 好。 I found the solution. 我找到了解决方案。 Here is the way how I basically did: 这是我基本上的工作方式:

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

You can just inject the MarkerLocationer into AddProjectToDB and retrieve the value from there. 您可以将MarkerLocationer注入AddProjectToDB并从那里检索值。 There is no need to use hidden fields. 无需使用隐藏字段。 Like this: 像这样:

MarkerLocationer.java (Updated) 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 (Updated) 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

    }
}

And the JSF page 和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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM