最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

put coldfusion query result into javascript array as a javascript object (google maps) - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question asked Jul 7, 2011 at 17:38 trstrs 2,45413 gold badges43 silver badges61 bronze badges 1
  • What is it exactly that you're asking? I don't see a question in there anywhere. – charliegriefer Commented Jul 7, 2011 at 18:12
Add a ment  | 

4 Answers 4

Reset to default 3

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>

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.

  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.

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);
发布评论

评论列表(0)

  1. 暂无评论