将数组传递给Tapestry中的javascript

时间:2011-09-19 21:45:00

标签: javascript google-maps-api-3 tapestry

在我的Tapestry模板中,我有一个RoutePoint对象数组(包含纬度和经度值),我想将它们传递给javascript,以便line of points can be displayed on a map.有人知道怎么做它?

1 个答案:

答案 0 :(得分:1)

有点像@Pointy指出,但Tapestry有一种更优雅的方式来初始化脚本,将其内联到<script>标签中。这是我的GoogleMap组件代码,显示了如何将地图添加到Tapestry页面,在地址上打印指针。有关如何使用行添加多个连接,请查看google maps API。

在Tapestry页面中:

@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);
}

使用@Import(library = {"GoogleMap.js"})向您的页面/组件添加javascript文件,其中GoogleMap.js看起来有点像(我使用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
            });
        }
    });
}
});
祝你好运!