2017-02-13 9 views
0

У меня проблема с передачей координат геолокации из managedBean, чтобы сохранить их в базе данных нового объекта. Я создаю ячейку базы данных, используя Hibernate, но не могу передать эти два атрибута.Передача ManagedBean в качестве атрибута Entity

Вот мой передний конец:

<script type="text/javascript"> 
    function geocodeEvent() { 
     PF('geoMap').geocode(document.getElementById('address').value); 
    } 
</script> 

<h3 style="margin-top:0">Geocode</h3> 
<h:panelGrid columns="3" style="margin-bottom:10px" cellpadding="5"> 
    <p:outputLabel for="address" value="Address:" /> 
    <p:inputText id="address" /> 
    <p:commandButton value="Geocode" icon="ui-icon-search" onclick="geocode()" type="button" /> 
</h:panelGrid> 

<!-- This is easy... --> 
<p:inputText id="entityName" value="#{entity.name}" required="true" label="Entity Name" title="Enter Entity Name" /> 

<p:gmap id="geoGmap" widgetVar="geoMap" center="#{entityGeocodeView.centerGeoMap}" zoom="2" type="ROADMAP" model="#{entityGeocodeView.geoModel}" style="width:100%;height:400px"> 
    <p:ajax event="geocode" listener="#{entityGeocodeView.onGeocode}" update="@this entityLongitude entityLatitude" /> 
</p:gmap> 

<!-- ...and here is the problem --> 
<!-- Values from there... --> 
<p:inputText id="thisValueLongitude" value="#{entityGeocodeView.longitude}"/? 
<p:inputText id="thisValueLatitude" value="#{entityGeocodeView.latitude}"/> 

<!-- ...I want to pass for set entity attributes when submit entity for saving to database --> 
<p:inputText id="entityLongitude" value="#{entity.locationLongitude}"/> 
<p:inputText id="entityLatitude" value="#{entity.locationLatitude}"/> 

И бэк Bean:

@ManagedBean 
@ViewScoped 
public class EntityGeocodeView { 

    private MapModel geoModel; 
    private String centerGeoMap = "41.850033, -87.6500523"; 
    private double latitude; 
    private double longitude; 

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

    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); 
       Marker currentMarker = new Marker(result.getLatLng(), result.getAddress()); 
       geoModel.addOverlay(currentMarker); 
       latitude = currentMarker.getLatlng().getLat(); 
       longitude = currentMarker.getLatlng().getLng(); 
      } 
     } 
    } 

    public MapModel getGeoModel() { 
     return geoModel; 
    } 

    public void setGeoModel(MapModel geoModel) { 
     this.geoModel = geoModel; 
    } 

    public String getCenterGeoMap() { 
     return centerGeoMap; 
    } 

    public double getLatitude() { 
     return latitude; 
    } 

    public void setLatitude(double latitude) { 
     this.latitude = latitude; 
    } 

    public double getLongitude() { 
     return longitude; 
    } 

    public void setLongitude(double longitude) { 
     this.longitude = longitude; 
    } 
} 
+0

Некоторые замечания: нет needvto добавить 'метки' в названии. И вопросы arevin 99.9% времени никогда не javascript (полностью клиентская сторона) и спящий режим (backend-db). Тщательно проверьте свои вопросы и разделите функциональность проблемы в логических технических частях – Kukeltje

ответ

0

Хорошо, понял. Я решил это, используя javascript, на основе google maps ajax.

Вот решение:

<script type="text/javascript"> 
    function changeValues() { 
     document.getElementById("entityLatitude").value = document.getElementById("thisValueLatitude").value; 
     document.getElementById("entityLongitude").value = document.getElementById("thisValueLongitude").value; 
    } 
</script> 

<p:gmap id="geoGmap" widgetVar="geoMap" center="#{entityGeocodeView.centerGeoMap}" zoom="2" type="ROADMAP" model="#{entityGeocodeView.geoModel}" style="width:100%;height:400px"> 
    <p:ajax event="geocode" listener="#{entityGeocodeView.onGeocode}" update="@this thisValueLongitude thisValueLatitude" oncomplete="changeValues()"/> 
</p:gmap>