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.

2
  • How are you firing the request to the server to initialize the managed bean: through a GET request by changing the current URL or through an ajax request? Commented Mar 20, 2014 at 14:55
  • throught a GET request, for my page navigation i use prettyfaces Commented Mar 20, 2014 at 15:31

2 Answers 2

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");
                …
}
Sign up to request clarification or add additional context in comments.

1 Comment

Should't the method be: getCurrentInstance()?
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);



    });

3 Comments

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
but you do not want to in html5? i see from you code in java or android or so
I want to pass those values to my managed bean because I need to make a POST request in my Java code

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.