开发者

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

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 st开发者_JAVA技巧ate_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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜