Add markers to Gmap

Asked

Viewed 101 times

0

I want to add a marker to the point I’m showing on the map. It loads the map and everything, but I can’t add the marker.

<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>

Managedbean:

@Named
@SessionScoped
public class GadoBean implements Serializable {

private static final long serialVersionUID = 1L;
private MapModel mapa;
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 String center;
private LatLng coord;

@Inject
private CoordenadasService coordenadasService;

@Inject
private CoordenadasRep coordenadasRep;

public GadoBean() {

    mapa = new DefaultMapModel();


}

public void inicializar() {

}




public void obterPosicaoTag() {
    posicoes = new ArrayList<Coordenadas>();
    posicoes = coordenadasRep.listar();
    int totalPosicoes = this.getPosicoes().size(); 
    if (totalPosicoes > 0) {
        Coordenadas posicaoAtual = this.getPosicoes().get(totalPosicoes - 1);
        coord = new LatLng(posicaoAtual.getPosLongitude(), posicaoAtual.getPosLatitude());
        lastLong = (float) posicaoAtual.getPosLongitude();
        lastLat = (float) posicaoAtual.getPosLatitude();
        center = lastLat + "," + lastLong;
        System.out.println(center);
        mapa.addOverlay(new Marker(coord));




    }
}

//getters and setters

1 answer

0

I made some changes and now it’s working:

<f:metadata>
    <f:viewParam name="dummy"/>
    <f:event listener="#{gadoBean.obterPosicaoTag}" type="preRenderView"/>
</f:metadata>
<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>

No Bean:

public GadoBean() {
}

public void inicializar() {
}

public void obterPosicaoTag() {
    mapa = new DefaultMapModel();
    posicoes = new ArrayList<Coordenadas>();
    posicoes = coordenadasRep.listar();
    int totalPosicoes = this.getPosicoes().size();
    if (totalPosicoes > 0) {
        Coordenadas posicaoAtual = this.getPosicoes().get(totalPosicoes -  1);
        coord = new LatLng(posicaoAtual.getPosLatitude(),  posicaoAtual.getPosLongitude());
        mapa.addOverlay(new Marker(coord, "Gado"));
        lastLong = (float) posicaoAtual.getPosLongitude();
        lastLat = (float) posicaoAtual.getPosLatitude();
        center = lastLat + "," + lastLong;
   }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.