我正在尝试通过单击获取纬度和经度。我已经尝试了另一个主题的解决方案,但是单击地图时我得到了 0。她是我的 xhtml 页面。
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<ui:composition template="/template/main.xhtml">
<ui:define name="content">
<!--h:outputScript name="js/myjs.js" /-->
<h1 class="page-header text-center">
<i class="fa fa-exclamation-triangle"></i> Denunciar
</h1>
<div id="wrapper">
<h:form id="cadastrarDenuncia" styleClass="form-horizontal" p:role="form">
<!-- Número de denuncias -->
<h:inputHidden value="#{denunciaBean.denuncia.ndenuncia}"/>
<!-- Tabs para visualizacao -->
<p:wizard flowListener="#{denunciaBean.onFlowProcess}">
<!-- Tipos de Crime -->
<p:tab id="tabtipocrime" title="Local do Crime">
<div class="form-group panel panel-default">
<div class="panel-body">
<h:outputLabel for="idTipoCrime" class="col-md-4 control-label">Tipo de Crime: </h:outputLabel>
<div class="col-md-4">
<h:selectOneMenu class="form-control" id="idTipoCrime" value="#{denunciaBean.denuncia.tipoCrime}" required="true" requiredMessage="Campo obrigatório!">
<f:selectItem itemValue="" itemLabel="Selecione"/>
<f:selectItem itemValue="Roubo" itemLabel="Roubo"/>
<f:selectItem itemValue="Furto" itemLabel="Furto"/>
<f:selectItem itemValue="TraficoEn" itemLabel="Tráfico de Entorpecentes"/>
<f:selectItem itemValue="Homicidio" itemLabel="Homicídio"/>
<f:selectItem itemValue="Latrocinio" itemLabel="Latrocício"/>
<f:selectItem itemValue="Receptacao" itemLabel="Receptação"/>
<f:selectItem itemValue="Estupro" itemLabel="Estupro"/>
<f:selectItem itemValue="Quadrilha" itemLabel="Quadrilha"/>
<f:selectItem itemValue="CrimeAmbiental" itemLabel="Crime Ambiental"/>
<f:selectItem itemValue="Sequestro" itemLabel="Sequestro"/>
<f:selectItem itemValue="ViolenciaDomestica" itemLabel="Violência Doméstica"/>
<f:selectItem itemValue="Assassinato" itemLabel="Assassinato"/>
<f:selectItem itemValue="Agressao" itemLabel="Agressão"/>
<f:selectItem itemValue="Ameaca" itemLabel="Ameaça"/>
<f:selectItem itemValue="Pedofilia" itemLabel="Pedofilia"/>
</h:selectOneMenu>
</div>
<div class="text-danger"><h:message for="idTipoCrime"/></div>
</div>
</div>
</p:tab>
<!-- Local do Crime-->
<p:tab id="tabcrimelocal" title="Local do Crime">
<div class="form-group panel panel-default">
<div class="panel-body">
<h:outputLabel class="col-md-4 control-label">Clique no local aproximado do acontecimento: </h:outputLabel>
<p:growl id="mensagem" showDetail="true" />
<p:gmap id="idmapcrimelocal" center="-10.9232843, -37.0501223" zoom="15" type="HYBRID" widgetVar="idmapcrimelocal" style="width:470px; height:300px" onPointClick="handlePointClick(event);">
</p:gmap>
<p:remoteCommand name="onPoint" actionListener="#{denunciaBean.onPointSelect}" update="mensagem" />
<h:inputHidden id="lat" value="#{denunciaBean.denuncia.latitude}" />
<h:inputHidden id="lng" value="#{denunciaBean.denuncia.longitude}" />
</div>
</div>
</p:tab>
<!-- Outras Informacoes -->
<p:tab id="tabextrainformation" title="Observações">
<div class="form-group panel panel-default">
<div class="panel-body">
<!-- Data e Hora do Crime -->
<div class="form-group">
<h:outputLabel class="col-md-4 control-label" for="idDataHoraCrime">Data e Hora do crime: </h:outputLabel>
<div class="col-md-4">
<h:inputText id="data" label="Data e Hora do Crime" value="#{denunciaBean.denuncia.dataHoraCrime}"
size="16" maxlength="16" required="true">
<f:convertDateTime dateStyle="long" pattern="dd/MM/yyyy HH:mm" />
</h:inputText>
</div>
<div class="col-md-8 control-label text-danger"><h:message for="idDataHoraCrime"/></div>
</div>
<!-- Observacoes-->
<div class="form-group">
<h:outputLabel class="col-md-4 control-label" for="idObservacao">Observações: </h:outputLabel>
<div class="col-md-4">
<h:inputTextarea class="form-control input-md" value="#{denunciaBean.denuncia.observacao}" id="idObservacao" placeholder="Observacao"/>
</div>
<div class="col-md-8 control-label text-danger"><h:message for="idObservacao"/></div>
</div>
<!-- Botões -->
<div class="form-group">
<label class="col-md-4 control-label" for="idConfirmar"></label>
<div class="col-md-8">
<h:commandButton id="idConfirmarBt" styleClass="btn btn-primary" action="#{denunciaBean.salvar}" value="Confirmar"
onclick="if (!confirm('Confirmar denúncia?')) return false;"/>
<h:commandButton styleClass="btn btn-danger" value="Cancelar" onclick="redirecionar()"/>
</div>
</div>
</div>
</div>
</p:tab>
</p:wizard>
</h:form>
</div>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?key=AIzaSyAj8u8gZyCkVaMTiRcTXWclFgs0s7sv2nY&" />
<script type="text/javascript" >
function handlePointClick(event) {
if (navigator.geolocation) {
browserSupportFlag = true;
var latlng = event.latLng;
geocoder = new google.maps.Geocoder();
geocoder.geocode({
'latLng' : latlng
}, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
onPoint([ {
name : 'lng',
value : event.latLng.lng()
}, {
name : 'lat',
value : event.latLng.lat()
} ]);
}
});
}
}
</script>
</ui:define>
</ui:composition>
</html>
这是我的课。
package br.com.smartpatrolling.denuncia;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import br.com.smartpatrolling.usuario.Usuario;
@Entity
@Table(name = "denuncia")
public class Denuncia implements Serializable {
private static final long serialVersionUID = -387555820515169694L;
@Id
@GeneratedValue
private Integer ndenuncia;
@ManyToOne
// Permite excluir todas as denuncias do usuario ao excluir um usuário
@OnDelete(action = OnDeleteAction.CASCADE)
@JoinColumn(nullable = false)
private Usuario usuario;
private String tipoCrime;
/** Local do crime */
private double latitude;
private double longitude;
private String observacao;
/** Data e hora aproximados do crime */
private Date dataHoraCrime;
/** Data e hora do cadastro da denuncia */
@Column(nullable = false, updatable = false)
private Date dataHoraCadastro;
public Integer getNdenuncia() {
return ndenuncia;
}
public void setNdenuncia(Integer ndenuncia) {
this.ndenuncia = ndenuncia;
}
public Usuario getUsuario() {
return usuario;
}
public void setUsuario(Usuario usuario) {
this.usuario = usuario;
}
public String getTipoCrime() {
return tipoCrime;
}
public void setTipoCrime(String tipoCrime) {
this.tipoCrime = tipoCrime;
}
public String getObservacao() {
return observacao;
}
public void setObservacao(String observacao) {
this.observacao = observacao;
}
public Date getDataHoraCrime() {
return dataHoraCrime;
}
public void setDataHoraCrime(Date dataHoraCrime) {
this.dataHoraCrime = dataHoraCrime;
}
public Date getDataHoraCadastro() {
return dataHoraCadastro;
}
public void setDataHoraCadastro(Date dataHoraCadastro) {
this.dataHoraCadastro = dataHoraCadastro;
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((dataHoraCadastro == null) ? 0 : dataHoraCadastro.hashCode());
result = prime * result + ((dataHoraCrime == null) ? 0 : dataHoraCrime.hashCode());
long temp;
temp = Double.doubleToLongBits(latitude);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(longitude);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + ((ndenuncia == null) ? 0 : ndenuncia.hashCode());
result = prime * result + ((observacao == null) ? 0 : observacao.hashCode());
result = prime * result + ((tipoCrime == null) ? 0 : tipoCrime.hashCode());
result = prime * result + ((usuario == null) ? 0 : usuario.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Denuncia other = (Denuncia) obj;
if (dataHoraCadastro == null) {
if (other.dataHoraCadastro != null)
return false;
} else if (!dataHoraCadastro.equals(other.dataHoraCadastro))
return false;
if (dataHoraCrime == null) {
if (other.dataHoraCrime != null)
return false;
} else if (!dataHoraCrime.equals(other.dataHoraCrime))
return false;
if (Double.doubleToLongBits(latitude) != Double.doubleToLongBits(other.latitude))
return false;
if (Double.doubleToLongBits(longitude) != Double.doubleToLongBits(other.longitude))
return false;
if (ndenuncia == null) {
if (other.ndenuncia != null)
return false;
} else if (!ndenuncia.equals(other.ndenuncia))
return false;
if (observacao == null) {
if (other.observacao != null)
return false;
} else if (!observacao.equals(other.observacao))
return false;
if (tipoCrime == null) {
if (other.tipoCrime != null)
return false;
} else if (!tipoCrime.equals(other.tipoCrime))
return false;
if (usuario == null) {
if (other.usuario != null)
return false;
} else if (!usuario.equals(other.usuario))
return false;
return true;
}
}
这是我的豆子。(更新)
package br.com.smartpatrolling.web;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import org.primefaces.event.FlowEvent;
import org.primefaces.event.map.PointSelectEvent;
import org.primefaces.model.map.LatLng;
import br.com.smartpatrolling.denuncia.Denuncia;
import br.com.smartpatrolling.denuncia.DenunciaRN;
@ManagedBean(name = "denunciaBean")
@RequestScoped
public class DenunciaBean {
private Denuncia denuncia = new Denuncia();
private List<Denuncia> lista = null;
@ManagedProperty(value = "#{contextoBean}")
private ContextoBean contextoBean;
public String salvar() {
this.denuncia.setUsuario(this.contextoBean.getUsuarioLogado());
DenunciaRN denunciaRN = new DenunciaRN();
denunciaRN.salvar(this.denuncia);
this.denuncia = new Denuncia();
this.lista = null;
return "minhasDenuncias";
}
public String excluir() {
DenunciaRN denunciaRN = new DenunciaRN();
denunciaRN.excluir(this.denuncia);
this.denuncia = new Denuncia();
this.lista = null;
return null;
}
public List<Denuncia> getLista() {
if (this.lista == null){
DenunciaRN denunciaRN = new DenunciaRN();
this.lista = denunciaRN.listarDenuncias(this.contextoBean.getUsuarioLogado());
}
return lista;
}
public void onPointSelect(PointSelectEvent event) {
LatLng latlng = event.getLatLng();
this.denuncia.setLatitude(latlng.getLat());
this.denuncia.setLongitude(latlng.getLng());
addMessage(new FacesMessage(FacesMessage.SEVERITY_INFO, "Point Selected", "Lat:" + latlng.getLat() + ", Lng:" + latlng.getLng()));
}
public void addMessage(FacesMessage message) {
FacesContext.getCurrentInstance().addMessage(null, message);
}
public void setLista(List<Denuncia> lista) {
this.lista = lista;
}
public Denuncia getDenuncia() {
return denuncia;
}
public void setDenuncia(Denuncia denuncia) {
this.denuncia = denuncia;
}
public String onFlowProcess(FlowEvent event) {
return event.getNewStep();
}
public ContextoBean getContextoBean() {
return contextoBean;
}
public void setContextoBean(ContextoBean contextoBean) {
this.contextoBean = contextoBean;
}
}
这是打印屏幕。(更新)
更新了部分视图
<!-- Local do Crime-->
<p:tab id="tabcrimelocal" title="Local do Crime">
<div class="form-group panel panel-default">
<div class="panel-body">
<h:outputLabel class="col-md-4 control-label">Clique no local aproximado do acontecimento: </h:outputLabel>
<p:gmap id="idmapcrimelocal" center="-10.9232843, -37.0501223" zoom="15" type="HYBRID" widgetVar="idmapcrimelocal" style="width:100%; height:300px">
<p:ajax event="pointSelect" listener="#{denunciaBean.onPointSelect}" update="mensagem" />
</p:gmap>
</div>
</div>
</p:tab>