House of Fusion
Search over 2,500 ColdFusion resources here
  
Home of the ColdFusion Community

Mailing Lists
Home /  Groups /  ColdFusion Talk (CF-Talk)

problems with json and google maps

  << Previous Post |  RSS |  Sort Oldest First |  Sort Latest First |  Subscribe to this Group Next >> 
Top  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Seamus Campbell
04/20/2012 04:25 AM

Hi I'm trying to populate a google map from a cfquery. I serializeJSON the query then try and use that json object to populate the map. I'm using code from Gabriel Svennerberg's Google Maps books (see code below), but my json object from the query looks very different (in format) from the example json object that Svennerberg shows. His is in this format: var json = [   {     "title": "Stockholm",     "lat": 59.3,     "lng": 18.1,     "description": "Stockholm is the capital and the largest city of Sweden and constitutes the most populated urban area in Scandinavia with a population of 2.1 million in the metropolitan area (2010)"   },   {     "title": "Oslo",     "lat": 59.9,     "lng": 10.8,     "description": "Oslo is a municipality, and the capital and most populous city of Norway with a metropolitan population of 1,442,318 (as of 2010)."   } ] and mine is like this: {"COLUMNS":["BUSINESS_NAME","LATITUDE","LONGITUDE"],"DATA":[["sdfdsf Books",-36.890408,121.154751],["dsdsdsd Booksellers",-14.921735,18.602189],["fff ints",-17.8451823,195.0541819]]} Is there a way I can convert my query to a json object with the same format as Svennerberg's? any halp gratefully received <cfsilent> <cfquery name="get_map_details" datasource="#application.main_dsn#"> SELECT business_name, latitude, longitude FROM tbl_premises WHERE latitude <> 0 </cfquery> </cfsilent> <cfset json_query = serializeJSON(get_map_details,false)> <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script><script type="text/javascript"> (function() {   window.onload = function() {     // Creating a new map     var map = new google.maps.Map(document.getElementById("map"), {           center: new google.maps.LatLng(-24.327, 140.273),           zoom: 5,           mapTypeId: google.maps.MapTypeId.ROADMAP         });   // Creating a global infoWindow object that will be reused by all markers     var infoWindow = new google.maps.InfoWindow();   // Looping through the JSON data     for (var i = 0, length = json_query.length; i < length; i++) {       var data = json_query[i],       latLng = new google.maps.LatLng(data.latitude, data.longitude);       // Creating a marker and putting it on the map       var marker = new google.maps.Marker({         position: latLng,         map: map,         title: data.business_name       });     }   } })(); </script> </head> <body> <div id = "map" style="width: 800px; height: 500px; border: 1px solid #000;"></div> </body> </html>

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Edward Chanter
04/20/2012 04:40 AM

I've had similar problems to this when trying to serialise JSON for a jQuery plugin. The only solution I found that worked is detailed here: <!--- get the matches ---> <cfquery name="q" datasource="#request.ds#"> select product_id as value, product_title as label from products where product_title like <cfqueryparam value="%#url.term#%" /> </cfquery> <!--- where we'll hold the data to convert to JSON ---> <cfset data = [] /> <cfoutput query="q">   <!--- create a new structure on each iteration, forcing lowercase keys --->   <cfset obj = {     "label" = label,     "value" = value   } />   <!--- push this match onto the array --->   <cfset arrayappend(data, obj) /> </cfoutput> <!--- serialize the new structure ---> <cfoutput>#serializeJSON(data)#</cfoutput> http://goo.gl/VGxlA Credit goes to Eric Hynds not me for this one. HTH > Hi > I'm trying to populate a google map from a cfquery. > I serializeJSON the query then try and use that json object to populate the ----- Excess quoted text cut - see Original Post for more ----- Sweden and ----- Excess quoted text cut - see Original Post for more ----- populous ----- Excess quoted text cut - see Original Post for more ----- format ----- Excess quoted text cut - see Original Post for more ----- { ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Geoffrey Bentz
04/20/2012 09:28 AM

Scott Stroz put a function on CFLib.org that can help. http://cflib.org/udf/queryConvertForjqGrid He wrote it to convert a query to a json format for jQuery's jqGrid plugin, but I started using it this week to send json data for jQuery templates. It only took a slight tweak since the jQuery template did not need the extra page and total record information that jqGrid needs. Geoffrey

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Seamus Campbell
04/20/2012 06:08 PM

Thanks to all who replied. I used Edward Chanter solution which gave me the json object in what appeared to be the correct format but I still couldn't get it to work. I'm going to advertise to get someone to help me on cf-jobs.

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Steve 'Cutter' Blades
04/30/2012 08:33 AM

My serializeCFJSON JQuery plugin might help you with this https://github.com/cutterbl/serializeCFJSON Steve 'Cutter' Blades Adobe Community Professional Adobe Certified Expert Advanced Macromedia ColdFusion MX 7 Developer ____________ http://cutterscrossing.com Co-Author "Learning Ext JS 3.2" Packt Publishing 2010 https://www.packtpub.com/learning-ext-js-3-2-for-building-dynamic-desktop-style-user-interfaces/book "The best way to predict the future is to help create it" On 4/20/2012 6:07 PM, Seamus Campbell wrote: > Thanks to all who replied. I used Edward Chanter solution which gave me the json object in what appeared to be the correct format but I still couldn't get it to work. I'm going to advertise to get someone to help me on cf-jobs.


<< Previous Thread Today's Threads Next Thread >>

Search cf-talk

June 19, 2013

<<   <   Today   >   >>
Su Mo Tu We Th Fr Sa
             1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30             

Designer, Developer and mobile workflow conference