1

I have to pass geolocation values got by using html5 geolocation to my managed bean ItemMb, here is my code to clarify things and i don't really know how to achieve this.

My managed bean

public class ItemMb {

    private Item item = new Item();

    // get/set
}

My dto Item

public class Item {

//  private Double idCollect;
    // login et password
    private Double lat;
    private Double lon;
    private Double accuracy;
    private String photo;
    private String remark;

    public Item(){
    }
// getters and setters

Here is my .js file where i got geolocations values and innertext them in some primefaces tags

window.watchID = navigator.geolocation.watchPosition(function(position){              

      document.getElementById("xCurrentPosition").innerHTML = (Math.round(position.coords.latitude*100) / 100);
      document.getElementById("yCurrentPosition").innerHTML = (Math.round(position.coords.longitude*100) / 100);
      document.getElementById("accuracyCurrentPosition").innerHTML = position.coords.accuracy;    

      });

Now i want to pass those JavaScript generated geolocation values to my managed bean ItemMb. If you need more informations feel free to ask.

Junctionbe
  • 51
  • 6

2 Answers2

4

I've finally found the answer, i will let it there in case if someone has the same problem. You have to follow these steps:

1) Define a primefaces remoteCommand

<p:remoteCommand name="myRemoteCommand" process="@this" autoRun="false" actionListener="#{myMb.myListener}"/>

2) Call the command in your JavaScript code

myRemoteCommand ([{name:'paramName1',   value: 'paramValue1'}, {name:'paramName2',     value: 'paramValue2'}, …]);

3) In the backing bean use this method

void myListener(){
                String param1 = (String) FacesContext.getInstance().getExternalContext().getRequestParameterMap().get("paramName1");
                …
}
Junctionbe
  • 51
  • 6
0

Here is the JavaScript code in the HTML5 application. i've tested it works.

navigator.geolocation.getCurrentPosition(function(position) {

    //lon      
    var lon = position.coords.longitude;

    //lat
    var lat = position.coords.latitude;

    //alert(lon+", "+lat);



    });
Tekinnn
  • 19
  • 4
  • That's not what i want unfortunally:p In my code i already display those value , but what i want is to store those geolocation value as data from my Item, i would like to set lon, lat and accuracy into Item property – Junctionbe Mar 20 '14 at 14:34
  • but you do not want to in html5? i see from you code in java or android or so – Tekinnn Mar 20 '14 at 14:39
  • I want to pass those values to my managed bean because I need to make a POST request in my Java code – Junctionbe Mar 20 '14 at 14:48