在 Tapestry 中将数组传递给 javascript

发布于 2024-12-05 12:59:54 字数 222 浏览 1 评论 0原文

在我的 Tapestry 模板中,我有一个 RoutePoint 对象数组(由纬度和经度值组成),我想将它们传递给 javascript,以便 点线可以 显示在地图上。 有人知道该怎么做吗?

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 line of points can
be displayed on a map.
Has anyone an idea how to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

末が日狂欢 2024-12-12 12:59:56

有点像 @Pointy 指出的,但 Tapestry 有一种更优雅的方式来初始化脚本,将其内联到

在您的 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
            });
        }
    });
}
});

祝你好运!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文