0

I am developing a functionality in my jsf application, which should retrieve the lat and lon of places from the database and plot it in Google map. Since gmaps4jsf library is not updated with google maps java script api v3, i'm using google maps java script api as it is. The problem is that I can't call the script to plot the place in map after retrieving the lat and lon (via jsf).

What is the solution to this problem?

2
  • Welcome to StackOVerflow. We don't really care about your company name/address/etc. so there's no need to put it in your signature. Commented Apr 26, 2012 at 12:48
  • 1
    How exactly do you get the coordinates from the server? Please add some code! Commented Apr 26, 2012 at 12:56

1 Answer 1

2

Just let JSF print it as if it's a JS variable.

<h:form>
    <h:commandButton value="Submit" action="#{bean.submit}" />
</h:form>
<h:outputScript rendered="#{not empty bean.lat and not empty bean.lon}">
    var lat = #{bean.lat};
    var lon = #{bean.lon};
    initializeMapSomehowWith(lat, lon);
</h:outputScript>

(the <h:outputScript> generates a HTML <script type="text/javascript"> element, if you're still on old JSF 1.x, use <h:panelGroup><script> instead and also <h:outputText> instead of EL in template text)

with something like

public void submit() {
    lat = 12.106173;
    lon = -68.935304;
}

Remember: JSF basically generates HTML. JS is part of HTML response.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your input. Issue is that 2 actions has to be performed when the user clicks a buttonretrieving lat and lon from jsf and this value has to be used to plot the location in map using java script has to be accomplished in the user's click event.
Then just let JSF print that piece of HTML/JS conditionally with rendered attribute on for example some parent <h:panelGroup>. In the action method you of course take care that the rendered condition will evaluate true instead of default false.
Sorry I submitted the edit half way through by mistake. Thanks for your input. Requirement: 2 actions has to be performed when the user clicks a button. 1. Lat and Lon value needs to be retrieved from back end(preferably using jsf) . 2. The retrieved value then has to be used to plot the location in map using java script. I am able to do any one of the action above. Issue is, I am not able to accomplish both as mentioned in the sequence above. Could you please suggest?
The script got generated, but how to invoke that?
The initializeMapSomehowWith() function example does that. Substitute it with whatever JS function you need to invoke with the given arguments.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.