Primefaces地理编码事件发生更改,并在用户输入地址并按下按钮时成功将地图居中,但地图未显示关联的标记。如何使该标记可见?
HTML代码:
<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代码:
function geocode() {
var address = document.getElementById('direccionForm:address').value;
PF('_gmap').geocode(address);
}
Java代码:
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()));
}
}
}
}
谢谢。
答案 0 :(得分:0)
愚蠢的错误,p:gmap组件的“模型”属性应为
model="#{manejadorComercio.gmapComercio.model}
代替
model="#{gmapComercio.model}".
那样工作。