Note that there are some explanatory texts on larger screens.

plurals
  1. POPrimefaces gmap get location
    primarykey
    data
    text
    <p>I have a bean which I use to show the updated-location of dragged-marker. Here is my code:</p> <pre><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); } } </code></pre> <p>I use the bean above in a form that I want user to fill. 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. </p> <pre><code> @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&amp;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"; } } </code></pre> <p>And here is my JSF:</p> <pre><code>&lt;h:outputLabel value="Proje Adı: " /&gt; &lt;p:inputTextarea value="#{addProjectToDB.projectName}" id="projectName" autoResize="true"/&gt; &lt;h:outputLabel value="Proje Detayı: " /&gt; &lt;p:inputTextarea value="#{addProjectToDB.projectExp}" id="projectExp" autoResize="true"/&gt; &lt;h:outputLabel value="Proje Koordinatları: " /&gt; &lt;h:inputHidden value="#{addProjectToDB.projectCoordLat}" id="lat" /&gt; &lt;h:inputHidden value="#{addProjectToDB.projectCoordLong}" id="lon" /&gt; &lt;h:form&gt; &lt;p:growl id="growl" showDetail="true"/&gt; &lt;script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"&gt;&lt;/script&gt; &lt;p:gmap center="41.017599,28.985704" zoom="13" type="HYBRID" model="#{markerLocationer.draggableModel}" style="width:600px;height:400px"&gt; &lt;p:ajax event="markerDrag" listener="#{markerLocationer.onMarkerDrag}" update="growl"/&gt; &lt;/p:gmap&gt; &lt;/h:form&gt; &lt;p:commandButton value="Projeyi Kaydet" icon="ui-icon-disk" action="#{addProjectToDB.Save}" oncomplete="infDialog.show()" /&gt; </code></pre> <p>My problem is when I try to save the form, it gives the location as 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? Thanks in advance.</p> <hr> <p>OK. I found the solution. Here is the way how I basically did:</p> <pre><code> 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()); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload