0

Primefaces geocode event changes and centers the map successfully when the user enters an address and presses the button, but the map doesn't show the associated marker. How can I make that marker visible?

Html code:

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=..."></script>

<h:form id="direccionForm">
    <p:inputText id="address" />
    <p:commandButton value="Localizar" icon="fa fa-search" onclick="geocode()" type="button" />
    <p:gmap id="gmap" widgetVar="_gmap" center="#{manejadorComercio.gmapComercio.centerGeoMap}" zoom="15" type="hybrid" style="width:600px;height:400px" model="#{gmapComercio.model}" >
        <p:ajax event="geocode" listener="#{manejadorComercio.gmapComercio.onGeocode}" update="@this" />
    </p:gmap>
</h:form>

JS code:

function geocode() {
    var address = document.getElementById('direccionForm:address').value;
    PF('_gmap').geocode(address);
}

Java code:

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Scope;

@Name( "manejadorComercio" )
@Scope( ScopeType.CONVERSATION )
public class ManejadorComercio implements Serializable {

    private GMapComercioModel gmapComercio;

    @Create
    public void inicializa() {    
        gmapComercio = new GMapComercioModel();
    }
}

public class GMapComercioModel {

    private MapModel model = new DefaultMapModel();
    private String centerGeoMap = "40.4530541, -3.6905332";

    public GMapComercioModel() {
        model.addOverlay(new Marker(new LatLng(40.4530541, -3.6905332), "Bernabeu"));
    }

    public void onGeocode(GeocodeEvent event) {
        List<GeocodeResult> results = event.getResults();

        if (results != null && !results.isEmpty()) {
            LatLng center = results.get(0).getLatLng();
            centerGeoMap = center.getLat() + "," + center.getLng();

            for (int i = 0; i < results.size(); i++) {
                GeocodeResult result = results.get(i);
                model.addOverlay(new Marker(result.getLatLng(), result.getAddress()));
            }
        }
    }
}

Thanks in advance.

MrUpsidown
  • 21,592
  • 15
  • 77
  • 131
jbn1981
  • 73
  • 2
  • 9

1 Answers1

0

Stupid mistake, "model" attribute of p:gmap component should be

model="#{manejadorComercio.gmapComercio.model}

instead of

model="#{gmapComercio.model}".

That way it works.

jbn1981
  • 73
  • 2
  • 9