gpt4 book ai didi

java - Primefaces gmap 获取位置

转载 作者:行者123 更新时间:2023-12-01 14:56:56 26 4
gpt4 key购买 nike

我有一个 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 正确检索长和纬度?提前致谢。

<小时/>

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

            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 注入(inject) 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>

关于java - Primefaces gmap 获取位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14238574/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com