从Bean变量设置Gmap位置

时间:2017-02-08 13:14:27

标签: primefaces

如何使用我的Bean中的变量设置Primefaces Gmap标记的位置,而不是客户端的输入字段,并使用按钮触发它。

GeocodeView .java

@Named
@SessionScoped
public class GeocodeView implements Serializable {

private static final long serialVersionUID = 1L;

private MapModel geoModel;
private MapModel revGeoModel;
private String centerGeoMap = "41.850033, -87.6500523";
private String centerRevGeoMap = "41.850033, -87.6500523";

//I want to use this varibale for placing a marker in the map
private String place = "New York";


@PostConstruct
public void init() {
    geoModel = new DefaultMapModel();
    revGeoModel = new DefaultMapModel();
}

public void onGeocode(GeocodeEvent event) {
    List<GeocodeResult> results = new ArrayList<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);
            geoModel.addOverlay(new Marker(result.getLatLng(), result.getAddress()));
        }
    }
}

public void onReverseGeocode(ReverseGeocodeEvent event) {
    List<String> addresses = event.getAddresses();
    LatLng coord = event.getLatlng();

    if (addresses != null && !addresses.isEmpty()) {
        centerRevGeoMap = coord.getLat() + "," + coord.getLng();
        revGeoModel.addOverlay(new Marker(coord, addresses.get(0)));
    }
}

public MapModel getGeoModel() {
    return geoModel;
}

public MapModel getRevGeoModel() {
    return revGeoModel;
}

public String getCenterGeoMap() {
    return centerGeoMap;
}

public String getCenterRevGeoMap() {
    return centerRevGeoMap;
}
}

我的HTML

[....]
<h:form>
    <h:panelGroup id="panel">
        <f:view contentType="text/html">
            <p:gmap id="geoGmap" widgetVar="geoMap"
                    center="#{geocodeView.centerGeoMap}" zoom="8" type="ROADMAP"
                    model="#{geocodeView.geoModel}" style="width:100%;height:400px" />
        </f:view>
    </h:panelGroup>
    <p:poll update="panel" interval="5"></p:poll>
</h:form>
<script
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB1vfYoTcBN85M1q79xXMLZ4EU01zFUxJU"></script>
</h:body>
</html>

所以问题是,我可以看到地图但是bean的位置没有设置。

1 个答案:

答案 0 :(得分:0)

您可以将其添加到@PostConstruct方法中:

String adress = "location adress";    
locationMarker = new Marker(new LatLng(41.850033, -87.6500523), adress);
getGeoModel().addOverlay(locationMarker);