开发者

Passing an array to javascript in Tapestry

In my Tapestry template, I have an array of RoutePoint objects (which consist of a latitude and a longitude value) and I want to pass them to javascript, so that the lin开发者_运维问答e of points can be displayed on a map. Has anyone an idea how to do it?


Somewhat like @Pointy noted but Tapestry has a more elegant way of initializing scripts that inlining it into a <script> tag. Here is my GoogleMap component code showing how I add a map to my Tapestry page printing a pointer on an address. For how to add multiple connecting with a line check out the google maps API.

In your Tapestry page:

@BeginRender
void doBeginRender(MarkupWriter writer) {

    String clientId = javaScriptSupport.allocateClientId(componentResources);
    String mapsScripURL = (request.isSecure()) ? SECURE_GOOGLE_MAPS_SCRIPT_URL : GOOGLE_MAPS_SCRIPT_URL;
    javaScriptSupport.importJavaScriptLibrary(mapsScripURL);


    writer.element("div", "id", clientId, "class", "googleMapCanvas " + clientId);
        writer.end();

    JSONObject parameters = new JSONObject();
    parameters.put("mapCanvasId", clientId);
    parameters.put("geoAddress", geoAddress);
    javaScriptSupport.addInitializerCall("GoogleMap", parameters);
}

Add a javascript file to your page/component using @Import(library = {"GoogleMap.js"}) where the GoogleMap.js looks somewhat like (I use prototype.js):

Tapestry.Initializer.GoogleMap = function (parameters) {
new GoogleMap(parameters);
};

var GoogleMap = { };
GoogleMap = Class.create({
initialize:function(parameters) {
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode( { 'address': parameters.geoAddress}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var mapOptions = {
                    zoom: 11,
                    center: results[0].geometry.location,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            var map = new google.maps.Map(document.getElementById(parameters.mapCanvasId), mapOptions);
            var marker = new google.maps.Marker({
              map: map, 
              position: results[0].geometry.location
            });
        }
    });
}
});

Good luck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜