1

I have the following code in javascript:

        <script type="text/javascript"
            src="#{facesContext.externalContext.requestContextPath}/js/sample-points.js"></script>

        <script type="text/javascript">//<![CDATA[
            var cloudmade = new CM.Tiles.CloudMade.Web({key: 'bbb'});
            var map = new CM.Map('cm-example', cloudmade);
            map.setCenter(new CM.LatLng(51.50874, 22.76367), 4);

            var markers = [];
            for (var i = 0; i < samplePoints.length; i++) {
                markers.push(new CM.Marker(new CM.LatLng(samplePoints[i][0], samplePoints[i][1])));
            }

            var clusterer = new CM.MarkerClusterer(map, {clusterRadius: 70});
            clusterer.addMarkers(markers);
        //]]></script>

"samplePoints" is an array of coordinates which I can use to show markers on the map.

Map is showing here:

<div id="cm-example" style="width: 99.5%; height: 600px"></div>

How can I provide this array from jsf/richfaces without using file (e.g. I want to fetch those data from db, create array and send to this script)?

Thanks

Nikola
  • 624
  • 1
  • 14
  • 31

2 Answers2

2

Just let JSF print it as if it is JavaScript code.

Replace

var markers = [];
for (var i = 0; i < samplePoints.length; i++) {
    markers.push(new CM.Marker(new CM.LatLng(samplePoints[i][0], samplePoints[i][1])));
}

by (assuming Facelets)

var markers = [];
<ui:repeat value="#{bean.samplePoints}" var="samplePoint">
    markers.push(new CM.Marker(new CM.LatLng(#{samplePoint[0]}, #{samplePoint[1]})));
</ui:repeat>

where #{bean.samplePoints} returns a List<BigDecimal[]> or something.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • How do you mean "by (assuming Facelets)"? – Nikola Jun 03 '11 at 10:04
  • JSF can be used on old JSP or its successor Facelets (XML based). The `` is not available in old JSP. If you're using JSF 2.0 with XHTML files, then you're definitely using Facelets. – BalusC Jun 03 '11 at 11:18
  • I am using JSF 1.2 with xhtml. Does that mean I cannot use ? – Nikola Jun 03 '11 at 11:53
  • You can just use it. You've an `xmlns:ui="http://java.sun.com/jsf/facelets"` in root tag declaration, right? – BalusC Jun 03 '11 at 11:55
  • Yes, I have, but when I try to execute that part of code, I think that I never reach the loop (or lines inside ui:repeat). I got this generated code: ` markers.push(new CM.Marker(new CM.LatLng(samplePoint[0], samplePoint[1]))); ` Bean: `private List samplePoints; ... @Create public void init() { String[] a = new String[2]; a[0] = "50.4397550076928"; a[1] = "30.6139826774597"; samplePoints.add(a); } ... getter setter ` – Nikola Jun 03 '11 at 12:08
  • Then you don't have a `xmlns:ui="http://java.sun.com/jsf/facelets"` in root tag declaration. – BalusC Jun 03 '11 at 12:12
  • As I said, I have. But, I found that the problem is in wrong syntax: Instead of: `` it should be: `` But, thanks anyway – Nikola Jun 03 '11 at 12:55
  • Oh, I confused with the `` for JSP which has `items` instead of `value`. I'll fix the answer. – BalusC Jun 03 '11 at 13:18
1

See this Link

using jsFunction you can load any data structure (e.g. Points), and on your clients side you get a javaScript data structure that you can easily access it (point.x).

Community
  • 1
  • 1
Reza
  • 834
  • 1
  • 7
  • 18