将coldfusion查询结果作为javascript对象放入javascript数组中(谷歌地图)

发布于 2024-11-19 02:56:09 字数 967 浏览 3 评论 0 原文

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

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

"NY" :[new google.maps.LatLng(1,2), new google.maps.LatLng(3,4), new 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>

谢谢您的帮助。

I have a database of latitude and longitude values for state borders which I am using to draw polygons of the state on a map.

I am querying the database using coldfusion and I want the value to be returned like this
example

"NY" :[new google.maps.LatLng(1,2), new google.maps.LatLng(3,4), new google.maps.LatLng(5,6)]

for every state that is requested and then put it in a javascript array of stateBorders

see code below:

<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>

Thank you for your help.

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

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

发布评论

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

评论(4

赠意 2024-11-26 02:56:09

你可以尝试在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>

You can try to craete the datas you need in cf and then convert it into json format and pass i to js.
This is untested but can give you an idea:

<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>
夜雨飘雪 2024-11-26 02:56:09

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

For converting query object into JS object, there's ToScript(). However, if you need it in a specific JS object format, you've got to construct the struct carefully yourself, then maybe use SerializeJSON() to get the JSON representation of your object literal.

梦幻的心爱 2024-11-26 02:56:09
  1. 在 Coldfusion 中创建所需的变量:

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

    var toScript(stateID, "stateIDvar");

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

  1. Create the variables you want in coldfusion:

    <cfset stateID = #states.stateid#>

  2. Convert the CF variable into javascript variable:

    var toScript(stateID, "stateIDvar");

Where stateID is the name of the CF variable and stateIDvar will be the javascript name. And then insert the points into your javascript array in a while loop.

平生欢 2024-11-26 02:56:09

这是与@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);

Here is a similar answer to @Andrea Campolonghi's, this iterates through the column list, creating a struct that can then be json encoded (in case you dont care for how serializeJSON encodes queries with separate arrays for columnlist and data

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