简体   繁体   English

如何将JSON数据从控制器传递到JavaScript

[英]How to pass json data from the controller to javascript

In my cluster Table have cluster coordinates column name description, i am directly storing coordinates in Json format like below is my cluster table ,using these coordinates i want to draw multiple cluster(polygons) in google map, 在我的群集表中有群集坐标列名称说明,我直接以Json格式存储坐标,如下所示是我的群集表,使用这些坐标我想在Google地图中绘制多个群集(多边形),

    id |       description                                                                                                                                                                                                
----+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  1 | {"points":[{"lat":14.232437996569354,"lng":76.409912109375},{"lat":13.336175186494927,"lng":77.1185302734375},{"lat":12.961735843534294,"lng":77.596435546875},{"lat":12.511665400971031,"lng":76.904296875}]}


 2   |    {"points":[{"lat":14.232437996569354,"lng":76.409912109375},  {"lat":13.336175186494927,"lng":77.1185302734375},{"lat":12.961735843534294,"lng":77.596435546875},{"lat":12.511665400971031,"lng":76.904296875}]}

I need to draw multiple polygon 我需要绘制多个多边形
but i am trying get this json into rails controller using 但是我正在尝试使用以下方式将这个json放入rails控制器

controller code 控制器代码

query="select id,description  from clusters;
@result=ActiveRecord::Base.connection.execute(query);

The problem i am facing here how to pass this @result to javascript to draw multiple polygon in google map. 我在这里面临的问题是如何将此@result传递给JavaScript以在Google地图中绘制多个多边形。 if copy this json into one varible in js i able to draw cluster but problem i am facing how pass this json data from database reading in controller to javascript in rails 如果将这个json复制到js中的一个变量中,我可以绘制集群,但是问题是我面临着如何将这个json数据从控制器中的数据库读取传递到rails中的javascript

my google map code in javascript if hard code json into one varible it is working but i need take this json from database how to pass json with id from controller to javascript 我的谷歌地图代码在JavaScript中,如果将json硬编码转换为一个变量是可行的,但是我需要从数据库中获取此json如何将ID为json的json从控制器传递到javascript

var obj=i need json to store here //this how to fetch json from controller to js  
polygons = [];
for(var i = 0; i < obj.length; ++i){
   //do something with obj[i]
   for(var ind in obj[i]) {
        console.log(ind);
          var arr = new google.maps.MVCArray();
        for(var vals in obj[i][ind]){
            console.log(vals, obj[i][ind][vals].lat );
                 arr.push( new google.maps.LatLng( obj[i][ind][vals].lat, obj[i][ind][vals].lng ) );
        }
   }

polygons.push(arr);
}       

    polygons.forEach( function(polyPath) {

        var polygon = new google.maps.Polygon({
            paths: polyPath,
            strokeColor: '#FF0000',
            strokeOpacity: 0.8,
            strokeWeight: 2,
            fillColor: '#FF0000',
            fillOpacity: 0.35,
            map: map
        });
    });
}

google.maps.event.addDomListener(window, 'load', initialize);

I would suggest an ajax-load for points. 我建议点一个ajax加载。 Then you can use the JSON-Output for rendering the action. 然后,您可以使用JSON-Output呈现操作。 It could be: 它可能是:

render json: @result

Other solution: When you have the googlemaps code in your view you could assign a json-string from the db to a javascript-variable: 其他解决方案:当您在视图中使用googlemaps代码时,可以将db中的json-string分配给javascript变量:

var json_string = '<%= @results.raw %>';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM