【问题标题】:put coldfusion query result into javascript array as a javascript object (google maps)将coldfusion查询结果作为javascript对象放入javascript数组中(谷歌地图)
【发布时间】:2011-07-07 17:38:13
【问题描述】:

我有一个州边界的纬度和经度值数据库,我用它在地图上绘制州的多边形。

我正在使用 Coldfusion 查询数据库,我希望像这样返回值 例子

“纽约”:[新 google.maps.LatLng(1,2),新 google.maps.LatLng(3,4),新 google.maps.LatLng(5,6)]

对于每个请求的状态,然后将其放入 stateBorders 的 javascript 数组中

见下面的代码:

<cfquery datasource="source" name="states">
select * from state_lat_long where stateid order by stateid, orderid
</cfquery>


var stateBorder={};//declare array to hold the state latitude and longitude values

//need to take this whole thing and put it into array, then loop through the array
<cfoutput query="states" group="stateid">
    var #states.stateid# = [
    "#states.stateid#":[
    <cfset count=0>
    <cfoutput> <cfif count>, </cfif>new google.maps.LatLng    (#states.latitude#,#states.longitude#)<cfset count=count +1></cfoutput>
    ]
    ];
    stateBorder.push(states.stateid);
</cfoutput>

感谢您的帮助。

【问题讨论】:

  • 您到底要问什么?我没有在任何地方看到任何问题。

标签: javascript arrays google-maps coldfusion


【解决方案1】:

您可以尝试在cf中创建您需要的数据,然后将其转换为json格式并将i传递给js。 这是未经测试的,但可以给你一个想法:

<cfquery datasource="source" name="states">
select * from state_lat_long where stateid order by stateid, orderid
</cfquery>

<cfset arr = ArrayNew(1)>

<cfoutput query="states">

    <cfset state = {#stateid# = 'new google.maps.LatLng(#states.latitude#,#states.longitude#)'}>
    <cfset arrayAppend(arr,state)>

</cfoutput>


<script type="text/javascript" charset="utf-8">
var states = <cfoutput>#serializeJson(arr)#</cfoutput>; 
</script>

【讨论】:

    【解决方案2】:

    用于将查询对象转换为JS对象,有ToScript()。但是,如果您需要特定的 JS 对象格式,则必须自己仔细构建结构,然后可能使用 SerializeJSON() 来获取对象字面量的 JSON 表示。

    【讨论】:

      【解决方案3】:
      1. 在coldfusion中创建你想要的变量:

        &lt;cfset stateID = #states.stateid#&gt;

      2. 将CF变量转换为javascript变量:

        var toScript(stateID, "stateIDvar");

      其中 stateID 是 CF 变量的名称,stateIDvar 将是 javascript 名称。然后在 while 循环中将这些点插入到您的 javascript 数组中。

      【讨论】:

        【解决方案4】:

        这是与@Andrea Campolonghi 的类似答案,它遍历列列表,创建一个可以进行 json 编码的结构(如果您不关心 serializeJSON 如何使用单独的列列表和数据数组对查询进行编码

        //this is the query
                rc.qAllocations=getmodel("somemodel").getQuery();
        
                returnArray = ArrayNew(1);
        
        var col2List = rc.qAllocations.Columnlist;      
        for (i=1; i<= rc.qAllocations.recordcount; i++) {
        Struct = StructNew();
        for (col2=1; col2 <= ListLen(col2List); col2++) {
            var stable2 = LCase(listGetAt(col2List, col2));;
                Struct[stable2] = #rc.qAllocations[stable2][i]#;
                ArrayAppend(returnArray,Struct);
            }
        }
        rc.json = serializeJson(returnArray);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-03-17
          • 1970-01-01
          • 2016-04-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-07-27
          相关资源
          最近更新 更多