Display coordinates on the map with gmap

0

I want to get coordinates of the database and show them on the map along with the marker, but I can not. I think there is something missing in the view . It should show only the last location data stored in the DB.

Can anyone help me? The problem is that it fetches the data in the database, and if I manually enter the coordinates the map displays. So I do not think it's an error with the API key, but if I try to fetch from class gadoBean it does not search.

Follow the code below:

View:

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:p="http://primefaces.org/ui">
<h:head>
<script src="https://maps.googleapis.com/maps/api/js?Sensor=false"type="text/javascript"></script>
</h:head>
<title>Rastreamento</title>
<h:body>
 <h:form>
   <p:poll interval="10"
           listener="#{gadoBean.obterPosicaoTag}"
           update="panelMap"/>
<p:panel id="panelMap">
<p:gmap id="mapa"  

        center="#{gadoBean.center}" 
        zoom="18" 
        model="#{gadoBean.mapa}"  
        type="HYBRID" 
        style="width:600px;height:400px"/>
    </p:panel>
    </h:form>  
</h:body>
</html>

GadoBean:

package com.sisRastrbov.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;
import javax.inject.Inject;
import javax.inject.Named;

import com.sisRastrbov.model.Coordenadas;
import com.sisRastrbov.model.Fazenda;
import com.sisRastrbov.model.Gado;
import com.sisRastrbov.model.Tag;
import com.sisRastrbov.model.Usuario;
import com.sisRastrbov.repository.CoordenadasRep;
import com.sisRastrbov.repository.Usuarios;
import com.sisRastrbov.repository.fazendasRep;
import com.sisRastrbov.security.AuthenticationService;
import com.sisRastrbov.services.CoordenadasService;

import org.primefaces.model.map.Circle;
import org.primefaces.model.map.DefaultMapModel;
import org.primefaces.model.map.LatLng;
import org.primefaces.model.map.MapModel;
import org.primefaces.model.map.Marker;

@Named
@ViewScoped
public class GadoBean implements Serializable {

private static final long serialVersionUID = 1L;
private MapModel mapa;
private Marker marcador = null;
private Circle circulo = null;
private Tag tag;
private Gado gadoAtual;
private float lastLat, lastLong;
private Coordenadas coordenadas;
private List<Coordenadas> posicoes;
private Fazenda fazendaAtual;
private List<Fazenda> fazendas;
private Usuarios usuarios;
private String center;


@Inject
private CoordenadasService coordenadasService;

@Inject
private CoordenadasRep coordenadasRep;

public String getCenter() {
    return center;
}

public void setCenter(String center) {
    this.center = center;
}

private void limpar() {
    coordenadas = new Coordenadas();

}
//<p:commandButton value="Rastrear" id="botaoRastrear" action="# {gadoBean.obterPosicaoTag}" update="mapa"/>
public GadoBean(){
    mapa = new DefaultMapModel();
}
public void inicializar() {

}

public void salvar() {

    coordenadasService.salvar(coordenadas);
    limpar();

}
public void obterPosicaoTag() {
    posicoes = new ArrayList<Coordenadas>();
    posicoes = coordenadasRep.listar();
    int totalPosicoes = this.getPosicoes().size(); //obtem o total de   registros contidos na tabela Coordenadas.
    if (totalPosicoes > 0) {
        Coordenadas posicaoAtual = this.getPosicoes().get(totalPosicoes -  1);//posicao atual recebe sempre a última posição registrada
        LatLng coord = new  LatLng(posicaoAtual.getPosLongitude(),posicaoAtual.getPosLatitude());//coord  recebe o ponto.
        lastLong =(float)posicaoAtual.getPosLongitude();//recebem as coordenadas.
        lastLat = (float)posicaoAtual.getPosLatitude();
        center = lastLat + "," + lastLong;


        System.out.println(center);

        if (circulo == null) {//cria um circulo verde em volta do ponto 
            circulo = new Circle(coord, 15);
            circulo.setStrokeColor("#00ff00");
            circulo.setFillColor("#00ff00");
            circulo.setStrokeOpacity(0.5);
            circulo.setFillOpacity(0.5);
            mapa.addOverlay(circulo);
        } else {
            circulo.setCenter(coord);
        }
        if (marcador == null) {
            marcador = new Marker(coord);
            mapa.addOverlay(marcador);
        } else {
            marcador.setLatlng(coord);
        }

    } else {
        tag = null;
    }
}

public Marker getMarcador() {
    return marcador;
}

public MapModel getMapa() {

    return mapa;
}

public void setMapa(MapModel mapa) {
    this.mapa = mapa;
}

public Fazenda getFazendaAtual() {
    return fazendaAtual;
}

public void setFazendaAtual(Fazenda fazendaAtual) {
    this.fazendaAtual = fazendaAtual;
}

public void setMarcador(Marker marcador) {
    this.marcador = marcador;
}

public Circle getCirculo() {
    return circulo;
}

public void setCirculo(Circle circulo) {
    this.circulo = circulo;
}

public Tag getTagAtual() {
    return tag;
}

public void setTagAtual(Tag tagAtual) {
    this.tag = tagAtual;
}

public float getLastLat() {
    return lastLat;
}

public void setLastLat(float lastlat) {
    this.lastLat = lastlat;
}

public float getLastLong() {
    return lastLong;
}

public void setLastLong(float lastlong) {
    this.lastLong = lastlong;
}

public List<Coordenadas> getPosicoes() {
    return posicoes;
}

public void setPosicoes(List<Coordenadas> posicoes) {
    this.posicoes = posicoes;
}
public Coordenadas getCoordenadas() {
    return coordenadas;
}

public void setCoordenadas(Coordenadas coordenadas) {
    this.coordenadas = coordenadas;
   }




}

Coordinates:

    package com.sisRastrbov.model;

    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;
    @Entity
    @Table(name = "Coordenadas")
    public class Coordenadas implements Serializable {
    private static final long serialVersionUID = 1L;

    private Long id;
    // Gado gado_id;
    private double posLatitude;
    private double posLongitude;
    private Date posData;
    private Tag tag;



    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @ManyToOne
    @JoinColumn(name = "tag_id", nullable = false)
    public Tag getTag() {
        return tag;
    }

    public void setTag(Tag tag) {
        this.tag = tag;
    }

    /*@ManyToOne
    @JoinColumn(name = "gado_id", nullable = false)
    public Gado getGado_id() {
        return gado_id;
    }*/

    /*public void setGado_id(Gado gado_id) {
        this.gado_id = gado_id;
    }*/

    @Column(name = "pos_latitude")
    public double getPosLatitude() {
        return posLatitude;
    }

    public void setPosLatitude(double posLatitude) {
        this.posLatitude = posLatitude;
    }
    @Column(name = "pos_longitude")
    public double getPosLongitude() {
        return posLongitude;
    }

    public void setPosLongitude(double posLongitude) {
        this.posLongitude = posLongitude;
    }

    @Column(name = "pos_data")
    public Date getPosData() {
        return posData;
    }

    public void setPosData(Date posData) {
        this.posData = posData;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.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;
        Coordenadas other = (Coordenadas) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        return true;
    }
   }

Tag:

package com.sisRastrbov.model;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;




@Entity
@SequenceGenerator(name = "tag_sequence", sequenceName = "tag_sequence")
@Table(name = "tag")
public class Tag implements Serializable {

private static final long serialVersionUID = 1L;

private Long tagId;
private char tagStatus;
private String descricao;

List<Coordenadas> posicoes;

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator =  "tag_sequence")
@Column(name = "tag_id")
public Long getTagId() {
    return tagId;
}

public void setTagId(Long tagId) {
    this.tagId = tagId;
}

@Column(name = "tag_status")
public char getTagStatus() {
    return tagStatus;
}

public void setTagStatus(char tagStatus) {
    this.tagStatus = tagStatus;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((tagId == null) ? 0 : tagId.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;
    Tag other = (Tag) obj;
    if (tagId == null) {
        if (other.tagId != null)
            return false;
    } else if (!tagId.equals(other.tagId))
        return false;
    return true;
}

public String getDescricao() {
    return descricao;
}

public void setDescricao(String descricao) {
    this.descricao = descricao;
}
@OneToMany(mappedBy = "tag")
public List<Coordenadas> getPosicoes() {
    return posicoes;
}

public void setPosicoes(List<Coordenadas> posicoes) {
    this.posicoes = posicoes;
}

}

Ramp Coordinates:

package com.sisRastrbov.repository;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

import com.sisRastrbov.model.Coordenadas;



public class CoordenadasRep implements Serializable{
private static final long serialVersionUID = 1L;
@Inject
private EntityManager manager;

public Coordenadas guardar(Coordenadas coo) {
    return coo = manager.merge(coo);
}



public List<Coordenadas> listar(){
    List<Coordenadas> coords = new ArrayList<Coordenadas>();
    coords = manager.createQuery("from Coordenadas",   Coordenadas.class).getResultList();
    return coords;
}
}
    
asked by anonymous 08.07.2016 / 16:44

1 answer

0

Can you tell if the coordinate is correct?

What I noticed is that the center="# {gadoBean.center}" should contain Latitude and Longitude.

Example:

center="#{meuBean.latitude}, #{meuBean.longitude}"

Where:

private BigDecimal latitude;  
private BigDecimal longitude;

Treco on PrimeFaces

<p:gmap center="#{meuBean.latitude}, #{meuBean.longitude}" zoom="13" type="HYBRID" styleClass="ms-mapas" model="#{meuBean.mapa}" />

JS Excerpt

<script src="http://maps.google.com/maps/api/js?sensor=false"type="text/javascript"></script>
    
12.07.2016 / 16:55